Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.linkpendium.com!news.linkpendium.com!panix!panix2.panix.com!not-for-mail From: roy@panix.com (Roy Smith) Newsgroups: comp.lang.python Subject: Re: Determine actually given command line arguments Date: 15 May 2013 11:29:18 -0400 Organization: PANIX -- Public Access Networks Corp. Lines: 44 Message-ID: References: NNTP-Posting-Host: panix2.panix.com X-Trace: reader1.panix.com 1368631758 1199 166.84.1.2 (15 May 2013 15:29:18 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Wed, 15 May 2013 15:29:18 +0000 (UTC) Xref: csiph.com comp.lang.python:45353 In article , Henry Leyh wrote: >On 15.05.2013 14:24, Roy Smith wrote: >> In article , >> Henry Leyh 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.