Determine actually given command line arguments

Henry Leyh henry.leyh at ipp.mpg.de
Wed May 15 09:50:53 EDT 2013


On 15.05.2013 15:00, Oscar Benjamin wrote:
> On 15 May 2013 13:52, Henry Leyh <henry.leyh at ipp.mpg.de> wrote:
>> On 15.05.2013 14:24, Roy Smith wrote:
>>>
>>> In article <kmva9j$1hbk$1 at gwdu112.gwdg.de>,
>>>    Henry Leyh <henry.leyh at ipp.mpg.de> wrote:
>>>
>>>> Is there a simple way to determine which
>>>> command line arguments were actually given on the commandline, i.e. does
>>>> argparse.ArgumentParser() know which of its namespace members were
>>>> actually hit during parse_args().
>>>
>>>
>>> I think what you're looking for is sys.argv:
>>>
>>> $ cat argv.py
>>> import sys
>>> print sys.argv
>>>
>>> $ python argv.py foo bar
>>> ['argv.py', 'foo', 'bar']
>>
>> Thanks, but as I wrote in my first posting I am aware of sys.argv and was
>> hoping to _avoid_ using it because I'd then have to kind of re-implement a
>> lot of the stuff already there in argparse, e.g. parsing sys.argv for
>> short/long options, flag/parameter options etc.
>>
>> I was thinking of maybe some sort of flag that argparse sets on those
>> optional arguments created with add_argument() that are really given on the
>> command line, i.e. those that it stumbles upon them during parse_args().
>
> I don't know about that but I imagine that you could compare values
> with their defaults to see which have been changed.

Yes, I was trying that and it sort of works with strings if I use 
something sufficiently improbable like "__UNSELECTED__" as default.  But 
it gets difficult with boolean or even number arguments where you just 
may not have valid "improbable" defaults.  You could now say, so what, 
it's the default anyway.  But in my program I would like to distinguish 
between given and not given arguments rather than between default and 
non-default.

Regards,
Henry




More information about the Python-list mailing list