Skip to content

So, how does FCLP compare to other parsers?

siywilliams edited this page Apr 25, 2013 · 6 revisions

Here is a basic side-by-side syntax comparison between the Fluent Command Line Parser and other popular command line parsers. The examples are taken from their main pages, with the right column containing the code converted to the FCLP syntax.

NDesk.Options Fluent Command Line Parser
```c# static void Main(string[] args) { string data = null; bool help = false; int verbose = 0;

var p = new OptionSet () { { "file=", v => data = v }, { "v|verbose", v => { ++verbose; } }, { "h|?|help", v => help = v != null }, };

List extra = p.Parse (args); }

    </td>
    <td valign="top">
```c#
static void Main(string[] args)
{
   var p = new FluentCommandLineParser();

   string data = null;
   bool verbose = false;
   bool help   = false;

   p.Setup<string>("file")
    .Callback(f => data = f);

   p.Setup<bool>('v', "verbose")
    .Callback(v => verbose = v);

   p.SetupHelp("h", "?", "help")
    .Callback(() => help = true);

   var result = fclp.Parse(args);
}
</td>
Command Line Parser Fluent Command Line Builder
```c# // Define a class to receive parsed values class Options { [Option('r', "read", Required = true, HelpText = "Input file to be processed.")] public string InputFile { get; set; }

[Option('v', "verbose", DefaultValue = true, HelpText = "Prints filename to console.")] public bool Verbose { get; set; }

[ParserState] public IParserState LastParserState { get; set; }

[HelpOption] public string GetUsage() { return HelpText.AutoBuild(this, hlp => HelpText.DefaultParsingErrorsHandler(this, hlp)); } } // Consume them static void Main(string[] args) { var options = new Options();

if (Parser.Default.ParseArguments(args, options)) { // Values are available here if (options.Verbose) Console.WriteLine("File: {0}", options.InputFile); } }

    </td>
    <td valign="top">
```c#
// Define a class to receive parsed values
public class Options
{
   public string InputFile { get; set; }
   public bool Verbose { get; set; }
}
// Consume them
static void Main(string[] args)
{
   var b = new FluentCommandLineBuilder<Options>();

   b.Setup(option => option.InputFile)
    .As('r', "read")
    .WithDescription("Input file to be processed.")
    .Required();
			
   b.Setup(option => option.Verbose)
    .As('r', "read")
    .WithDescription("Prints filename to console.")
    .SetDefault(true);

   b.SetupHelp("?", "help")
    .Callback(hlpTxt => Console.WriteLine(hlpTxt));

   var result = fclb.Parse(args);

   if (result.HasErrors == false)
   {
      // Values are available here
      if (b.Object.Verbose)
         Console.WriteLine("File: {0}", b.Object.InputFile);
   }
}
</td>
Clone this wiki locally