By themaster


2019-01-11 11:17:09 8 Comments

I have a python program that I am running through command line arguments. I have used sys module.

Below is my test.py Python file where I am taking all the args:

if len(sys.argv) > 1:
    files = sys.argv

get_input(files)

The get_input method is in another Python file where I have the options defined.

options = {

    '--case1': case1,
    '--case2': case2,

}


def get_input(arguments):

    for file in arguments[1:]:
        if file in options:
            options[file]()
        else:
            invalid_input(file)

To run:

python test.py --case1 --case2

My intentions are that I want to show the user all the commands in case they want to read the docs for that.

They should be able to read all the commands like they usually are in all the package for reading help, python test.py --help . With this they should be able to look into all the commands they can run.

How do I do this?

2 comments

@Arount 2019-01-11 11:33:32

One of the best quality a Python developer can be proud of is to use built-in libraries instead of custom ones. So let's use argparse:

import argparse

# define your command line arguments
parser = argparse.ArgumentParser(description='My application description')
parser.add_argument('--case1', help='It does something', action='store_true')
parser.add_argument('--case2', help='It does something else, I guess', action='store_true')

# parse command line arguments
args = parser.parse_args()

# Accessing arguments values
print('case1 ', args.case1)
print('case2 ', args.case2)

You can now use your cmd arguments like python myscript.py --case1

This comes with a default --help argument you can now use like: python myscript.py --help which will output:

usage: myscript.py [-h] [--case1] [--case2]

My application description

optional arguments:
  -h, --help  show this help message and exit
  --case1     It does something
  --case2     It does something else, I guess

@themaster 2019-01-11 11:41:20

Thanks Arount. This seems to be an apt choice :)

@Arount 2019-01-11 11:43:13

@themaster cool, be careful I just fixed a typo within the example code ;)

@Amit Nanaware 2019-01-11 11:27:12

Hi you can use option parser and add your options and related help information.

It has by default help option which shows all the available options which you have added.

The detailed document is here. And below is the example.

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
                  help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")

(options, args) = parser.parse_args()

@BoarGules 2019-01-11 11:33:40

optparse has been deprecated since Python 3.2. For new code, use argparse instead.

Related Questions

Sponsored Content

33 Answered Questions

[SOLVED] How to read a file line-by-line into a list?

32 Answered Questions

[SOLVED] "Least Astonishment" and the Mutable Default Argument

11 Answered Questions

[SOLVED] How to import a module given its name?

  • 2008-11-19 06:09:57
  • Kamil Kisiel
  • 240998 View
  • 407 Score
  • 11 Answer
  • Tags:   python python-import

14 Answered Questions

[SOLVED] How to return multiple values from a function?

53 Answered Questions

[SOLVED] Calling an external command in Python

11 Answered Questions

[SOLVED] Display help message with python argparse when script is called without any arguments

  • 2010-10-28 11:33:34
  • musashiXXX
  • 85432 View
  • 169 Score
  • 11 Answer
  • Tags:   python argparse

7 Answered Questions

[SOLVED] Catch multiple exceptions in one line (except block)

17 Answered Questions

[SOLVED] How to read/process command line arguments?

9 Answered Questions

[SOLVED] Simple argparse example wanted: 1 argument, 3 results

  • 2011-09-15 07:11:46
  • matt wilkie
  • 229552 View
  • 450 Score
  • 9 Answer
  • Tags:   python argparse

3 Answered Questions

[SOLVED] python script command line arguments from file

Sponsored Content