python脚本简短选项文件名参数不被识别为参数

发布于 2025-02-13 04:17:38 字数 1695 浏览 0 评论 0原文

我有一个使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云 2025-02-20 04:17:38

答案是我在'hu:f'上错过了一个额外的结肠。感谢@slothrop评论。

粘贴的正确答案:

(opts, args) = getopt.getopt(sys.argv[1:], 'hu:f:', ["help", "url=", "filename="])

文档参考: https://docs.python.org3/ library/getopt.html

shortopts是脚本想要的一系列选项字母
认识到,有需要争论的选择,然后是结肠
(':'; ie,与unix getopt()使用)的格式。

The answer to this was that I was missing an extra colon at 'hu:f'. Thanks to @slothrop comment.

Correct answer pasted below:

(opts, args) = getopt.getopt(sys.argv[1:], 'hu:f:', ["help", "url=", "filename="])

Documentation reference: https://docs.python.org/3/library/getopt.html

shortopts is the string of option letters that the script wants to
recognize, with options that require an argument followed by a colon
(':'; i.e., the same format that Unix getopt() uses).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文