fclp / fluent-command-line-parser

A simple, strongly typed .NET C# command line parser library using a fluent easy to use interface
Other
530 stars 86 forks source link

git style commands #82

Closed siywilliams closed 6 years ago

siywilliams commented 7 years ago

See also https://github.com/fclp/fluent-command-line-parser/issues/26

OK so I've been creating quite a few projects recently that have git style commands, I've been expanding fclp to support this. This is optional and fclp can still be used without commands.

Example below and also here

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

We have two commands here, Add and Remove, both can have different options and rules.

// 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
}
siywilliams commented 6 years ago

Build now working on teamcity.jetbrains.com and pre-release nuget package containing this functionality.