473,473 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

command line args

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
2 2640
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Manlio Perillo | last post by:
Regards. In the standard library there are two modules for command line parsing: optparse and getopt. In the Python Cookbook there is another simple method for parsing, using a docstring. ...
0
by: andrea valle | last post by:
Hi to all, I need to run a program from inside python (substantially, algorithmic batch processing). I'm on mac osx 10.3.8 with python 2.3 framework and macpython. Trying to use exec*, I...
2
by: paul | last post by:
I have a file type that is going to be associated with my visual basic application and i want the user to be able to double click on a file of said type and have it launch the program and load the...
8
by: djc | last post by:
I'm new to this and was wondering what the options are for interpreting the command line using a CLI program. Specifically methods for interpreting the parameters passed to the program on the...
4
by: Milan | last post by:
How do you pass in arguments from the command line to a .Net windows app. Senario: vb.net application should be able to execute from command prompt by passing login and password and should be...
6
by: shana07 | last post by:
Friends, I wish to consult about this big arithmetic program. This program basically is calculating big numbers. It work great from command prompt, for instance: java bigal 99999999999999999 + 5....
11
by: imtiazalikhan | last post by:
hi i am trying to sum 10 numbers passing them by command line line argument but the problem is it only work with 1 and if i passing other numbers then again it will show the result of number1:...
4
by: cjt22 | last post by:
Hi there. I just wondered whether anyone could recommend the correct way I should be passing command line parameters into my program. I am currently using the following code: def main(argv =...
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
6
by: tvaughan77 | last post by:
Hi, I have some code that I want to use to run a command line utility and I want to be able to run it from an aspx page running under IIS. The command line utility is a local utility running on...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.