-
Notifications
You must be signed in to change notification settings - Fork 481
Is there a way to implement multiple level verbs? #13
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
Comments
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. |
I see. So are there any plans to support nested verbs in future versions? |
This would probably be a breaking change. But I definitely want this. Was anyone working on a PR? |
@Nate-Wilkins I was able to implement this feature in my project without actually modifying this project. 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. |
@akfish Great thanks! I'll take a gander |
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) |
@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. |
@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. |
A small trick I found that works quite well. When I find a match with an existing sub-parser (verb), I skip the 1st argument, and forward it to another Parser. This could be done recursively, and probably in a better way than reflection. interface ISubParser
{
int RunAndReturnExitCode(string[] args);
}
[Verb("repo", HelpText = "Manage repositories.")]
public class RepoVerb : ISubParser
{
int ISubParser.RunAndReturnExitCode(string[] args)
{
return Parser.Default.ParseArguments<AddOptions, RemoveOptions>(args)
.MapResult(
(AddOptions opts) => AddRepo.RunAndReturnExitCode(opts),
(RemoveOptions opts) => RemoveRepo.RunAndReturnExitCode(opts),
errs => 1);
}
}
class Program
{
private static Type GetSubParser(string verb)
{
return Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x =>
typeof(ISubParser).IsAssignableFrom(x)
&& x.GetCustomAttribute<VerbAttribute>()?.Name == verb);
}
static int Main(string[] args)
{
if (args.Length > 0)
{
Type type = GetSubParser(args[0]);
if (type != null)
{
// Command with sub-verbs
return (Activator.CreateInstance(type) as ISubParser).RunAndReturnExitCode(args.Skip(1).ToArray());
}
}
return Parser.Default.ParseArguments<RepoVerb, AccountVerb>(args)
.MapResult(
(RepoVerb opts) => 1,
(AccountVerb opts) => 1,
errs => 1);
}
} |
Thursday Oct 31, 2013 at 19:48 GMT
Originally opened as gsscoder/commandline#107
I am trying to make an app with multiple level of verbs, something like:
What I tried is:
In each verb, nested a sub verb:
main:
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.
The text was updated successfully, but these errors were encountered: