Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git style commands #82

Merged
merged 23 commits into from
Dec 3, 2017
Merged

git style commands #82

merged 23 commits into from
Dec 3, 2017

Conversation

siywilliams
Copy link
Member

@siywilliams siywilliams commented Apr 17, 2017

See also #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 and others added 23 commits February 9, 2015 13:49
…not yet hooked up to the actual parser (so doesn't actually work)
…e tests pass (should do) and the functionality is there.
- only call OnSuccess if parsing is successful
- rename properties to reflect new name
…ample:

my.exe add --file file1.txt file2.txt

can instead be supplied as (where --file is defined as the primary option):

my.exe add file1.txt file2.txt
- fix issue with incorrect grouping.
…value is valid. For example a nullable bool that is provided but no explicit value is valid as a default for true
@siywilliams
Copy link
Member Author

siywilliams commented Nov 25, 2017

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

@siywilliams siywilliams merged commit 5a5f9d9 into develop Dec 3, 2017
@siywilliams siywilliams deleted the commands branch December 3, 2017 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant