【问题标题】:Python: Why is my Click CLI not executing the desired functions?Python:为什么我的 Click CLI 没有执行所需的功能?
【发布时间】:2017-03-16 17:16:40
【问题描述】:

我有一个带有点击装饰器的功能。该函数的全部目的是为 tq_oex_interchange 类所做的工作添加一个 CLI 选项。

@click.command()
@click.option('-input_file', help='The input file to process')
@click.option('-output_file', help='The output file to create')
@click.option('-part_num_col', help='The column containing the part numbers')
@click.option('-summate_cols', help='The columns to summate')
def run(input_file, output_file, part_num_col, summate_cols):
    from re import split
    summate_cols = [int(i) for i in split(',', summate_cols)]
    part_num_col = int(part_num_col)
    teek = tq_oex_interchange()
    click.echo("Working...")
    teek.scan_file(input_file, output_file, part_num_col, summate_cols)

但是,当我使用命令执行我的脚本时

python tq_oex.py -input_file C:\Users\barnej78\Desktop\test_0.csv -output_file C:\Users\barnej78\Desktop\cmd.csv -part_num_col 3 -summate_cols 5,6

什么都没有发生,甚至点击回声都没有执行。

另外,

python tq_oex.py --help

也不做任何事情。

这些命令都没有错误或异常输出。

我做错了什么?

【问题讨论】:

  • 这是整个脚本吗?

标签: python python-3.x python-click


【解决方案1】:

您是否能够从此处成功运行示例代码?

http://click.pocoo.org/5/

我会从这个开始并确保它有效。然后使用 Click 测试文档为它编写一个测试:

http://click.pocoo.org/5/testing/

这样,当你开始调整它时,你可以运行测试看看有什么问题......

对于 Click 应用程序,我通常从一个简单的参数开始,然后添加另一个参数以确保它仍然有效:

@click.command()
@click.argument('input_file')
def run_files(input_file):
    click.echo(input_file)

然后添加一个选项:

@click.command()
@click.argument('input_file')
@click.option('--output_file', help='The output file to create')
def run_files(input_file, output_file):
    click.echo(input_file, output_file)

出于调试目的,我也喜欢设置默认值:

def run_files(input_file='/path/to/input_file',
              output_file='/path/to/output_file'):
    click.echo(input_file, output_file)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-17
    • 2013-09-08
    • 2022-01-20
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    相关资源
    最近更新 更多