【问题标题】:How to parse empty variable to python input options如何将空变量解析为python输入选项
【发布时间】:2022-09-29 20:35:32
【问题描述】:

我在 python 脚本中使用 click 并尝试在 shell 中运行它。完整的命令如下所示。

python3 test.py --include_dirs ${include_dirs} --excludes ${exclude_models} --calibration_type ${calibration_type} --per_channel ${per_channel} --optional_test ${test1}

由于用户可能不会给出所有选项,因此以这种方式编写脚本会导致问题。例如,如果include_dirs 变量未设置,则\'--excludes\' 将是--include_dirs 选项的输入。

所以我想知道是否有一种优雅的方法来解决它。我可以为这些选项提供空输入吗?

    标签: python shell


    【解决方案1】:

    在 shell 脚本中编写命令时,应始终在每个元素周围加上双引号。这样,您就可以确定如果值中有空格,或者值是空的,那么无论如何都可以正常工作。

    所以你要:

    python3 test.py --include_dirs "${include_dirs}" --excludes "${exclude_models}" --calibration_type "${calibration_type}" --per_channel "${per_channel}" --optional_test "${test1}"
    

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      相关资源
      最近更新 更多