Search code examples
pythonpython-3.xpython-click

Problems running click command group


I am having trouble with command groups. I have been following this guide.

#!/usr/bin/env python

import click


@click.group()
@click.option("--template-id", prompt="Template ID", help="The template to use.")
@click.option("--lang", prompt="Langcode", help="The language to use.")
def cli(template_id, lang):
    pass


@cli.command()
@click.argument('template-id')
@click.argument('lang')
def upload_translations(template_id, lang):
    pass


if __name__ == "__main__":
    cli()

Running this causes problems:

» ~/cli.py upload_translations --template-id=xxxxx --lang=ja 
Template ID: sdf
Langcode: asdf
Error: no such option: --template-id
  1. Why is click requesting the options? I am already passing that on the command line!
  2. Why is there an Error: no such option: --template-id?

Solution

  • The --template-id option is not an option to the upload_translations command; it is an option to the base cli. So you would call it like:

    ./cli.py --template-id=xxxxxx --lang=ja upload_translations ...
    

    Also, you have a --lang option both on cli and on upload_translations. Which means this would also be valid:

    ./cli.py --template-id=xxxxxx upload_translations --lang=ja ...
    

    That's a bit confusing; you may want to either remove the --lang option from one or the other, or give it a different name in one of those two commands if it's not actually the same thing.