Python WinDbg 中的点击模块

Python Clik Module in WinDbg

我使用 click 模块已有一段时间了,我认为它很棒。但是我在 WinDbg python 插件中使用它时遇到了一些问题。

我正在使用以下脚本,它在 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()

成功调用脚本如下所示(在 Linux 命令行中):

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

脚本调用失败如下所示(在 WinDbg 插件中):

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

知道为什么会发生这种情况以及为什么 WinDbg 插件不接受参数以便通过单击正确解析它们。

点击"feature":

参见 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)) 

所以,click 不是从 sys.args 获取参数,而是从 windbg 真正的命令行获取参数。

您可以轻松解决此问题:

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