By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

command line args

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
jo*@gmail.com wrote:
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 ....

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jul 18 '05 #2

P: n/a
jo*@gmail.com wrote:
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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.