无法使用 getopt python 处理参数

发布于 2024-11-29 15:36:45 字数 850 浏览 6 评论 0原文

为了给我的 python 脚本提供选项,我想引入一些参数。我发现在 python 中执行此操作的更好方法是使用 getopt,但是一旦我运行我的脚本,它就不会执行任何操作。请帮我!!!。这是我的代码:

def main(argv):
     try:
            opts, args = getopt.getopt(argv, 'hi:o:t', ['help', 'input=', 'output='])
    except getopt.GetoptError:
            usage()
            sys.exit(2)
            file = None
            outfile = None
    for opt, arg in opts:
            if opt in ('-h', '--help'):
                    usage()
                    sys.exit(2)
            elif opt in ('-i', '--input'):
                    file = arg
            elif opt in ('-o', '--output'):
                    outfile = arg
            elif opt == '-t':
                    maininfo(file,outfile)
            else:
                    usage()
                    sys.exit(2)

if __name__ =='__main__':
    main(sys.argv[1:])

In order to offer options to my python script, I want to introduce some parameters. I found that the better way to do this in python is with getopt, but once I run my script it doesn't do anything. Please help me!!!. This is my code:

def main(argv):
     try:
            opts, args = getopt.getopt(argv, 'hi:o:t', ['help', 'input=', 'output='])
    except getopt.GetoptError:
            usage()
            sys.exit(2)
            file = None
            outfile = None
    for opt, arg in opts:
            if opt in ('-h', '--help'):
                    usage()
                    sys.exit(2)
            elif opt in ('-i', '--input'):
                    file = arg
            elif opt in ('-o', '--output'):
                    outfile = arg
            elif opt == '-t':
                    maininfo(file,outfile)
            else:
                    usage()
                    sys.exit(2)

if __name__ =='__main__':
    main(sys.argv[1:])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

长不大的小祸害 2024-12-06 15:36:45

我建议添加更多日志记录。这不仅可以帮助您现在,还可以帮助将来使用您的脚本的任何人。

def main(argv):
    filename = None
    outfile = None
    call_maininfo = False
    try:
        opts, args = getopt.getopt(argv, 'hi:o:t', ['help', 'input=', 'output='])
        if not opts:
            print 'No options supplied'
            usage()
    except getopt.GetoptError, e:
        print e
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit(2)
        elif opt in ('-i', '--input'):
            filename = arg
        elif opt in ('-o', '--output'):
            outfile = arg
        elif opt == '-t':
            call_maininfo = True
        else:
            usage()
            sys.exit(2)

    print 'Processed options [{0}] and found filename [{1}] and outfile [{2}]'.format(
            ', '.join(argv),
            filename,
            outfile,
            )

    if call_maininfo:
        print 'Calling maininfo()'
        maininfo(filename, outfile)

我还将对 maininfo() 的调用移出了循环,因为您可以在文件名之前提供 -t

I suggest adding more logging. Not only will this help you out now, it'll help out whoever uses your script in future.

def main(argv):
    filename = None
    outfile = None
    call_maininfo = False
    try:
        opts, args = getopt.getopt(argv, 'hi:o:t', ['help', 'input=', 'output='])
        if not opts:
            print 'No options supplied'
            usage()
    except getopt.GetoptError, e:
        print e
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit(2)
        elif opt in ('-i', '--input'):
            filename = arg
        elif opt in ('-o', '--output'):
            outfile = arg
        elif opt == '-t':
            call_maininfo = True
        else:
            usage()
            sys.exit(2)

    print 'Processed options [{0}] and found filename [{1}] and outfile [{2}]'.format(
            ', '.join(argv),
            filename,
            outfile,
            )

    if call_maininfo:
        print 'Calling maininfo()'
        maininfo(filename, outfile)

I also moved the call to maininfo() out of the loop as you could supply -t before the filenames!

柠檬色的秋千 2024-12-06 15:36:45

您可以使用 optparse(旧版本,在 python 2.7 后将被弃用)或 argparse (新版本),这是标准的 python 模块解析参数。

希望它首先对此有所帮助

You can use optparse(old version, will be deprecated after python 2.7) or argparse ( a new version), which are standard python module parsing arguments.

Hope it helps this first

絕版丫頭 2024-12-06 15:36:45

请参阅此答案: https://stackoverflow.com/a/1540399/2542738

基本上,您需要删除 'python' 来自 opts,因为它是列表 opts 的第一个元素: opts.pop(0) 然后你应该没问题了。

Please see this answer: https://stackoverflow.com/a/1540399/2542738

Basically, you need to remove 'python' from opts as it is the first element of the list opts: opts.pop(0) and then you should be fine.

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