- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个简单的 bash 脚本,它采用一个可选参数 (-t),后跟一些附加参数。
我认为 getopts 是实现此目标的合理方法,但我很难获得所需的行为。我有以下内容:
foo() {
baz=10
while getopts ":t:" option; do
case "$option" in
t) baz=$OPTARG ;;
esac
done
shift $((OPTIND - 1))
bar -t $baz $1 $2 $3
}
问题是 $OPTIND
似乎不会根据参数是否存在而变化,所以我没有像预期的那样使用可选参数获得正确的行为(即,我可以'无论参数是否存在,都不会转移到正确数量的参数。
我希望以下两项都能正确执行:
foo a b c
foo -t 5 a b c
实现此目标的最简单方法是什么?我更喜欢不是 hack 的解决方案,因为我可能想使用额外的可选参数。
最佳答案
问题是您永远不会重置 $OPTIND
,因此每次您调用 foo
时,它都会从最后一次处理的选项索引之后开始检查其参数。例如:
# $OPTIND is now 1
foo -t opt arg1 arg2 arg3
# the above recognizes -t opt as an option, and sets $OPTIND to 3
# $OPTIND is now 3
foo arg4 arg5 arg6
# the above recognizes that arg6 is not an option, so it leaves $OPTIND at 3
解决方案是在 foo
中本地化 $OPTIND
,明确地将其设置为 1
:
foo() {
baz=10
local OPTIND=1
while getopts ":t:" option; do
case "$option" in
t) baz=$OPTARG ;;
esac
done
shift $((OPTIND - 1))
bar -t $baz $1 $2 $3
}
(您可能还想本地化 $baz
,同时进行本地化。)
关于bash - 使用 getopts 读取一个可选参数并移动正确数量的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14656181/
是否可以将参数传递给通过 getopt::long 调用的子例程?例如,当用户在命令行上指定 script.pl -pandora argument 时,我有此代码调用 &Salt GetOption
我正在尝试将 getopts 用于 bash 脚本。这个脚本可以有标志,所有这些标志都是强制性的,需要包含一个值。当本应包含值的强制标志之一为空时,getopts 使用下一行标志作为其内容。我该如何防
我公司使用 Getopt::Declare 作为其命令行选项解析器。我们的期权处理 block 的结构通常如下所示: Readonly my $ARGS => Getopt::Declare->new
我有一个带有可能的命令行参数的字符串(使用 Read-Eval-Print-Loop 程序),我希望它在传递给 Getopt::Long 时被解析为类似于命令行参数。 详细说明: 我有一个字符串 $s
我刚刚在代码审查中第一次被要求检查对 GetOptions() 的调用的返回码。 Getopt::Long 的功能Perl 模块。 我不记得曾经看到过这样的 GetOptions() 测试。功能。 那
我有这个 getopt: GetOptions( GetOptions ("library=s" => \@libfiles); @libfiles = split(/,/,join(','
我想使用 Getopt::Long::GetOptions 获取脚本的命令行选项。 我有这样的需求: perl script.pl -c -c -m argument 这里我们有选项标志 -c
我正在尝试使用 getopt() 解析命令行参数。下面是我的代码。无论我在运行程序时传递什么参数,getopt() 总是返回 -1。 例如: $ gcc -o test test.c $ ./test
在我的 python 脚本中使用 getopt.getopt() 函数时,临时返回值保持为空。我缺少什么。 def ParseOpts(cmdName): shortForm = 'c:n:'
我想使用 getopt,但它行不通。 它给了我 gcc -g -Wall -std=c99 -ftrapv -O2 -Werror -Wshadow -Wundef -save-temps -Werr
有人可以帮助我使用 getopt 函数吗? 当我在 main 中执行以下操作时: char *argv1[] = {"testexec","-?"}; char *argv2[] = {"testex
我有一个脚本,它从 CLI 获取 3 个输入变量并将其分别插入到 3 个变量: GetOptions("old_path=s" => \$old_path, "var=s" =
如何以这种方式接受命令行参数: ./a.out --printall 所以在我的程序中,我有类似的东西 if (printall) { // do something } 我不想这样做: if (
在 Perl getopts 中,是否可以多次使用相同的选项但具有不同的值?我想为用户提供输入不同网格坐标的选项,但使用相同的选项名称以尽量减少混淆。 例如: my_grid.pl --coords=
是否有用于Groovy的标准或推荐的getopts库,该库可以让我快速处理Groovy中的长期和短期命令行争论? groovy foo.groovy --fname = foo.txt --outpu
use Getopt::Long::Configure(pass_through); # .... GetOptions( "display=s" => \$display,
我正在运行 bash 4.2 版,我正在尝试使用内置命令 getopts 解析命令行参数, 但是getopts好像没有正确解析,如果-s不是第一个参数,就不会被解析 -s 未解析: %> ./geto
GetOptions( "r|repo=s" => \$repo, "R|list-repos" => \$list, ); 当我用 -r qwe 调用这个脚本
我正在尝试使用 getopt() 从 PHP7 cli 获取选项,但是在调用 php myprocess.php task -d -o 时我得到一个空数组。不知道我错过了什么,希望你能帮助我。 这就是
我正在寻找一种方法来处理包含必须解析的空格的参数 通过 shell getopts 命令。 while getopts ":a:i:o:e:v:u:" arg do echo "ARG is:
我是一名优秀的程序员,十分优秀!