Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional command line arguments

Given code like this, how do I actually set a file in the run options?

I am using Spyder and have put -h -s -p -o as arguments, but I'm not sure how to specify a named file for the -o option.

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
like image 611
user1893110 Avatar asked Nov 20 '16 23:11

user1893110


2 Answers

This is a simple tutorial dealing with argpase.

But first of all, i recommend you to read the official documentation if you want to have more control when using argparse module.

Also if you want to pass arguments to Spyder, i would recommend the answer of @Carlos Cordoba who suggested to see this answer.

My tutorial script:

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()

Now, in your terminal or with Spyder:

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

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

And when you use -h or --help as argument you'll have this output:

$ python3 my_script.py -h

Output:

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
like image 121
Chiheb Nexus Avatar answered Oct 14 '22 00:10

Chiheb Nexus


Suppose you want to run a python script test.py with or without one command line argument.

examples:

without argument- python ./test.py

with argument- python ./test.py 'Martin'

import sys

if len(sys.argv) == 2:
    NAME = sys.argv[1]
else:
    NAME = 'world'

print("Hello %s !!" %NAME)
like image 45
Mithun Biswas Avatar answered Oct 13 '22 23:10

Mithun Biswas