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


Groups > comp.lang.python > #53370 > unrolled thread

argparse - specify order of argument parsing?

Started byEduardo Alvarez <!nospam!astrochelonian@gmail.com>
First post2013-08-31 13:11 -0400
Last post2013-09-01 08:53 +0200
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#53370 — argparse - specify order of argument parsing?

FromEduardo Alvarez <!nospam!astrochelonian@gmail.com>
Date2013-08-31 13:11 -0400
Subjectargparse - specify order of argument parsing?
Message-ID<slrnl248ua.4b8.!nospam!astrochelonian@hypnotoad.vtr.net>
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,
-- 
Eduardo Alvarez

[toc] | [next] | [standalone]


#53373

FromTim Chase <python.list@tim.thechases.com>
Date2013-08-31 12:50 -0500
Message-ID<mailman.419.1377971352.19984.python-list@python.org>
In reply to#53370
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


[toc] | [prev] | [next] | [standalone]


#53374

FromTerry Reedy <tjreedy@udel.edu>
Date2013-08-31 14:13 -0400
Message-ID<mailman.420.1377972816.19984.python-list@python.org>
In reply to#53370
On 8/31/2013 1:11 PM, Eduardo Alvarez wrote:
> When using argparse, is there a way to specify in what order arguments
> get parsed?

I expect argparse to forward iterate the sequence of arguments that it 
receives.

> 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,

Right.

 > 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.

So just document that a config file has to be given first to work as 
expected. Order dependence among arguments is common.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#53375

FromTerry Reedy <tjreedy@udel.edu>
Date2013-08-31 14:17 -0400
Message-ID<mailman.421.1377973205.19984.python-list@python.org>
In reply to#53370
On 8/31/2013 2:13 PM, Terry Reedy wrote:
> On 8/31/2013 1:11 PM, Eduardo Alvarez wrote:
>> When using argparse, is there a way to specify in what order arguments
>> get parsed?
>
> I expect argparse to forward iterate the sequence of arguments that it
> receives.

Aside from the environment variable solution, you could search sys.argv 
for 'config=filename' and remove it and process it *before* you invoke 
argparse.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#53400

FromPeter Otten <__peter__@web.de>
Date2013-09-01 08:53 +0200
Message-ID<mailman.438.1378018398.19984.python-list@python.org>
In reply to#53370
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().

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web