Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Passing parameters at the command line (New Python User)

Reply
Thread Tools

Passing parameters at the command line (New Python User)

 
 
cjt22@bath.ac.uk
Guest
Posts: n/a
 
      09-24-2007
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 = None):


file1= "directory1"
file2 = "directory2"


if argv is None:
args = sys.argv[1:]

if len(args) == 0:
Initialise.init(0)
Process.processCon(file1, 0)
Output.print()

for i in range(len(args)):
if args[i] == "-no":
Initialise.init(0)
Process.processCon(file2,1)
Output.print()

if args[i] == "-not":
Initialise.init(1)
Process1.process(stepStore, firstSteps)
Output.print1()



if __name__ == "__main__":
main()


Have I used bad syntax here so that a user can either run the program
with commands:
main.py
main.py -no
main.py -not

If I also wanted an option file to be passed in at the command line
for 'main.py' and 'main.py -no' what would be the best way to go about
this? I have never used Python to pass in arguments at the command
line so any help would be much appreciated.

Cheers
Chris

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      09-24-2007
wrote:

> 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:


<snip/>

Use the module optparse.

Diez
 
Reply With Quote
 
 
 
 
Ben Finney
Guest
Posts: n/a
 
      09-24-2007
writes:

> I have never used Python to pass in arguments at the command line so
> any help would be much appreciated.


Your 'main()' approach is good. I'd rather have the function require
an 'argv' parameter, and have the default set only in the 'if __name__
== "__main__":' block, since that fits my ideas better about the
defaults.

def main(argv):
parse_commandline(argv)
do_cool_stuff()

if __name__ == "__main__":
from sys import argv
main(argv)

I also tend to catch SystemExit in the function, so the exit code can
be returned instead of raised; but that's outside the scope of this
thread.

For anything more advanced than unconditionally grabbing arguments in
sequence from the command line, you should investigate the 'optparse'
module <URL:http://docs.python.org/lib/module-optparse> from the
standard library.

--
\ "Holy knit one purl two, Batman!" -- Robin |
`\ |
_o__) |
Ben Finney
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      09-24-2007
On Mon, 24 Sep 2007 01:04:58 -0700, cjt22 wrote:

> for i in range(len(args)):
> if args[i] == "-no":
> Initialise.init(0)
> Process.processCon(file2,1)
> Output.print()
>
> if args[i] == "-not":
> Initialise.init(1)
> Process1.process(stepStore, firstSteps)
> Output.print1()


That ``for`` loop is an anti-pattern in Python. If you want to iterate
over the elements of `args` the just do it directly instead of using an
index:

for arg in args:
if arg == '-no':
# ...

If you need the element *and* an index:

for i, arg in enumarate(args):
# ...

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      09-24-2007
wrote:
> 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 = None):
>
>
> file1= "directory1"
> file2 = "directory2"
>
>
> if argv is None:
> args = sys.argv[1:]
>
> if len(args) == 0:
> Initialise.init(0)
> Process.processCon(file1, 0)
> Output.print()
>
> for i in range(len(args)):
> if args[i] == "-no":
> Initialise.init(0)
> Process.processCon(file2,1)
> Output.print()
>
> if args[i] == "-not":
> Initialise.init(1)
> Process1.process(stepStore, firstSteps)
> Output.print1()
>
>
>
> if __name__ == "__main__":
> main()
>
>
> Have I used bad syntax here so that a user can either run the program
> with commands:
> main.py
> main.py -no
> main.py -not
>
> If I also wanted an option file to be passed in at the command line
> for 'main.py' and 'main.py -no' what would be the best way to go about
> this? I have never used Python to pass in arguments at the command
> line so any help would be much appreciated.


A solution using argparse (http://argparse.python-hosting.com/):

import argparse

def main(no=False, nott=False):
file1 = "directory1"
file2 = "directory2"

if nott:
print 'Initialise.init(1)'
print 'Process1.process(stepStore, firstSteps)'
print 'Output.print1()'
else:
print 'Initialise.init(0)'
if no:
print 'Process.processCon(file2, 1)'
else:
print 'Process.processCon(file1, 0)'
print 'Output.print()'

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-no', action='store_true')
parser.add_argument('-not', action='store_true', dest='nott')
args = parser.parse_args()
main(no=args.no, nott=args.nott)

Note that I've used print statements since I don't have your Initialize,
Process, etc. objects. If I knew what "-no" and "-not" meant better, I
could give you a better suggestion, e.g. where you parse the 0 or 1
value for Initialize.init directly from the command line.

STeVe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing hashes as command-line parameters Jeff Leeman Ruby 8 04-17-2009 08:39 AM
Passing arguments to a command line from a python script =?iso-8859-1?q?Luis_M._Gonz=E1lez?= Python 6 03-20-2007 03:18 AM
What is the best way for passing parameters to select command? orenr@tici.co.il ASP .Net 9 07-21-2006 11:44 AM
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57