python脚本简短选项文件名参数不被识别为参数
我有一个使用SYS和GetOPT模块进行两个参数的脚本。 我遇到的问题是,当使用shortopts测试该论点是否正确传递时,文件名参数的简短选项不起作用。使用长选项正常。
以下是从argv中解析参数的函数:
下面的函数
def parseArgs(argv):
arg_url = ""
arg_filename = ""
arg_help = "{0} -u <url> -f <screenshot file name> ".format(argv[0])
try:
(opts, args) = getopt.getopt(sys.argv[1:], 'hu:f', ["help", "url=", "filename="])
except getopt.GetoptError as err:
print("\nInvalid command\n")
print("Help:\n" + arg_help + "\n")
print("\n" + str(err) + "\n")
sys.exit(2)
print("opts print: " + str(opts))
if len(opts) != 0:
for (opt, arg) in opts:
if opt in ("-u", "--url"):
arg_url = arg
elif opt in ("-f", "--filename"):
arg_filename = arg
else:
print(arg_help) # print the help message
sys.exit(2)
else:
print(arg_help)
sys.exit(2)
print('url:', arg_url)
print('fileName:', arg_filename)
return arg_url, arg_filename
是终端输出。您可以看到文件名的简短选项不会传递有效值。
下面的输出
user@562539635f45:/test# python3 imgkit-test.py -u urltest -f test
opts print: [('-u', 'urltest'), ('-f', '')]
url: urltest
fileName:
function output:('urltest', '')
url output: urltest
imageName output:
是使用长选项的输出:
输出
user@562539635f45:/test# python3 imgkit-test.py -u urltest --filename test
opts print: [('-u', 'urltest'), ('--filename', 'test')]
url: urltest
fileName: test
function output:('urltest', 'test')
url output: urltest
imageName output: test
我可能会导致问题。我没有看到任何明显的东西。任何帮助将不胜感激。
I have a script that takes two arguments using sys and getopt module.
The issue I am having is that when using shortopts to test that the arguments are being passed correctly, the short option for the filename argument does not work. Using long option works fine.
Below is the function parsing the arguments from argv:
FUNCTION
def parseArgs(argv):
arg_url = ""
arg_filename = ""
arg_help = "{0} -u <url> -f <screenshot file name> ".format(argv[0])
try:
(opts, args) = getopt.getopt(sys.argv[1:], 'hu:f', ["help", "url=", "filename="])
except getopt.GetoptError as err:
print("\nInvalid command\n")
print("Help:\n" + arg_help + "\n")
print("\n" + str(err) + "\n")
sys.exit(2)
print("opts print: " + str(opts))
if len(opts) != 0:
for (opt, arg) in opts:
if opt in ("-u", "--url"):
arg_url = arg
elif opt in ("-f", "--filename"):
arg_filename = arg
else:
print(arg_help) # print the help message
sys.exit(2)
else:
print(arg_help)
sys.exit(2)
print('url:', arg_url)
print('fileName:', arg_filename)
return arg_url, arg_filename
Below is the terminal output. You can see that the short option for filename doesn't pass a valid value.
OUTPUT
user@562539635f45:/test# python3 imgkit-test.py -u urltest -f test
opts print: [('-u', 'urltest'), ('-f', '')]
url: urltest
fileName:
function output:('urltest', '')
url output: urltest
imageName output:
Below is the output for using the long option:
OUTPUT
user@562539635f45:/test# python3 imgkit-test.py -u urltest --filename test
opts print: [('-u', 'urltest'), ('--filename', 'test')]
url: urltest
fileName: test
function output:('urltest', 'test')
url output: urltest
imageName output: test
I'm stumped as what could be causing the issue. I'm not seeing anything obvious. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是我在'hu:f'上错过了一个额外的结肠。感谢@slothrop评论。
粘贴的正确答案:
文档参考: https://docs.python.org3/ library/getopt.html
The answer to this was that I was missing an extra colon at 'hu:f'. Thanks to @slothrop comment.
Correct answer pasted below:
Documentation reference: https://docs.python.org/3/library/getopt.html