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

Is there a way to implement multiple level verbs? #107

Open
akfish opened this issue Oct 31, 2013 · 12 comments
Open

Is there a way to implement multiple level verbs? #107

akfish opened this issue Oct 31, 2013 · 12 comments

Comments

@akfish
Copy link

akfish commented Oct 31, 2013

I am trying to make an app with multiple level of verbs, something like:

app repo list
app repo add
app account list

What I tried is:

    class Options
    {
        #region Global Options
        //...
        #endregion

        #region Help
        //..
        #endregion

        #region Verbs
        [VerbOption("account", HelpText = "Manage mail accounts")]
        public AccountOptions AccountVerb { get; set; }

        [VerbOption("repo", HelpText = "Manage repositories")]
        public RepoOptions RepoVerb { get; set; }
        #endregion
    }

In each verb, nested a sub verb:

    class AccountOptions : IOptions
    {
        [VerbOption("add", HelpText = "Add an account")]
        public AddOption AddVerb { get; set; }

        //And so on....

        public class AddOption : IOptions
        {
        }
        //And so on....

    }

main:

    class Program
    {
        static void Main(string[] args)
        {
            string theVerb = null;
            IOptions theOptions = null;

            var options = new Options();
            if (CommandLine.Parser.Default.ParseArguments(args, options, (verb, subOptions) =>
            {
                theVerb = verb;
                if (verb != null)
                    theOptions = subOptions as IOptions;
            }))
            {
                theOptions.Execute(options);
            }
            else
            {
                Console.Error.WriteLine("Command line parsing error.");
            }
        }
    }

I was able to get first level of verbs to working (e.g. app repo, app account, as the documents said. But the second level ones (app account list, app account add) didn't.

So is there a way to do this? Thanks.

@akfish
Copy link
Author

akfish commented Nov 1, 2013

After some messing around I found that the sub verbs did get initialized correctly. So the problem was to get the last level of verb instance. That's easily solve by some recursive checking.

Though it would be cool to have a build-in way to get the parsed verb-path in these multiple level situation.

@nemec
Copy link
Collaborator

nemec commented Nov 2, 2013

There was some talk of this in #17, but I don't think nested verbs ever made it into the code. As you mentioned, it's probably quite easy if we switch to a recursive parsing model.

@akfish
Copy link
Author

akfish commented Nov 2, 2013

I see. So are there any plans to support nested verbs in future versions?

@nemec
Copy link
Collaborator

nemec commented Nov 2, 2013

Not as far as I know. I'm sure @gsscoder would accept a pull request for it, but I haven't seen him around in a while.

@Nate-Wilkins
Copy link

This would probably be a breaking change. But I definitely want this. Was anyone working on a PR?

@akfish
Copy link
Author

akfish commented Dec 2, 2014

@Nate-Wilkins I was able to implement this feature in my project without actually modifying this project.
Here is my implementation:
https://github.com/akfish/git-mail/tree/develop/git-mail

You should know that the project linked above was never finished, since later I moved on to node.js for cross-platform purpose and I haven't been working on .Net platform for almost a year.
But the CLI part was completed and would hopefully give you some hints.

@Nate-Wilkins
Copy link

@akfish Great thanks! I'll take a gander

@cosmo0
Copy link

cosmo0 commented Dec 2, 2014

If you create this feature that doesn't break the existing usage, I accept PRs into my fork (which is the only one more or less active at this point, I believe)

@Nate-Wilkins
Copy link

@nemec Could you point me to some of the code that does the verb parsing? I'll be taking a look at this but no promises :(

@cosmo0 I'll send the PR to your fork should I also send one here? (This is all assuming I implement the feature...)

@akfish Looks like the general gist is to use a sub parser 👍 project looks pretty cool too.

@cosmo0
Copy link

cosmo0 commented Dec 2, 2014

Well, you can do both, but @gsscoder has vanished from the web, so I'm pretty sure he won't merge it.

@nemec
Copy link
Collaborator

nemec commented Dec 2, 2014

@Nate-Wilkins try this file or this file.

@cosmo0 I did manage to get a reply from Giacomo, he said he's busy at work these days and doesn't have time for this project.

@cosmo0
Copy link

cosmo0 commented Dec 3, 2014

@nemec : great to hear that he's still alive, at least.

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