[Tutor] Two Questions...(i) Checking for None (ii) Making commandline arguments mutually exclusive

Alan Gauld alan.gauld at btinternet.com
Sat Nov 4 14:30:45 CET 2006

"Asrarahmed Kadri" <ajkadri at googlemail.com> wrote

>   *-s <IP address> -D <start date> -n <no. of days> -t <start time> 
> <end
> time>*
> the first argument which is -s (for source) can be replaced by -d 
> (for
> destination) or -o (for observer) or -r (for reporter). Now what I 
> want is
> to make sure that the user only supplies one of the options from the 
> 4
> alternatives.

I think you will need to hard-code the logic.

Check to see which options have been defined and if more than one of
the optional options exists print an error.

if a and (b or c or d) or (b and (c or d)) or  (c and d):
   print errorMessage

Another way is to count the values:

if len([val for val in [a,b,c,d] if val != None]) > 1:
   print errorMessage

> My second question is about chekcing for None type. The issue is 
> that
> optionParser sets the value = None for all the options that are not
> specified by the user. So suppose if the user doesnt specify -t, 
> then time
> would be set to None. How to check the None type.

if x == None

As simple as that.

Alan G. 

More information about the Tutor mailing list