簡體   English   中英

可選的命令行參數

[英]Optional command line arguments

給定這樣的代碼,我如何在運行選項中實際設置文件?

我正在使用 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

這是一個處理argpase的簡單教程。

但首先,如果你想在使用 argparse 模塊時有更多的控制,我建議你閱讀官方文檔

另外,如果您想將參數傳遞給 Spyder,我會推薦@Carlos Cordoba的答案,他建議查看此答案

我的教程腳本:

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

假設您想運行一個帶有或不帶有一個命令行參數的 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM