PDA

View Full Version : Let program use command line arguments



7cardcha
August 19th, 2011, 03:00 AM
You know how when you use a command line command you do something like this (I am using the yes command as example)

yes hello

And it will print out hello to infinity. How do you make a program that can take command line arguments?

cgroza
August 19th, 2011, 03:44 AM
It is language dependent. You will have to tell us what language you are trying to do this in.

7cardcha
August 19th, 2011, 04:58 AM
Python.

Bachstelze
August 19th, 2011, 04:59 AM
Use sys.argv:


import sys
print sys.argv

Arndt
August 19th, 2011, 09:28 AM
Use sys.argv:


import sys
print sys.argv

One can add that the name 'argv' here comes from the old convention in the main function of C programs to let the array of arguments be called 'argv' (v for vector, I suppose).

MadCow108
August 19th, 2011, 09:44 AM
for more advanced command line handling use argparse (python > 2.7 but its also available as extra package for 2.6) or optparse (python < 2.7)
http://docs.python.org/dev/library/argparse.html