question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

can't set context_settings with chain=true

See original GitHub issue

@click.group(chain=True) cannot be combined with @click.command(context_settings=CONTEXT_SETTINGS).


Expected Behavior

I should see Error: No such command "test".

Suppose we name the following script debug, and we invoke using debug test. This works:

#!/usr/bin/env python
import click

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.group(chain=True)
@click.pass_context
@click.command(context_settings=CONTEXT_SETTINGS)
def main(ctx):
    print("hi")
    
if __name__ == '__main__':
    main()

Also works:

#!/usr/bin/env python
import click

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.group()
@click.command(context_settings=CONTEXT_SETTINGS)
def main():
    print("hi")
    
if __name__ == '__main__':
    main()

Actual Behavior

Both, however, fail if we instead do @click.group(chain=True). The error messages are confusing. In case (1): TypeError: 'Context' object is not iterable. For (2): TypeError: Attempted to convert a callback into a command twice.

Environment

  • Python version: 3.7
  • Click version: 7.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
n8peasecommented, Aug 13, 2020

I think you’re getting that error message because you’re decorating your main function with @click.group and @click.command, which AFAIK is not legal?

If I create separate functions for the group and the command it seems to work as expected:

#!/usr/bin/env python
import click

CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
@click.group(chain=True, context_settings=CONTEXT_SETTINGS)
@click.pass_context
def cli(ctx):
    print("cli")

@cli.command()
@click.pass_context
def foo(ctx):
    print("foo")

if __name__ == '__main__':
    cli()

use:

$ python debug.py foo -h
cli
Usage: debug.py foo [OPTIONS]

Options:
  -h, --help  Show this message and exit.
$ python debug.py test
cli
Usage: debug.py [OPTIONS] COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...
Try "debug.py -h" for help.

Error: No such command "test".
1reaction
davidismcommented, Aug 8, 2020

We’re no longer planning to remove chain, so no worries about that at least.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sf::ContextSettings Class Reference (SFML / Learn / 2.5.1 ...
ContextSettings allows to define several advanced settings of the OpenGL context attached to a window.
Read more >
Unit testing Spring Boot service layer bean which uses ...
It is a simple and fast way to unit test a class that uses method validation. First, create a proxy of the class...
Read more >
Monster Component in Java with Spring | grison.me
We could add some spring projects like spring-hateoas to it in order to ... Boot application @Accessors(chain = true) // getters/setters are ...
Read more >
click documentation - Read the Docs
Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary.
Read more >
https://gitee.com/baomidou/mybatis-plus/pulls/97.patch
orElseThrow(() -> ExceptionUtils.mpe("can't find the logicFiled from table {%s}", ... Table table) { + //cannot add table alias for customized tenantId ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found