Bash 别名因 python 脚本和 argparse 的默认参数而失败?

Bash alias failing with default arguments for python script and argparse?

我有以下问题:

我有一个 python 脚本的多个 bash 别名: python 脚本使用带有两个参数的 arg-parse,一个是可选的。

testing.py -i value1
testing.py -i value1 -e value2

alias test1=' ./testing.py -i ' 

这按预期工作

alias test2=' ./testing.py -i  -e '

这行不通!

最后,想从命令行执行此操作:

test2 value1 value2

我搜索了 bash 个函数,不确定它是否能解决我的问题。 我已经尝试了很多方法来逃脱,qoute,绕过它,重新排列......等等......没有骰子

能够将 bash </code> 中的第二个默认参数传递给 python 脚本的 <code>-e argparse 选项将非常有帮助。

任何帮助将不胜感激..

此致,

谢谢!

别名是简单的前缀扩展。他们不知道他们的论点是什么,也不会 运行 条件逻辑。

test1() { ./testing.py -i ""; }

test2() { ./testing.py -i "" -e ""; }

...或者,更好的是,处理这两种情况的单个函数:

testfunc() { ./testing.py ${1+ -i ""} ${2+ -e ""}; }

不能在别名中使用变量

(或者更准确地说,你可以,但不是你想要的)

test1 value1

翻译成

./testing.py -i value1

但是

test2 value1 value2

翻译成

./testing.py -i -e value1 value2

因为 </code> 和 <code> 是空字符串(很可能)。

使用函数

简单的解决方案是创建 bash 函数

test2() {
    ./testing.py -i "" -e ""
}