【问题标题】:Optional command line arguments可选的命令行参数
【发布时间】:2017-04-04 06:57:55
【问题描述】:

给定这样的代码,我该如何在运行选项中实际设置文件?

我正在使用 Spyder 并将 -h -s -p -o 作为参数,但我不确定如何为 -o 选项指定命名文件。

class CommandLine:
    def __init__(self):
        opts, args = getopt.getopt(sys.argv[1:],'hspw:o:')
        opts = dict(opts)

        if '-o' in opts:
            self.outfile = opts['-o']
        else:
            self.outfile = None

【问题讨论】:

标签: python options spyder getopt


【解决方案1】:

这是一个处理argpase的简单教程。

但首先,如果您想在使用 argparse 模块时获得更多控制权,我建议您阅读official documentation

另外,如果你想向 Spyder 传递参数,我会推荐@Carlos Cordoba 的答案,他建议看这个answer

我的教程脚本:

import argparse

class CommandLine:
    def __init__(self):
        parser = argparse.ArgumentParser(description = "Description for my parser")
        parser.add_argument("-H", "--Help", help = "Example: Help argument", required = False, default = "")
        parser.add_argument("-s", "--save", help = "Example: Save argument", required = False, default = "")
        parser.add_argument("-p", "--print", help = "Example: Print argument", required = False, default = "")
        parser.add_argument("-o", "--output", help = "Example: Output argument", required = False, default = "")
        
        argument = parser.parse_args()
        status = False
        
        if argument.Help:
            print("You have used '-H' or '--Help' with argument: {0}".format(argument.Help))
            status = True
        if argument.save:
            print("You have used '-s' or '--save' with argument: {0}".format(argument.save))
            status = True
        if argument.print:
            print("You have used '-p' or '--print' with argument: {0}".format(argument.print))
            status = True
        if argument.output:
            print("You have used '-o' or '--output' with argument: {0}".format(argument.output))
            status = True
        if not status:
            print("Maybe you want to use -H or -s or -p or -o as arguments ?") 


if __name__ == '__main__':
    app = CommandLine()

现在,在您的终端中或使用Spyder

$ python3 my_script.py -H Help -s Save -p Print -o Output

输出:

You have used '-H' or '--Help' with argument: Help
You have used '-s' or '--save' with argument: Save
You have used '-p' or '--print' with argument: Print
You have used '-o' or '--output' with argument: Output

当您使用 -h--help 作为参数时,您将获得以下输出:

$ python3 my_script.py -h

输出:

usage: my_script.py [-h] [-H HELP] [-s SAVE] [-p PRINT] [-o OUTPUT]

Description for my parser

optional arguments:
  -h, --help            show this help message and exit
  -H HELP, --Help HELP  Example: Help argument
  -s SAVE, --save SAVE  Example: Save argument
  -p PRINT, --print PRINT
                        Example: Print argument
  -o OUTPUT, --output OUTPUT
                        Example: Output argument

【讨论】:

    【解决方案2】:

    假设您想运行一个带有或不带有一个命令行参数的 python 脚本test.py

    示例:

    没有参数-python ./test.py

    带参数-python ./test.py 'Martin'

    import sys
    
    if len(sys.argv) == 2:
        NAME = sys.argv[1]
    else:
        NAME = 'world'
    
    print("Hello %s !!" %NAME)
    

    【讨论】:

      猜你喜欢
      • 2012-10-06
      • 1970-01-01
      • 2015-07-05
      • 2012-03-08
      • 2022-01-16
      • 2020-05-04
      • 1970-01-01
      • 2017-11-04
      相关资源
      最近更新 更多