Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use getopt/OPTARG in Python? How to shift arguments if too many arguments (9) are given?

How to use getopt/optarg in Python?

like image 257
Mandar Pande Avatar asked Jun 17 '11 07:06

Mandar Pande


People also ask

How does getopt work in Python?

The getopt module is a parser for command-line options based on the convention established by the Unix getopt() function. It is in general used for parsing an argument sequence such as sys. argv. In other words, this module helps scripts to parse command-line arguments in sys.

What is getopt library?

getopt is a C library function used to parse command-line options of the Unix/POSIX style. It is a part of the POSIX specification, and is universal to Unix-like systems. It is also the name of a Unix program for parsing command line arguments in shell scripts.


3 Answers

A google search would have helped. Have a look at the getopt and argparse modules in the standard library:

import argparse  parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=int, nargs='+',                    help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const',                    const=sum, default=max,                    help='sum the integers (default: find the max)')  args = parser.parse_args() print args.accumulate(args.integers) 

Then run it as expected:

$ prog.py -h usage: prog.py [-h] [--sum] N [N ...]  Process some integers.  positional arguments:  N           an integer for the accumulator  optional arguments:  -h, --help  show this help message and exit  --sum       sum the integers (default: find the max) 

When run with the appropriate arguments, it prints either the sum or the max of the command-line integers:

$ prog.py 1 2 3 4 4  $ prog.py 1 2 3 4 --sum 10 

This is straight from the standard library.

like image 29
brice Avatar answered Oct 05 '22 23:10

brice


This is an example of how I do it, I usually use the same basic template:

import sys import getopt  try:     opts, args = getopt.getopt(sys.argv[1:], 'm:p:h', ['miner=', 'params=', 'help']) except getopt.GetoptError:     usage()     sys.exit(2)  for opt, arg in opts:     if opt in ('-h', '--help'):         usage()         sys.exit(2)     elif opt in ('-m', '--miner'):         miner_name = arg     elif opt in ('-p', '--params'):         params = arg     else:         usage()         sys.exit(2) 

I don't think there is any 9 parameter limit.

like image 121
Makis Avatar answered Oct 06 '22 01:10

Makis


Have you tried reading the python docs for the module getopt (http://docs.python.org/library/getopt.html?highlight=getopt#module-getopt)? It provides a simple example of how the getopt is used. What do you mean by shift arguments? If you want to check that the user does not use more than 9 arguments, you can check the length of the sys.argv list, which contains all the options/arguments passed to the script. The first element is the name of the script which is invoked, so the length is always at least 1. You could do something like:

if len(sys.argv) > 10
    print('Too many arguments.')
like image 42
iceaway Avatar answered Oct 05 '22 23:10

iceaway