Skip to content

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

Open

Description

@lemon24

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.

Activity

  1. changed the title [-]Chained command with argument and option results in "Error: Missing command.[/-] [+]Chained command with argument and option results in "Error: Missing command."[/+] on Mar 31, 2019
  2. changed the title [-]Chained command with argument and option results in "Error: Missing command."[/-] [+]Chained command with argument and option results in "No such command"[/+] on Mar 31, 2019
  3. lemon24 commented on Mar 31, 2019

    @lemon24
    Author

    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.

  4. jcrotts commented on May 6, 2019

    @jcrotts
    Contributor

    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.

  5. added
    docsUpdates to documentation, readme, docstrings, typos
    on May 6, 2019
  6. jcrotts commented on May 28, 2019

    @jcrotts
    Contributor
  7. added this to the 9.0.0 milestone on Aug 13, 2020
  8. removed
    docsUpdates to documentation, readme, docstrings, typos
    on Aug 14, 2020
  9. removed this from the 8.1.0 milestone on Feb 22, 2022
  10. added
    parsingParsing, parameters, commands, chaining, context
    and removed on Aug 8, 2026
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

    parsingParsing, parameters, commands, chaining, context

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions