top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

argparse/python - how to specify order of argument parsing?

+1 vote
516 views

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.

posted Aug 31, 2013 by Abhay Kulkarni

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

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:pathtoconfig.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)
answer Aug 31, 2013 by Sheetal Chauhan
Similar Questions
+2 votes

Iam on python 2.7 and linux .I need to know if we need to place the modules in a particular or it doesn't matter at all while writing the program.

For Example

import os
import shlex
import subprocess
import time
import sys
import logging
import plaftform.cluster
from util import run

def main():
 """ ---MAIN--- """

if __name__ == '__main__':
 main()

In the above example :

I am guessing may be the python modules like os , shlex etc come first and later the user defined modules like import plaftform.cluster etc

Sorry if my question sounds dump , I was running pep8 and don't see its bothered much about it

0 votes

I don't quite manage to figure out gits argv parsing and would need some help on the way.

I want:
git format-patch -o outdir HEAD~

Work exactly the way it does now, setting output_directory to outdir. But I also want
git format-patch -o HEAD~

to set output_directory with a NULL value so that I can assign a default value to it. Is that even possible with the current argv-parsing implementation?

The currect argv parser is using OPTION_CALLBACK so I thought that that callback should be able to determine if there was an outdir supplied or not.

Or is the correct solution to also add a bolean type OPTION_BOOLEAN for -o?

+2 votes

How can I flatten just a specific sublist of each list in a list of lists?

So if I had this data

[ ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']],
 ['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']],
 ['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 $71685.00']],
 ['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]]

How can I make it be

[ ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00'],
 ['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00'],
 ['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00'],
 ['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']]

Been looking around but most solutions just entirely flatten everything. This was popular on SO but yeah it flattens everything I want to be more selective

def flatten(lst):
 for elem in lst:
 if type(elem) in (tuple, list):
 for i in flatten(elem):
 yield i
 else:
 yield elem

What I am thinking is that if for each list the sublist should be at index 1, so

[0][1]
[1][1]
[2][1]

for item in list:
 item[1] - somehow flatten.

Any Idea or pointer?

...