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

Multiple commands problem - use one_of instead of | #60

Open
WojciechMigda opened this issue Dec 20, 2020 · 2 comments
Open

Multiple commands problem - use one_of instead of | #60

WojciechMigda opened this issue Dec 20, 2020 · 2 comments

Comments

@WojciechMigda
Copy link

WojciechMigda commented Dec 20, 2020

Hi,

some time ago I stumbled upon a problem with CLI, which consisted of three or more commands. I have found a work-around and I am posting it here hoping it will help others struggling with the same.

The smallest non-working example is:

    auto cli = (
        command("a").set(m.a) |
        command("b").set(m.b) |
        (
            command("c").set(m.c) & value("i", m.i)
        )
    );

where clipp would parse a, b, but not c 123.

It turned out that I can make clipp do what I want if instead of specifying the alternative of commands with | I'd use clipp::one_of.
The example above becomes:

    auto cli = one_of(
        command("a").set(m.a),
        command("b").set(m.b),
        command("c").set(m.c) & value("i", m.i)
    );

The docs, unless I am missing something, specify that one_of is the same as |, yet they produce different usage lines. The first one gives

((a|b) | (c <i>))

while the second one

(a | b | (c <i>))
@WojciechMigda WojciechMigda changed the title Multiple commands problem - use one_of instead of | Multiple commands problem - use one_of instead of | Dec 20, 2020
@thomthom
Copy link

Ah! I ran into the same issue (Thank you for posting a solution to it).

I had this:

  auto cli = (
    (
      install_mode |
      list_mode |
      fetch_mode |
      remove_mode |
      reset_mode |
      help_mode
    ),

    option("-v", "--version").call(ShowVersion).doc("show version")
  );

I needed to change it to this for things to work as I wanted it to:

  auto cli = (
    one_of(
      install_mode,
      list_mode,
      fetch_mode,
      remove_mode,
      reset_mode,
      help_mode
    ),

    option("-v", "--version").call(ShowVersion).doc("show version")
  );

Is this a documentation issue or an issue related with how the | operator works?

@WojciechMigda
Copy link
Author

AFAIR the implementations differ between the two, and the | operator implementation is not commutative.

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

2 participants