2017-07-24 30 views
0

我正在使用FluentCommandLineParser來處理我的命令行實用程序的解析。 我想設置它,使不同的命令,採取不同的參數。流利的命令行解析器調用函數

我已經成立了一個單獨的語法分析器,像這樣:

public class RoslynCliOptions 
{ 
    public string SolutionFile { get; set; } 
} 

public static FluentCommandLineParser<RoslynCliOptions> GetRoslynCliOptionParser() 
{ 
    var parser = new FluentCommandLineParser<RoslynCliOptions>(); 

    parser.Setup(x => x.SolutionFile).As('s', "SolutionPath"); 
    return parser; 
} 

現在,所有的這是有道理的,如果我只是將選項傳遞給命令行,我可以從我的殼呢

MyExecutable -s="C://SolutionPath.Sln" 

如何配置命令行解析器來解析命令。

所以我可以打電話

MyExecutable GenerateClass --name="foo" 

回答

1

命令仍處於預發佈,但是你可以下載via nuget或得到下面teamcity.jetbrains.com

例,也see here從最新版本的組件。

myapp.exe add "c:\file1.txt" "c:\file2.txt" --verbose --ignore-errors 
myapp.exe rem "c:\file1.txt" "c:\file2.txt" --verbose 

我們在這裏有兩個命令,分別是Add和Remove,都可以有不同的選項和規則。

// Contains all arguments for the add command 
class AddArgs { 
bool Verbose; 
bool IgnoreErrors; 
IEnumerable<string> Files; 
} 

// Contains all arguments for the remove command 
class RemoveArgs { 
bool Verbose; 
IEnumerable<string> Files; 
} 

// entry point into console app 
static void Main(string[] args) { 
var fclp = new FluentCommandLineParser(); 

// use new SetupCommand method to initialise a command 
var addCmd = fclp.SetupCommand<AddArgs>("add") 
        .Callback(args => Add(args)); // executed when the add command is used 

// the standard fclp framework, except against the created command rather than the fclp itself 
addCmd.Setup(args => args.Verbose) 
     .As('v', "verbose") 
     .SetDefault(false) 
     .WithDescription("Be verbose"); 

addCmd.Setup(args => args.IgnoreErrors) 
     .As("ignore-errors") 
     .SetDefault(false) 
     .WithDescription("If some files could not be added, do not abort"); 

addCmd.Setup(args => args.Files) 
     .As('f', "files") 
     .Description("Files to be tracked") 
     .UseForOrphanArguments(); 

// add the remove command 
var removeCmd = fclp.SetupCommand<RemoveArgs>("rem") 
        .Callback(args => Remove(args)); // executed when the remove command is used 

removeCmd.Setup(args => args.Verbose) 
      .As('v', "verbose") 
      .SetDefault(false) 
      .WithDescription("Be verbose"); 

removeCmd.Setup(args => args.Files) 
      .As('f', "files") 
      .WithDescription("Files to be untracked") 
      .UseForOrphanArguments(); 

fclp.Parse(args); 
} 

void Add(AddArgs args){ 
    // add was executed 
} 

void Remove(RemoveArgs args){ 
    // remove was executed 
} 
+0

恢復這個舊線程的道具,definetly將在未來有用 –