how to debug python click cli application? how to debug python click cli application? python python

how to debug python click cli application?


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:

  • console_script.exe
  • console_script-script.py

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.