创建自定义鱼开关
Create Custom Fish Switches
我正在研究名为 quick(see here) 的 fish shell 函数,我想为其添加一些开关。
例如 quick -l
列出所有路径标签。
Fish documentation好像没有提供这样的信息。
我的问题是,
- 使用 fish 中的开关增强函数功能的最佳方法是什么?
- 是否可以使用新开关扩展现有命令?
- 这是否与 bash/shell 的类似主题相关?
我遇到过一些相关信息,例如“How do I parse command line arguments in bash?”,但还没有找到针对这些问题的明确且具体的答案。请提供任何帮助,我们将不胜感激!
注意:以 > functions alias
为例,在 switch 语句中似乎只使用了 $argv。
您可以使用 getopt
。这很冗长。这是一个仅查找 -h
或 --help
的示例
function foobar
set -l usage "usage: foobar [-h|--help] subcommand [args...]
... more help text ...
"
if test (count $argv) -eq 0
echo $usage
return 2
end
# option handling, let's use getopt(1), and assume "new" getopt (-T)
set -l tmp (getopt -o h --long help -- $argv)
or begin
echo $usage
return 2
end
set -l opts
eval set opts $tmp
for opt in $opts
switch $opt
case -h --help
echo $usage
return 0
case --
break
case '-*'
echo "unknown option: $opt"
echo $usage
return 1
end
end
switch $argv[1]
case subcommand_a
... etc
我正在研究名为 quick(see here) 的 fish shell 函数,我想为其添加一些开关。
例如 quick -l
列出所有路径标签。
Fish documentation好像没有提供这样的信息。
我的问题是,
- 使用 fish 中的开关增强函数功能的最佳方法是什么?
- 是否可以使用新开关扩展现有命令?
- 这是否与 bash/shell 的类似主题相关?
我遇到过一些相关信息,例如“How do I parse command line arguments in bash?”,但还没有找到针对这些问题的明确且具体的答案。请提供任何帮助,我们将不胜感激!
注意:以 > functions alias
为例,在 switch 语句中似乎只使用了 $argv。
您可以使用 getopt
。这很冗长。这是一个仅查找 -h
或 --help
function foobar
set -l usage "usage: foobar [-h|--help] subcommand [args...]
... more help text ...
"
if test (count $argv) -eq 0
echo $usage
return 2
end
# option handling, let's use getopt(1), and assume "new" getopt (-T)
set -l tmp (getopt -o h --long help -- $argv)
or begin
echo $usage
return 2
end
set -l opts
eval set opts $tmp
for opt in $opts
switch $opt
case -h --help
echo $usage
return 0
case --
break
case '-*'
echo "unknown option: $opt"
echo $usage
return 1
end
end
switch $argv[1]
case subcommand_a
... etc