Here is my simple code:
import click
def print_help(ctx, value):
if value is False:
return
click.echo(ctx.get_help())
ctx.exit()
@click.command()
@click.option('--dry_run', is_flag=True, help="Enable the dry run.")
@click.option('--prepare', is_flag=True, help="Prepare/recover backups.")
@click.option('--help',
is_flag=True,
expose_value=False,
is_eager=False,
callback=print_help,
help="Print help message")
@click.pass_context
def all_procedure(ctx, dry_run, prepare):
if (dry_run is False) and (prepare is False):
print("No options passed!")
print_help(ctx, value=True)
if __name__ == "__main__":
all_procedure()
And it works.
Running without option:
$ python3 test_py.py
/home/shako/.local/lib/python3.5/site-packages/click/core.py:880: Warning: Invoked legacy parameter callback "<function print_help at 0x7f2482813048>". The new signature for such callbacks starting with click 2.0 is (ctx, param, value).
value, args = param.handle_parse_result(ctx, opts, args)
No options passed!
Usage: test_py.py [OPTIONS]
Options:
--dry_run Enable the dry run.
--prepare Prepare/recover backups.
--help Print help message
Running with --help:
$ python3 test_py.py --help
/home/shako/.local/lib/python3.5/site-packages/click/core.py:880: Warning: Invoked legacy parameter callback "<function print_help at 0x7f6df0bfa048>". The new signature for such callbacks starting with click 2.0 is (ctx, param, value).
value, args = param.handle_parse_result(ctx, opts, args)
Usage: test_py.py [OPTIONS]
Options:
--dry_run Enable the dry run.
--prepare Prepare/recover backups.
--help Print help message
Running with an option:
$ python3 test_py.py --dry_run
/home/shako/.local/lib/python3.5/site-packages/click/core.py:880: Warning: Invoked legacy parameter callback "<function print_help at 0x7f29c0e2a048>". The new signature for such callbacks starting with click 2.0 is (ctx, param, value).
value, args = param.handle_parse_result(ctx, opts, args)
The only remaining thing here is to overcome this Warning.
Any suggestion here? :)
To be clear.
The param should be passed from function.
The param type is:
param -> <click.core.Option object at 0x7fa40bbc61d0>
For now, done by passing None as param:
import click
def print_help(ctx, param, value):
if value is False:
return
click.echo(ctx.get_help())
ctx.exit()
@click.command()
@click.option('--dry_run', is_flag=True, help="Enable the dry run.")
@click.option('--prepare', is_flag=True, help="Prepare/recover backups.")
@click.option('--help',
is_flag=True,
expose_value=False,
is_eager=False,
callback=print_help,
help="Print help message")
@click.pass_context
def all_procedure(ctx, dry_run, prepare):
if (dry_run is False) and (prepare is False):
print_help(ctx, None, value=True)
if __name__ == "__main__":
all_procedure()
shako@shako-localhost:~$ python3 test_py.py --help
Usage: test_py.py [OPTIONS]
Options:
--dry_run Enable the dry run.
--prepare Prepare/recover backups.
--help Print help message
shako@shako-localhost:~$ python3 test_py.py
Usage: test_py.py [OPTIONS]
Options:
--dry_run Enable the dry run.
--prepare Prepare/recover backups.
--help Print help message
Decision made to keep current "see --help" message in #502.
Most helpful comment
For now, done by passing None as param: