似乎正在进行一些工作以在 future 增加对此的支持:
https://github.com/fish-shell/fish-shell/issues/478
https://github.com/xiaq/fish-shell/tree/opt-parse
但与此同时,处理这个问题的推荐方法是什么?我应该解析 $argv 吗?如果是这样,您有一些提示/最佳实践吗?
最佳答案
解析 $argv
适用于基本场景,但否则会变得乏味且容易出错。
在fish有自己的参数解析解决方案之前,社区创建了:
从fish 2.7.0 开始,您可以使用fish 的内置选项解析器:argparse
function foo --description "Example argparse usage"
set --local options 'h/help' 'n/count=!_validate_int --min 1'
argparse $options -- $argv
if set --query _flag_help
printf "Usage: foo [OPTIONS]\n\n"
printf "Options:\n"
printf " -h/--help Prints help and exits\n"
printf " -n/--count=NUM Count (minimum 1, default 10)"
return 0
end
set --query _flag_count; or set --local _flag_count 10
for i in (seq $_flag_count); echo foo; end
end
要查看可能的全部内容,请运行 argparse -h
或 argparse --help
.
关于parsing - Fish 函数选项/参数解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16048757/