Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53400
| Path | csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'parameters': 0.04; 'argument': 0.05; 'defaults': 0.07; 'modified': 0.07; 'modify': 0.07; 'parser': 0.07; 'arguments': 0.09; 'overwrite': 0.09; 'parsed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:parsing': 0.09; 'python': 0.11; 'alvarez': 0.16; 'command-line': 0.16; 'given,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'specifying': 0.16; 'subject:argparse': 0.16; 'wrote:': 0.18; 'seems': 0.21; 'command': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'config': 0.24; 'specify': 0.24; 'url:dev': 0.24; 'script': 0.25; 'first,': 0.26; 'switch': 0.26; 'header:X -Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'file': 0.32; 'option': 0.32; 'url:python': 0.33; 'could': 0.34; 'there': 0.35; 'options:': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'thank': 0.38; 'url:library': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'skip:- 10': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'even': 0.60; 'read': 0.60; 'skip:a 50': 0.61; 'utilize': 0.61; 'obvious': 0.74; 'eduardo': 0.84; 'order:': 0.84; 'partially': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: argparse - specify order of argument parsing? |
| Date | Sun, 01 Sep 2013 08:53:10 +0200 |
| Organization | None |
| References | <slrnl248ua.4b8.!nospam!astrochelonian@hypnotoad.vtr.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p50848550.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.438.1378018398.19984.python-list@python.org> (permalink) |
| Lines | 53 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1378018398 news.xs4all.nl 15907 [2001:888:2000:d::a6]:60104 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:53400 |
Show key headers only | View raw
Eduardo Alvarez wrote:
> When using argparse, is there a way to specify in what order arguments
> get parsed? I am writing a script whose parameters can be modified in
> the following order:
>
> Defaults -> config file -> command-line switches.
>
> However, I want to give the option of specifying a config file using a
> command line switch as well, so logically, that file should be parsed
> before any other arguments are applied. However, it seems that
> parse_args() parses arguments in the order they're given, so if the
> config file switch is not given first, the config file will overwrite
> whatever was in the command-line switches, which should have higher
> priority.
>
> Thank you in advance,
If you use
http://docs.python.org/dev/library/argparse.html#fromfile-prefix-chars
to read the configuration file it should be obvious to the user that the
order is significant. You can even construct multiple config files with
partially overlapping options:
$ cat load_options.py
import argparse
parser = argparse.ArgumentParser(fromfile_prefix_chars="@")
parser.add_argument("--infile")
parser.add_argument("--outfile")
parser.add_argument("--logfile")
print(parser.parse_args())
$ cat option1.txt
--infile=alpha.txt
--outfile=beta.txt
$ cat option2.txt
--outfile=GAMMA.txt
--logfile=DELTA.txt
$ python load_options.py @option1.txt @option2.txt
Namespace(infile='alpha.txt', logfile='DELTA.txt', outfile='GAMMA.txt')
$ python load_options.py @option2.txt @option1.txt
Namespace(infile='alpha.txt', logfile='DELTA.txt', outfile='beta.txt')
If you insist you could modify the argument list with the following hack:
sys.argv[1:] = sorted(sys.argv[1:], key=lambda arg: arg.startswith("@"),
reverse=True)
There might also be a way to utilize parse_known_args().
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
argparse - specify order of argument parsing? Eduardo Alvarez <!nospam!astrochelonian@gmail.com> - 2013-08-31 13:11 -0400 Re: argparse - specify order of argument parsing? Tim Chase <python.list@tim.thechases.com> - 2013-08-31 12:50 -0500 Re: argparse - specify order of argument parsing? Terry Reedy <tjreedy@udel.edu> - 2013-08-31 14:13 -0400 Re: argparse - specify order of argument parsing? Terry Reedy <tjreedy@udel.edu> - 2013-08-31 14:17 -0400 Re: argparse - specify order of argument parsing? Peter Otten <__peter__@web.de> - 2013-09-01 08:53 +0200
csiph-web