Determine actually given command line arguments

Roy Smith roy at panix.com
Wed May 15 11:29:18 EDT 2013


In article <kn00fb$8kc$1 at gwdu112.gwdg.de>,
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.

Sorry, I missed that.

I'm not clear on exactly what you're trying to do.  You say:

> Now I would also like the program to be able to _write_ a 
> configparser config file that contains only the parameters actually 
> given on the commandline. 

I'm guessing what you're trying to do is parse the command line first,
then anything that was set there can get overridden by a value in the
config file?  That seems backwards.  Usually, the order is:

1) built-in default
2) config file (possibly a system config file, then a per-user one)
3) environment variable
4) command-line argument

It sounds like you're doing it in the reverse order -- allowing the
config file to override the command line.



More information about the Python-list mailing list