Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if argparse argument has been actually specified on command line?

I am using arparse to update a config dict using values specified on the command line. Since i only want to update the values in the config for which a value was explicitly mentioned on the command line.

Therefore i try to identify not-specified arguments by checking for each action if getattr(args, action.dest) == action.default or equality of the type converted arg. Then i update all my values in the dict for which this is false.

But this of course fails, if i explicitly specify an argument on the command line which is the same as my default argument. Is there a possibility to identify these explicitly mentioned arguments withing argparser or do i have to identify them manually in sys.argv?

Thanks!

Edit:

To make my intentions clearer. I have an argument like the following:

parser.add_argument('--test', default='meaningful_default')

and a config like

config = { 'test' : 'nondefault_val'}

Now i want to update the config only with the explicitly specified arguments. Comparing the args attributes with the default values works as long as i don't specify something like prog.py --test meaningful_default to update my config again with a value which just happens to be also the default value

like image 894
aem Avatar asked Aug 17 '15 18:08

aem


People also ask

How do you pass arguments to Argparse?

First, we need the argparse package, so we go ahead and import it on Line 2. On Line 5 we instantiate the ArgumentParser object as ap . Then on Lines 6 and 7 we add our only argument, --name . We must specify both shorthand ( -n ) and longhand versions ( --name ) where either flag could be used in the command line.

What is parser Add_argument in Python?

parser. add_argument('indir', type=str, help='Input dir for videos') created a positional argument. For positional arguments to a Python function, the order matters. The first value passed from the command line becomes the first positional argument. The second value passed becomes the second positional argument.


1 Answers

If you prefer to use argparse and to be able to specify default values, there is a simple solution using two parsers.

I. Define your main parser and parse all your arguments with proper defaults:

parser = argparse.ArgumentParser()
parser.add_argument('--test1', default='meaningful_default1')
parser.add_argument('--test2', default='meaningful_default2')
...
args, unparsed = parser.parse_known_args()

II. Define an aux parser with argument_default=argparse.SUPPRESS to exclude unspecified arguments. Add all the arguments from the main parser but without any defaults:

aux_parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
for arg in vars(args): aux_parser.add_argument('--'+arg)
cli_args, _ = aux_parser.parse_known_args()

This is not an extremely elegant solution, but works well with argparse and all its benefits.

like image 120
MtDersvan Avatar answered Oct 11 '22 21:10

MtDersvan