Skip to content

allow_extras/prefix_command and subcommands #36

Closed

Description

@lambdafu

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.

Activity

  1. henryiii commented on Nov 4, 2017

    @henryiii
    Collaborator

    Can you put up a little pseudo code example?

  2. lambdafu commented on Nov 4, 2017

    @lambdafu
    ContributorAuthor

    Sure!

    #include <CLI/CLI.hpp>
    
    int main(int argc, char** argv) {
      CLI::App app{"test"};
      // [1] This will allow "foo -v" as well as "cmd -w foo" etc.                                                                                                                                         
      // app.allow_extras();
      // [2] Even weirder:
      // app.prefix_command();                                                                                                                                                             
    
      int v = 0;
      app.add_flag("-v", v);
      CLI::App* cmd = app.add_subcommand("cmd", "Invoke cmd");
      // [5] cmd->set_callback([](){ std::cout << "cmd\n"; throw CLI::Success(); });                                                                                                                       
      int w = 0;
      cmd->add_flag("-w", w);
      // [3] No effect: cmd->allow_extras();                                                                                                                                                               
      // [4] No effect: cmd->prefix_command();                                                                                                                                                             
    
      try {
        std::vector<std::string> rest = app.parse(argc, argv);
        for (auto x : rest)
          std::cout << "REST: " << x << "\n";
        std::cout << "v: " << v << "\n";
        std::cout << "w: " << w << "\n";
      } catch (const CLI::ParseError &e) {
        return app.exit(e);
      }
    }
    

    Without modifications, this program will behave as expected:

    $ ./a.out -v cmd -w
    v: 1
    w: 1
    $ ./a.out -v foo cmd -w
    ERROR: ExtrasError: [foo]
    $ ./a.out -v cmd -w foo
    ERROR: ExtrasError: [foo]
    

    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).

    Thanks!

  3. lambdafu commented on Nov 5, 2017

    @lambdafu
    ContributorAuthor

    Possibly related: It is unclear to me how to get the remaining non-parsed arguments from the subcommand callback.

  4. henryiii commented on Nov 5, 2017

    @henryiii
    Collaborator

    I'll try to get back to you later today!

  5. henryiii commented on Nov 6, 2017

    @henryiii
    Collaborator

    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.

  6. henryiii commented on Nov 16, 2017

    @henryiii
    Collaborator

    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).

  7. lambdafu commented on Nov 18, 2017

    @lambdafu
    ContributorAuthor

    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!

  8. lambdafu commented on Nov 19, 2017

    @lambdafu
    ContributorAuthor

    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.

  9. henryiii commented on Nov 19, 2017

    @henryiii
    Collaborator

    I’m planning to revisit inheriting settings, actually. Either to expand it or reduce it, I’m not sure which. But will standardize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions