Commandline: Support for default verb?

Created on 4 Nov 2017  路  10Comments  路  Source: commandlineparser/commandline

Issue by alexreg
_Monday Oct 16, 2017 at 17:20 GMT_
_Originally opened as https://github.com/gsscoder/commandline/issues/494_


Is there any support for some sort of "default" verb? That is, a verb that gets executed when no verb is actually specified. Perhaps an existing verb can be marked as default, even?

Most helpful comment

I have some legacy options (without a verb) and need to add some verbs. I came up with a similar workaround like @tkouba. The problem with such an approach is that help texts are no longer automatically generated.

So built-in support for default verbs would be much appreciated.

The CLAP library (https://github.com/adrianaisemberg/CLAP) seems to support default verbs, but I'm hesitating to move to that library, because it does not look like it's still actively maintained.

Is supporting default verbs on the roadmap for this library?

All 10 comments

Comment by alexreg
_Monday Oct 16, 2017 at 17:38 GMT_


Furthermore, is it possible to have global options while still having a verb?

Comment by nemec
_Monday Oct 16, 2017 at 17:46 GMT_


This is a duplicate of #490. Unfortunately, no it isn't possible. With the way verbs work in the current version (e.g. Parse<VerbA, VerbB>(args)) I'm having a hard time thinking of a way to add a default verb and global options.

Comment by alexreg
_Monday Oct 16, 2017 at 18:06 GMT_


Thanks for the quick reply, @nemec. What about a simple IsDefault property of the Verb attribute. The name of the verb should also be optional in this case. As for global options, what about a special Options generic argument that inherits from a (dummy) GlobalOptions class, or something similar?

Update: Actually, quite a nice solution that already works is simply inheriting from a GlobalOptions class!

Comment by alexreg
_Monday Oct 16, 2017 at 18:46 GMT_


I'm currently trying to come up with a workaround for this, but I can't get the NoVerbSelectedError, nor the BadVerbSelectedError. It just runs my (one and only) options handler instead. Using the latest beta here. Any known issue with that right now?

Comment by alexreg
_Monday Oct 16, 2017 at 20:28 GMT_


Update: it seems I have to specify at least two options types to ParseArguments for these errors to kick in! I see why, though perhaps this should be documented.

Comment by nemec
_Tuesday Oct 17, 2017 at 00:38 GMT_


You're right - it is not obvious that one generic argument is interpreted as an Options class but multiple are considered Verbs.

Comment by TheFanatr
_Saturday Oct 21, 2017 at 07:16 GMT_


Just to clarify, is there a workaround by using GlobalOptions?

I try to find a workaround. I use solution like this and it seems to work.

    [Verb("add", HelpText = "Add file contents to the index.")]
    class AddOptions
    { //normal options here
    }
    [Verb("commit", HelpText = "Record changes to the repository.")]
    class CommitOptions
    { //normal options here
    }
    class CloneOptions
    { //normal options here
    }

    class Program
    {
        static void Main(string[] args)
        {
            Parser parser = new Parser(o => 
            {
                o.HelpWriter = null;
            });
            var result = parser.ParseArguments<AddOptions, CommitOptions>(args);

            if ((result is NotParsed<object>) &&
                ((NotParsed<object>)result).Errors.Any(o => o.Tag == ErrorType.NoVerbSelectedError || o.Tag == ErrorType.BadVerbSelectedError))
            {
                parser.ParseArguments<CloneOptions>(args)
                    .WithParsed<CloneOptions>(opts => Console.WriteLine(opts))
                    .WithNotParsed(errs => Console.WriteLine(errs.First().Tag));
            }
            else
            {
                result
                  .WithParsed<AddOptions>(opts => Console.WriteLine(opts))
                  .WithParsed<CommitOptions>(opts => Console.WriteLine(opts))
                  .WithNotParsed(errs => Console.WriteLine(errs.First().Tag));
            }
        }
    }

I have some legacy options (without a verb) and need to add some verbs. I came up with a similar workaround like @tkouba. The problem with such an approach is that help texts are no longer automatically generated.

So built-in support for default verbs would be much appreciated.

The CLAP library (https://github.com/adrianaisemberg/CLAP) seems to support default verbs, but I'm hesitating to move to that library, because it does not look like it's still actively maintained.

Is supporting default verbs on the roadmap for this library?

V2.8 support default verb

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rohansi picture Rohansi  路  6Comments

timotei picture timotei  路  4Comments

ppumkin picture ppumkin  路  4Comments

ericnewton76 picture ericnewton76  路  5Comments

hakito picture hakito  路  3Comments