Parsing Fish函数选项/param解析器

Parsing Fish函数选项/param解析器,parsing,fish,Parsing,Fish,似乎有一些工作正在进行中,以在未来增加对这方面的支持: 但同时,我们建议用什么方法来解决这个问题呢?我应该只解析$argv吗?如果是这样的话,你有一些建议/最佳实践吗?我肯定这是一个最佳实践,但与此同时,你可以这样做: function options echo $argv | sed 's|--*|\\'\n'|g' | grep -v '^$' end function function_with_options for i in (options $argv) ech

似乎有一些工作正在进行中,以在未来增加对这方面的支持:



但同时,我们建议用什么方法来解决这个问题呢?我应该只解析$argv吗?如果是这样的话,你有一些建议/最佳实践吗?

我肯定这是一个最佳实践,但与此同时,你可以这样做:

function options
  echo $argv | sed 's|--*|\\'\n'|g' | grep -v '^$'
end

function function_with_options
  for i in (options $argv)
    echo $i | read -l option value

    switch $option
      case a all
        echo all the things
      case f force
        echo force it
      case i ignore
        echo ignore the $value
    end
  end
end
输出:

➤ function_with_options -i thing -a --force
ignore the thing
all the things
force it
您可以这样做:

for item in $argv
    switch "$item"
        case -f --foo
        case -b --bar
    end
end
上面不支持在单个参数
-fbz
、选项值
--foo=baz
--foo-baz
f baz
、负赋值
--name=值
、选项结尾和破折号-和-始终是参数的一部分

为了解决这些问题,我编写了一个函数

getopts -ab1 --foo=bar baz
现在看看输出

a
b    1
foo  bar
_    baz
左侧的项目表示与CLI关联的选项标志或键。右边的项目是选项值。下划线
字符是无键参数的默认键

用于处理生成的流并匹配模式:

getopts -ab1 --foo=bar baz | while read -l key option
    switch $key
        case _
        case a
        case b
        case foo
    end
end

请参阅。

解析
$argv
对于基本场景很好,但在其他情况下可能会变得单调乏味且容易出错

在fish拥有自己的参数解析解决方案之前,社区创建了:

从fish 2.7.0开始,您可以使用fish的内置选项解析器:


要查看可能的全部内容,请运行
argparse-h
argparse--help

这是否仍然是fish的最新技术?是的,
fish
中没有任何内容可以处理这一问题。当然,您也可以像fish中的一些内部函数一样使用
getopt
。但是您必须处理不同版本的
getopt
等。有关getopt示例,请参阅fish函数
psub
umask
trap
。感谢您的回复。。。这对我真的很有用。
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