1
votes

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);
            }
        }
    }
}
3
That example is just very much outdated. Refer to github repository for actual examples: github.com/commandlineparser/commandline - Evk
@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

3 Answers

2
votes

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.

0
votes

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!

0
votes

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.