command line arguments

Peter Hansen peter at engcorp.com
Wed Aug 31 12:06:32 EDT 2005


Jon Hewer wrote:
> i am writing a little script and currently implementing command line
> arguments following the guide by mark pilgrim from dive into python;
> 
> http://diveintopython.org/scripts_and_streams/command_line_arguments.html
> 
> thats all fine, however i am not sure of the BEST way to handle
> multiple command line arguments
> 
> for my script, i want to be able to accept two arguments, a name and a
> url, but i am not sure if its best to use one command line option/flag
> (eg -n to specify name) and then grab the url from the extra data
> which will be in 'args':
> 
> opts, args = getopt.getopt(sys.argv[1:], "n:", ["name="])
> 
> or to have two command line options/flags, -n and -u, and checking
> that these have both been specified and then proceeding (this might be
> a little messier)
> 
> any tips would be much appreciated

I would approach this as a usability issue for your users, rather than 
from the point of view of which is "a little messier" in the code.

What's the purpose of this utility?  Is it to do something with the URL? 
  And the URL must always be specified?  What about the name?  Also 
mandatory, or optional?  The relationship between the two?

I would guess (without yet knowing what you are doing) that if these 
things are not really *optional*, making them options is not the best 
approach and you might be better off just requiring them to be specified 
on the command line as two arguments.

-Peter



More information about the Python-list mailing list