click icon indicating copy to clipboard operation
click copied to clipboard

Chained command with argument and option results in "No such command"

Open lemon24 opened this issue 6 years ago • 3 comments

Chained command with argument and option results in "No such command"; according to the documentation,

Other than that [nargs=-1 being allowed only on the last chained command] there are no restrictions on how they [chained commands] work. They can accept options and arguments as normal.

Reproduced on Ubuntu 18.04 with Python 3.5, 3.6 and 3.7, for all tagged Click versions between 3.0 and 7.0 (chain= does not seem to be supported for the ones before that).

Minimal repro (removing the argument allows the option to be recognized):

import click

@click.group(chain=True, no_args_is_help=False)
def cli():
    pass

@cli.command()
@click.argument('argument')
@click.option('--option')
def command(argument, option):
    pass

if __name__ == '__main__':
    cli()

Results in:

$ python3 test.py command a --option x
Usage: test.py [OPTIONS] COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...
Try "test.py --help" for help.

Error: No such command "--option".
$ python3 test.py command --help 
Usage: test.py command [OPTIONS] ARGUMENT

Options:
  --option TEXT
  --help         Show this message and exit.

lemon24 avatar Mar 31 '19 17:03 lemon24

OK, looking at the OptionParser instances used in the example above, I saw that they have allow_interspersed_args set to False (used to "to implement nested subcommands safely", which makes sense).

This, however, leads to inconsistent behavior:

  • non-chained commands allow both the --option argument and the argument --option orders
  • chained commands only allows --option argument

While the help text clearly states that commands should be called like [OPTIONS] ARGUMENT, the fact that both orders work in most of the cases may be confusing to users (it was for me, at least).

If I understand correctly, it's unlikely argument --option will ever be possible for chained commands. Should I update the Multi Command Chaining section to reflect the behavior described above? Why not Argparse? implies it, but that's not the first place someone would look when encountering it.

lemon24 avatar Mar 31 '19 19:03 lemon24

Seems reasonable to add a line in the docs to say that you have to use argument followed by options. Although I recall this being mentioned several places in the docs.

jcrotts avatar May 06 '19 20:05 jcrotts

https://github.com/pallets/click/issues/1323

jcrotts avatar May 28 '19 00:05 jcrotts