Commandline: Usage example not outputting option enum in example

Created on 16 Jul 2019  路  4Comments  路  Source: commandlineparser/commandline

Hi,

The DataStore.Console option enumeration is not outputting to the console when printing the usage information.

CommandLineParser version 2.5.0

Parser config:

var parser = new Parser(with => {
                with.CaseInsensitiveEnumValues = true;
                with.AutoHelp = true;
                with.AutoVersion = true;
                with.HelpWriter = Console.Out;
         });

Parser options (With usage example):

[Verb("fetch", HelpText = "Retrieve data from the Solar API and save the response to a data store.")]
    internal class FetchOptions : Options
    {
        [Option('c', longName: "collection", HelpText = "Data collections to retrieve.", Required =true)]
        public string Collections { get; set; }

        [Option('s', longName: "store", HelpText = "Where to save the data", Required = true)]
        public DataStore Store { get; set; }


        [Usage(ApplicationAlias = "poller")]
        public static IEnumerable<Example> Examples
        {
            get
            {
                return new List<Example>() {
                new Example("Fetch CumulationInverterData collection and output to the console", new FetchOptions { Collections = "CumulationInverterData",  Store = DataStore.Console })
            };
            }
        }
    }

    internal enum DataStore
    {
        Console,
        Csv,
        Mssql
    }

Console output:

$ dotnet FroniusSolarApi.Poller.dll fetch help
FroniusSolarApi.Poller 1.0.0
Copyright (C) 2019 FroniusSolarApi.Poller

ERROR(S):
  Required option 'c, collection' is missing.
  Required option 's, store' is missing.
USAGE:
Fetch CumulationInverterData collection and output to the console:
  poller fetch --collection CumulationInverterData

  -c, --collection    Required. Data collections to retrieve.

  -s, --store         Required. Where to save the data

  -v, --verbose       Set output to verbose messages.

  --help              Display this help screen.

  --version           Display version information.

Expected result:

I would expect the enum property would be included in the usage example as follows:

poller fetch --collection CumulationInverterData --store Console

question

Most helpful comment

To show enum options, you can use custom help and enable AddEnumValuesToHelpText as given below:

        public static void Main(string[] args)
        {
            var parser = new Parser(with => {
                    with.CaseInsensitiveEnumValues = true;
                    //with.AutoHelp = true; 
                    //with.AutoVersion = true;
                    with.HelpWriter = null; //Console.Out;
             });

               var result= parser.ParseArguments<FetchOptions, object>(args);//use at least two verbs, object is a dummy verb

                result.MapResult(
                  (FetchOptions opts) => RunAddAndReturnExitCode(opts),  
                  errs => {
                      var helpText = HelpText.AutoBuild(result, h =>    
          {
                        // Configure HelpText    
                           h.AddEnumValuesToHelpText=true;                              
            return h;
        }, 
        e =>e, 
                    verbsIndex :true);  //to show Verb help summary, set verbsIndex =true
         Console.WriteLine(helpText);
        return 1;
         });                
        }

The help text is:
dotnet ConsoleApp1.dll fetch help

ConsoleApp1 1.0.0
Copyright (C) 2019 ConsoleApp1
USAGE:
Fetch CumulationInverterData collection and output to the console:
poller fetch --collection CumulationInverterData

-c, --collection Required. Data collections to retrieve.

-s, --store Required. Where to save the data Valid values: Console,
Csv, Mssql

--help Display this help screen.

--version Display version information.

All 4 comments

To show enum options, you can use custom help and enable AddEnumValuesToHelpText as given below:

        public static void Main(string[] args)
        {
            var parser = new Parser(with => {
                    with.CaseInsensitiveEnumValues = true;
                    //with.AutoHelp = true; 
                    //with.AutoVersion = true;
                    with.HelpWriter = null; //Console.Out;
             });

               var result= parser.ParseArguments<FetchOptions, object>(args);//use at least two verbs, object is a dummy verb

                result.MapResult(
                  (FetchOptions opts) => RunAddAndReturnExitCode(opts),  
                  errs => {
                      var helpText = HelpText.AutoBuild(result, h =>    
          {
                        // Configure HelpText    
                           h.AddEnumValuesToHelpText=true;                              
            return h;
        }, 
        e =>e, 
                    verbsIndex :true);  //to show Verb help summary, set verbsIndex =true
         Console.WriteLine(helpText);
        return 1;
         });                
        }

The help text is:
dotnet ConsoleApp1.dll fetch help

ConsoleApp1 1.0.0
Copyright (C) 2019 ConsoleApp1
USAGE:
Fetch CumulationInverterData collection and output to the console:
poller fetch --collection CumulationInverterData

-c, --collection Required. Data collections to retrieve.

-s, --store Required. Where to save the data Valid values: Console,
Csv, Mssql

--help Display this help screen.

--version Display version information.

Thanks for the example @moh-hassan

However now my fetch verb does not get outputted when i run the help command.

           var parser = new Parser(with => {
                with.CaseInsensitiveEnumValues = true;
                //with.AutoHelp = true;
                //with.AutoVersion = true;
                with.HelpWriter = null; //Console.Out;
            });
            var result = parser.ParseArguments<FetchOptions, object>(args);
            return result.MapResult(
                        (FetchOptions opts) => poller.FetchAndSave(opts),
                    errs => {
                        var helpText = HelpText.AutoBuild(result, h =>
                        {
                            // Configure HelpText    
                            h.AddEnumValuesToHelpText = true;
                            return h;
                        }, e => e);

                        Console.WriteLine(helpText);
                        return 1;
                    });

dotnet FroniusSolarApi.Poller.dll help

FroniusSolarApi.Poller 1.0.0
Copyright (C) 2019 FroniusSolarApi.Poller

  --help       Display this help screen.

  --version    Display version information.

Since i have disabled AutoHelp in the parser options do i now need to handle the verb help output too?

To show help summary, set the parameter verbsIndex=true in AutoBuild.
I updated my code and add the parameter.

@moh-hassan perfect, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timotei picture timotei  路  4Comments

ericnewton76 picture ericnewton76  路  3Comments

Valinwolf picture Valinwolf  路  3Comments

Silverwing1983 picture Silverwing1983  路  4Comments

ppumkin picture ppumkin  路  4Comments