1

I am using the CommandLineParser and literally pasting the example code into my example project. I get alot of errors such as:

Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'DefaultValue' could not be found (are you missing a using directive or an assembly reference?)
Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'ParserStateAttribute' could not be found (are you missing a using directive or an assembly reference?)

Am I not including a library or something? I have included CommandLine and I have installed the package via nuget https://archive.codeplex.com/?p=commandline.

using System;
using CommandLine;

namespace Foo
{
    class Program
    {
        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 all messages to standard output.")]
            public bool Verbose { get; set; }

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

            [HelpOption]
            public string GetUsage()
            {
                return HelpText.AutoBuild(this,
                  (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
            }
        }

        static void Main(string[] args)
        {
            var options = new Options();
            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {
                // Values are available here
                if (options.Verbose) Console.WriteLine("Filename: {0}", options.InputFile);
            }
        }
    }
}
sazr
  • 24,984
  • 66
  • 194
  • 362
  • 1
    That example is just very much outdated. Refer to github repository for actual examples: https://github.com/commandlineparser/commandline – Evk Mar 26 '18 at 08:56
  • Have you checked this: https://github.com/commandlineparser/commandline/issues/219? – Lennart Mar 26 '18 at 08:56
  • @Evk thanks, I have seen this and I also tried that example. I got another error in the line that performs the parsing: `Does not contain a definition for .WithParsed` – sazr Mar 26 '18 at 09:01

3 Answers3

3

It seems DefaultValue and ParserStateAttribute are no longer part of the API. Check out the up-to-date demo project which is part of the GitHub repository. Also check out the quickstart examples in project's README.md.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • thanks I'll check it out. Is this library still well used and maintained or would you suggest another cmd line parser for C#? – sazr Mar 26 '18 at 09:02
  • 1
    While the CodePlex page is outdated (as CodePlex closed down), the GitHub page seems to be pretty active and well maintained (even using the current project organization practices), and the number of downloads on NuGet speaks for itself, so I would say the project is a very good choice :-) . (https://www.nuget.org/packages/commandlineparser) – Martin Zikmund Mar 26 '18 at 09:05
0

Just want to add this as an alternative for those that may be looking for other command line parsing libraries: RunInfoBuilder

It allows you to specify how commands should be parsed by using object trees. It's a bit unique in that it doesn't use the typical Attributes to mark properties, everything is done using configuration via code.

Disclaimer: I'm the author of the library.

Let me know if you guys have any questions, more than happy to help!

Isaiah Lee
  • 448
  • 2
  • 10
0

I think FluentArgs (see: https://github.com/kutoga/FluentArgs) would be a good solution for your problem. It already includes a nice help (default trigger flags: -h and --help) and is quite readable. There's the code:

using FluentArgs;
using System;

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            FluentArgsBuilder.New()
                .DefaultConfigs()
                .Parameter("-r", "--read")
                    .WithDescription("Input file to be processed.")
                    .IsRequired()
                .Flag("-v", "--verbose")
                    .WithDescription("Prints all messages to standard output.")
                .Call(verbose => inputFile =>
                {
                    /* Application code */
                    if (verbose)
                    {
                        Console.WriteLine("Filename: {0}", inputFile);
                    }
                })
                .Parse(args);
        }
    }
}

Possible calls:

  • myapp -r myfile.txt
  • myapp --read myfile.txt -v
  • myapp --verbose - myfile.txt
  • etc.
Kevin Meier
  • 2,339
  • 3
  • 25
  • 52