0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestions. I was confused about arguments and options, and about group arguments/options vs command arguments/options. I thought the group was able to pass options to the command. It is a bit strange, since I am not used to Linux commands to work the way that click is doing: click seems to expect that some options / arguments are given before the command, and some options/arguments after the command. Linux commands are not strict regarding options location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.