- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好,我是 python 编程新手,不知道如何使用 getopt。因此,由于我认为 python 是一种非常简单的语言,所以我决定编写自己的 getopt 函数。事情是这样的:
string = "a b -c -d"
list = string.split()
def get_short_opts(args):
opts = ""
for word in args:
print("Word = " + word)
if word[0] == '-' and word[1] != '-':
opts += word[1:] #to remove the '-'
args.remove(word)
print("Opts = " + opts)
print("Args = " + str(args))
return opts
print(get_short_opts(list))
基本上,该函数返回位于“-”字符之后的所有字符。当我一次使用多个选项且仅使用一个“-”并且如果我执行类似
的操作时,它会起作用["-a", "arg", "-b"]
但是当我尝试依次传递多个选项时,它不起作用。上面的主要代码是它不起作用的示例。你能解释一下为什么它只有时有效而有时无效吗?任何帮助,将不胜感激。谢谢!
最佳答案
问题是您在迭代列表时无法从列表中删除。
参见this question ,特别是this answer引用the official Python tutorial :
If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy.
(C++ 人们称之为“迭代器失效”,我不知道它的 Python 术语(如果有的话)。)
迭代 args
的副本并从原始版本中删除:
string = "a b -c -d"
list = string.split()
def get_short_opts(args):
opts = []
for word in args[:]:
print("Word = " + word)
if word[0] == '-' and word[1] != '-':
opts.append(word[1:]) #to remove the '-'
args.remove(word)
print("Opts = " + str(opts))
print("Args = " + str(args))
return opts
print(get_short_opts(list))
args[:]
表示法是从 args
的开头到结尾的切片,换句话说,是整个内容。但切片是副本,而不是原始切片。然后,您可以像之前一样从原始 args
中删除,而不会影响迭代顺序。
另请注意,我已将您的 opts
从字符串更改为列表。这似乎是有道理的。您可以迭代它,计算成员数量等。如果您愿意,您可以将其放回原样(每个选项连接的字符串)。
关于Python 自定义 getopt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34982825/
是否可以将参数传递给通过 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:
我是一名优秀的程序员,十分优秀!