command line args

J

joe

Hello,

I have the following commands:
testb -s <size>
testb -s <size> -o <input file>
testb -s <size> -o <codes>

How do i split the commands so that all three are valid. And how do i
check for missing arguments?

Thanks,
-Joe
 
T

Tim Daneliuk

Hello,

I have the following commands:
testb -s <size>
testb -s <size> -o <input file>
testb -s <size> -o <codes>

How do i split the commands so that all three are valid. And how do i
check for missing arguments?

Thanks,
-Joe

Look into the getopt module. It vastly simplifies parsing command line
arguments. For example, here is how I use it for handling the passing of
a configuration file, requesting help, or printing the program version.
This bit of code also will look for an environment variable named the
same as the program itself for further args:


#----------------------------------------------------------#
# Program Entry Point #
#----------------------------------------------------------#

import getopt
import sys

PROGNAME="MyNiftyProgram"
RCSID="$Id$" # Will be filled in by RCS checkin.out
CFGFILE="Default.config" # Default configuration file

def Usage():
print "Information about proper program command line use."


# Command line processing - Process any options set in the
# environment first, and then those given on the command line

OPTIONS = sys.argv[1:]
envopt = os.getenv(PROGNAME.upper())
if envopt:
OPTIONS = envopt.split() + OPTIONS

try:
opts, args = getopt.getopt(OPTIONS, '-f:hv')
except getopt.GetoptError:
Usage()
sys.exit(1)

for opt, val in opts:
if opt == "-f":
CFGFILE=val
if opt == "-h":
Usage()
sys.exit(0)
if opt == "-v":
print RCSID
sys.exit(0)

# Rest of program goes here ....
 
S

Steven Bethard

Hello,

I have the following commands:
testb -s <size>
testb -s <size> -o <input file>
testb -s <size> -o <codes>

How do i split the commands so that all three are valid. And how do i
check for missing arguments?

Use optparse. It's an improvement over the old getopt that makes
writing option parsers easy:

py> import optparse
py> option_parser = optparse.OptionParser(
.... usage='%prog -s <size> [-o <input file|codes>]')
py> option_parser.add_option('-s', metavar='<size>', type='int')
<Option at 0x12b1ee0: -s>
py> option_parser.add_option('-o', metavar='<input file|codes>')
<Option at 0x12b1fd0: -o>
py> options, args = option_parser.parse_args('-s 1 -o data.txt'.split())
py> options.s, options.o
(1, 'data.txt')
py> option_parser.print_help()
usage: evaluation.py -s <size> [-o <input file|codes>]

options:
-h, --help show this help message and exit
-s <size>
-o <input file|codes>

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top