You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
allow_extras/prefix_command and subcommands don't mix well. The main problem is that allow_extras/prefix_command has to be set on the parent, rather than the subcommand, for it to have any effect. This implies:
If no subcommand is given, extra arguments are ignored and parsing stops at the first unknown positional. There is no way to get allow_extra/prefix_command only for subcommands but not for the parent.
You can't have some subcommands that are the prefix of an operation, and some subcommands that are not.
To regain composability, allow_extras/prefix_command should take affect only at the level they are given.
Say I want to allow extra positional arguments to the subcommand, but not to the main program. My intuitive approach would be to set "allow_extras" on the subcommand ([3] above). I'd then expect this to work:
$ ./a.out -v cmd -w foo
ERROR: ExtrasError: [foo]
But for some reason, allow_extras() is already set on subcommands (in App.hpp::add_subcommand), and there is no change to the above. Similarily, prefix_command() in [4] has no effect that I could find.
So, the next thing to try is to set allow_extras() on the main app [1], but this will now lead to this:
$ ./a.out -v foo # OK
REST: foo
v: 1
w: 0
$ ./a.out -v cmd -w foo # NOT OK, should fail with ExtrasError, and app.parse() should not return ["foo"]
REST: foo
v: 1
w: 0
$ ./a.out -v foo cmd -w bar # NOT OK, should fail with ExtrasError, and app.parse() should not return ["foo", "bar"]
REST: foo
REST: bar
v: 1
w: 1
By the way, it is unclear to me how to get remaining positional arguments from within a callback handler added with set_callback[5].
Similar for prefix_command().
This is all very confusing to me. Intuitively, unless fallthrough is set, subcommand options should be completely separate from the parent, and should be parsed pretty much independently. This also seems to be the simplest thing to implement. I am not sure what the motivation is to behave differently (except maybe to support the fallthrough case).
Initial comment: you are absolutely right in your guess. Fallthrough was added first, since it was needed for the first application of CLI11 (GooFit). While allow_extras was implemented soon after, it was probably influenced by the fallthrough case. Prefix command was added much later at a request on GitHub.
Anyway, unless there's a barrier in the implementation, I think these changes can and should be made. I think what we'll need to do is add a way for apps to hold their unparsed arguments, which would also fix the non-parsed issue for subcommands, which I do think is related.
Please see if #37 fixes the problem. I really didn't address the issue directly, I mostly just fixed the behavior of the remaining options to be more accessible and intuitive. However, it should have fixed your issue as well! (I added a couple of tests, feel free to suggest more).
I'll take another look. In the meantime I side-stepped the issue entirely by forking Taywee/Args, which I found easier to hack (the implementation was easier to read and understand for me), but I want to come back to CLI11 as soon as possible. Taywee/Args has some interesting things going for it, but storing the values inside the option objects is a mistake. CLI11's approach of accepting a reference to the actual destination of the option value is cleaner, IMO. I also know better now what complexity I need from the subcommand feature, so I'll see if anything is missing from #37. Thanks!
Yeah, #37 is looking pretty good. I tried a couple of things, including nested subcommands, and it didn't break. I think it's a bit odd that allow_extras and prefix_command are inherited, but maybe I am just lacking imagination. Overriding it explicitely in subcommands works fine.
Activity
henryiii commentedon Nov 4, 2017
Can you put up a little pseudo code example?
lambdafu commentedon Nov 4, 2017
Sure!
Without modifications, this program will behave as expected:
Say I want to allow extra positional arguments to the subcommand, but not to the main program. My intuitive approach would be to set "allow_extras" on the subcommand ([3] above). I'd then expect this to work:
But for some reason, allow_extras() is already set on subcommands (in App.hpp::add_subcommand), and there is no change to the above. Similarily, prefix_command() in [4] has no effect that I could find.
So, the next thing to try is to set allow_extras() on the main app [1], but this will now lead to this:
By the way, it is unclear to me how to get remaining positional arguments from within a callback handler added with set_callback[5].
Similar for prefix_command().
This is all very confusing to me. Intuitively, unless fallthrough is set, subcommand options should be completely separate from the parent, and should be parsed pretty much independently. This also seems to be the simplest thing to implement. I am not sure what the motivation is to behave differently (except maybe to support the fallthrough case).
Thanks!
lambdafu commentedon Nov 5, 2017
Possibly related: It is unclear to me how to get the remaining non-parsed arguments from the subcommand callback.
henryiii commentedon Nov 5, 2017
I'll try to get back to you later today!
henryiii commentedon Nov 6, 2017
Initial comment: you are absolutely right in your guess. Fallthrough was added first, since it was needed for the first application of CLI11 (GooFit). While
allow_extraswas implemented soon after, it was probably influenced by the fallthrough case. Prefix command was added much later at a request on GitHub.Anyway, unless there's a barrier in the implementation, I think these changes can and should be made. I think what we'll need to do is add a way for apps to hold their unparsed arguments, which would also fix the non-parsed issue for subcommands, which I do think is related.
henryiii commentedon Nov 16, 2017
Please see if #37 fixes the problem. I really didn't address the issue directly, I mostly just fixed the behavior of the remaining options to be more accessible and intuitive. However, it should have fixed your issue as well! (I added a couple of tests, feel free to suggest more).
lambdafu commentedon Nov 18, 2017
I'll take another look. In the meantime I side-stepped the issue entirely by forking Taywee/Args, which I found easier to hack (the implementation was easier to read and understand for me), but I want to come back to CLI11 as soon as possible. Taywee/Args has some interesting things going for it, but storing the values inside the option objects is a mistake. CLI11's approach of accepting a reference to the actual destination of the option value is cleaner, IMO. I also know better now what complexity I need from the subcommand feature, so I'll see if anything is missing from #37. Thanks!
lambdafu commentedon Nov 19, 2017
Yeah, #37 is looking pretty good. I tried a couple of things, including nested subcommands, and it didn't break. I think it's a bit odd that allow_extras and prefix_command are inherited, but maybe I am just lacking imagination. Overriding it explicitely in subcommands works fine.
henryiii commentedon Nov 19, 2017
I’m planning to revisit inheriting settings, actually. Either to expand it or reduce it, I’m not sure which. But will standardize it.