HelpFormatter in Click
Asked Answered
E

1

6

I am using click within a local module and I would like to adjust how the help is displayed:

Currently output with --help:

Usage: __main__.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  foo     Foo is a program very nice and pretty...

By default the prog name is __main__.py and the text is trimmed to 78 chars.

I discovered that this can be adjusted using the HelpFormatter class. But I don't know how to use it in this context.

Current Code:

import click

@click.group()
def main(ctx):
   pass

@main.command()
def foo():
   pass

click.CommandCollection(sources=[main])()

Expected output:

Usage: my_module_name [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  foo     Foo is a program very nice and pretty and this sentence is very long.
Ensanguine answered 23/5, 2017 at 7:8 Comment(2)
Can you give an example of what you are after, both code and what you are hoping the help will look like?Educate
I added the expected outputEnsanguine
E
5

If you are trying to to avoid the truncation of the help string, this can be accomplished via the short_help parameter. short_help is generally derived from help but truncated. If passed explicitly, the entire string will be displayed.

To display the string my_module_name, that can be passed under the parameter prog_name

Test Code:

import click

@click.group()
def main(ctx):
   pass

@main.command(short_help='Foo is a program very nice and pretty and '
                         'this sentence is very long.')
def foo():
   pass

main(['--help'], prog_name='my_module_name')

Results of short_help:

Usage: my_module_name [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  foo  Foo is a program very nice and pretty and this sentence is very long.
Educate answered 24/5, 2017 at 16:1 Comment(5)
What about my ´__main__.py' ?Ensanguine
Do you get that behavior with the test code I just posted? Because I do not see that.Educate
As long as I put your code in the file test.py yes. But if you put it into the file foo/__main__.py and you do PYTHONPATH=. python -m foo --help you will see __main__.py instead of test.py...Ensanguine
Oh, __main__ is the actual name of your file? So in that case __main__.py is the name of your module. So you want to be able to specify an arbitrary string there?Educate
Yes exactly, I would like to specify an arbitrary string, or better, the name of my module.Ensanguine

© 2022 - 2024 — McMap. All rights reserved.