简体   繁体   English

鱼 Shell argparse 做某事非常意外

[英]Fish Shell argparse Doing Something Very Unexpected

I'm running fish 3.1.0 on OS X 10.14.5.我在 OS X 10.14.5 上运行 fish 3.1.0。

I am new to fish and to argparse.我是钓鱼和 argparse 的新手。 I like them both.我喜欢他们两个。

The following simple test code:下面是简单的测试代码:

#!/usr/local/bin/fish

function afoo 
    set -l options (fish_opt -s h -l help)
    set options $options (fish_opt -s m -l max --required-val)
    set options $options (fish_opt -s n -l nnn --optional-val)

    argparse $options -- $argv

    echo "max" $_flag_max
    echo "nnn" $_flag_nnn
end

Should create two switches m and n.应该创建两个开关 m 和 n。 m must take a value while n can take a value. m必须取值,而 n可以取值。

Here is a simple screen grab when testing that rudimentary function in Terminal's CLI:这是在终端 CLI 中测试基本 function 时的简单屏幕截图:

[/usr/local/bin/fish 3.1.0] ~ afoo -m foo -n baz
max foo
nnn
[/usr/local/bin/fish 3.1.0] ~ afoo -mfoo -nbaz
max foo
nnn baz
[/usr/local/bin/fish 3.1.0] ~ 

Why does argparse not parse the space between the -n switch and its parameter but it does parse the space between the -m switch and its parameter?为什么 argparse解析 -n 开关及其参数之间的空格,但解析 -m 开关及其参数之间的空格?

This is driving me nuts.这让我发疯了。 I've read the documentation a dozen times.我已经阅读了十几遍文档。 What am I doing wrong?我究竟做错了什么?

PS The order of the switches doesn't matter. PS开关的顺序无关紧要。

Why does argparse not parse the space between the -n switch and its parameter but it does parse the space between the -m switch and its parameter?为什么 argparse 不解析 -n 开关及其参数之间的空格,但它解析 -m 开关及其参数之间的空格?

The -n takes an optional parameter. -n采用可选参数。 Those have to, by getopt convention, be directly attached to the option argument.根据 getopt 约定,这些必须直接附加到选项参数。

Look at eg看看例如

echo never | grep --color never

This won't disable color, instead it will set color to auto (the default no-argument value) and look for the string "never".这不会禁用颜色,而是将颜色设置为自动(默认无参数值)并查找字符串“never”。 So it will show you "never" in red.所以它会用红色显示“从不”。 Instead you'd have to use --color=never (or if it had a short option "-c" then "-cnever" would also be acceptable).相反,您必须使用--color=never (或者如果它有一个简短的选项“-c”,那么“-cnever”也可以接受)。

Argparse acts like the usual unix getopt(3) option parsing, which it also uses under the covers, because that's how the rest of the system acts. Argparse 的行为类似于通常的 unix getopt(3) 选项解析,它也在幕后使用,因为这就是系统的 rest 的行为方式。

This is because n/nnn=?这是因为n/nnn=? has an optional parameter, whereas m/max= has a required one.有一个可选参数,而m/max=有一个必需参数。 Those strings are the output of your calls to fish_opt .这些字符串是您调用 fish_opt 的fish_opt

Non-option arguments are stored in $argv after a successful run of argparse .成功运行argparse后,非选项 arguments 存储在$argv中。 So your baz ends up in $argv .所以你的baz最终会出现在$argv中。

Excellent, Thanks so very.太好了,非常感谢。 very much.非常。

If you're a newbie like me you can find more detail about what these answers point to by searching stack overflow for 'getopt space optional argument'.如果您是像我这样的新手,您可以通过在堆栈溢出中搜索“getopt space optional argument”来找到有关这些答案指向的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM