I have built a cli application using the click library in python. There is no documentation on how to debug commands.
Without click, its easy to just debug python files in IDE, but when we use click, the commands need to be run through console_scripts setup in setup.py.
This is not well documented, but you can call your command functions directly, and thus can run the code in a debugger:
Sample Code:
import click
@click.command()
@click.option('--my_arg', default=1, help='a number')
def my_command(my_arg):
click.echo("my_arg='%d'" % my_arg)
if __name__ == '__main__':
my_command(['--my_arg', '3'])
Result:
my_arg='3'
I'm a couple years late to the party, but in case anyone else comes here looking for an answer like I just did:
Calling your function with CliRunner.invoke() will return a "Result" object with an "exc_info" attribute. You can feed that to traceback.print_exception() like so:
runner = CliRunner()
result = runner.invoke(my_command)
traceback.print_exception(*result.exc_info)
The setup.py generates:
from cmdline:
console_app --help
expands out to the IDE configuration cmd:
python <absolute path to>\console_app-script.py --help
Tested in PyCharm 2018.2 - you can set and hit breakpoints and preserve the expected cmdline/arg paradigm.