What command should be use when the testing of arguments is failed?

Jean-Michel Pichavant jeanmichel at sequans.com
Thu Oct 15 12:11:13 CEST 2009


Peng Yu wrote:
> I have the following python code snippet. I'm wondering what command I
> should use to terminate the program if the arguments are not right.
>
> #!/usr/bin/env python
>
> import sys
> import os
>
> if len(sys.argv) <= 1:
>   print "usage:", os.path.basename(sys.argv[0]), '<something>'
>   return ## what command should be used here to terminate the program?
>   
Any non zero value (usually -1) will do the trick thrown with a sys.exit;

By the way, if you are so worried about this kind of detail, you should 
check out http://docs.python.org/library/optparse.html.
Then you'll get easily a GNU/POSIX syntax for you program.

Yet if you don't want to use it, at least read the 16.4.2.8 section, 
where a common way to handle option/argument issues is described.

JM



More information about the Python-list mailing list