Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53373

Re: argparse - specify order of argument parsing?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
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; 'defaults': 0.07; 'modified': 0.07; 'arguments': 0.09; 'overwrite': 0.09; 'parsed': 0.09; 'skip:/ 10': 0.09; 'subject:parsing': 0.09; 'python': 0.11; '-tkc': 0.16; 'alvarez': 0.16; 'command-line': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'given,': 0.16; 'processing)': 0.16; 'specifying': 0.16; 'subject:argparse': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'seems': 0.21; 'command': 0.22; 'config': 0.24; 'specify': 0.24; "haven't": 0.24; 'environment': 0.24; "i've": 0.25; 'script': 0.25; 'first,': 0.26; 'switch': 0.26; 'least': 0.26; '(for': 0.26; 'header:In-Reply-To:1': 0.27; 'easier': 0.31; 'file': 0.32; 'skip:m 30': 0.32; 'option': 0.32; 'skip:d 20': 0.34; 'there': 0.35; 'doing': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'name': 0.63; 'alone,': 0.84; 'eduardo': 0.84; 'order:': 0.84; 'received:50.22': 0.84
Date Sat, 31 Aug 2013 12:50:16 -0500
From Tim Chase <python.list@tim.thechases.com>
To python-list@python.org
Subject Re: argparse - specify order of argument parsing?
In-Reply-To <slrnl248ua.4b8.!nospam!astrochelonian@hypnotoad.vtr.net>
References <slrnl248ua.4b8.!nospam!astrochelonian@hypnotoad.vtr.net>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Get-Message-Sender-Via boston.accountservergroup.com: none
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.419.1377971352.19984.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1377971352 news.xs4all.nl 15897 [2001:888:2000:d::a6]:50486
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53373

Show key headers only | View raw


On 2013-08-31 13:11, 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.

While I haven't come up with a good solution using argparse/optparse
alone, I've found that it's easier (for processing) to specify the
config file as an environment variable.  So rather than doing

  my_prog.py -c /path/to/config.ini arg1 arg2 ...

I just do

  MY_PROG_CONF=/path/to/config.ini my_prog.py arg1 arg2 ...

...at least on *nix systems; on Win32, it's

  c:\temp> set MY_PROG_CONF=c:\path\to\config.ini
  c:\temp> python my_prog.py arg1 arg2 ...

Then you just intercept the config-file name from os.environ:

  config_file = os.environ.get(
    "MY_PROG_CONF",
    default_ini_location)

-tkc


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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