Search code examples
windowswindbgpykd

Python Clik Module in WinDbg


I've been using click module for some time now and I think it's awesome. However I have some problems using it in a WinDbg python plugin.

I'm using the following script, which works fine in Linux:

import click

@click.group()
def shell():
    pass

@shell.command()
@click.option('--name', help='Your name please')
def hello(name):
    click.echo(name)

if __name__ == "__main__":
    shell()

A successful invocation of a script can be seen below (this is in Linux command line):

# python test.py hello --name=aaa
aaa

An unsuccessful invocation of a script can be seen below (this is in WinDbg plugin):

0:000> !py C:\Users\windbg\test.py hello --name=aaa
Usage: test.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  hello

Any ideas why this happens and why a WinDbg plugin doesn't accept parameters in order for them to be parsed by click correctly.


Solution

  • It is click "feature":

    see click\utils.py:

    if PY2 and WIN and _initial_argv_hash == _hash_py_argv():
        return _get_windows_argv() 
    return sys.argv[1:]
    
    def _get_windows_argv():
        argc = c_int(0)
        argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc)) 
    

    So, click gets args not from sys.args, but from windbg real commandline.

    You can easy fix this:

    if __name__ == "__main__":
        import sys
        shell(args=sys.argv[1:])