2016-11-20 7 views
1

Bei einem solchen Code, wie setze ich tatsächlich eine Datei in den Laufoptionen?Optionale Befehlszeilenargumente

Ich benutze Spyder und habe -h -s -p -o als Argumente, aber ich bin mir nicht sicher, wie man eine benannte Datei für die -o Option angibt.

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 
+0

Sie sollten die [argparse] (https://docs.python.org/2.7/library/argparse.html) Modul für die Befehlszeilenargument Parsing verwendet werden. – skrrgwasme

+0

Um Kommandozeilenargumente an Spyder zu übergeben, lesen Sie bitte: http://stackoverflow.com/questions/26679272/not-sure-how-to-use-argv-with-spyder –

Antwort

1

Dies ist ein einfaches Tutorial zu argpase.

Aber vor allem empfehle ich Ihnen, die zu lesen, wenn Sie mehr Kontrolle haben wollen, wenn Sie argparse Modul verwenden.

Auch wenn Sie Argumente zu Spyder übergeben möchten, würde ich die Antwort @Carlos Cordoba empfehlen, die vorgeschlagen, diese answer zu sehen.

Mein Tutorial-Skript:

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 -p as arguments ?") 


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

nun in Ihrem Terminal oder mit Spyder:

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

Ausgang:

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 

Und wenn verwenden Sie -h oder --help als Argument Sie werde diese Ausgabe haben:

$ python3 my_script.py -h 

Ausgang:

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