argparse - specify order of argument parsing?

Terry Reedy tjreedy at udel.edu
Sat Aug 31 14:13:17 EDT 2013


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




More information about the Python-list mailing list