Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for default verb? #174

Closed
ericnewton76 opened this issue Nov 4, 2017 · 10 comments
Closed

Support for default verb? #174

ericnewton76 opened this issue Nov 4, 2017 · 10 comments

Comments

@ericnewton76
Copy link
Member

Issue by alexreg
Monday Oct 16, 2017 at 17:20 GMT
Originally opened as gsscoder/commandline#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?

@ericnewton76
Copy link
Member Author

Comment by alexreg
Monday Oct 16, 2017 at 17:38 GMT


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

@ericnewton76
Copy link
Member Author

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.

@ericnewton76
Copy link
Member Author

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!

@ericnewton76
Copy link
Member Author

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?

@ericnewton76
Copy link
Member Author

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.

@ericnewton76
Copy link
Member Author

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.

@ericnewton76
Copy link
Member Author

Comment by TheFanatr
Saturday Oct 21, 2017 at 07:16 GMT


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

@tkouba
Copy link
Contributor

tkouba commented Nov 9, 2018

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));
            }
        }
    }

@rhegner
Copy link

rhegner commented Feb 19, 2019

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?

@moh-hassan
Copy link
Collaborator

V2.8 support default verb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants