Is there a way, when using argparse
, to tell whether a field has a value because the user specified it or because it was not specified and got the default value? Note: I would also like to consider the case where the user explicitly specifies the default value.
I would like to use argparse
to process command line arguments, and I would like to use formatter_class=argparse.ArgumentDefaultsHelpFormatter
to display what the default value will be for fields that are not specified. In addition, I would like to read values from a config file.
If the user specifies a value on the command line, I would like to make sure I use that value (even if that value matches the default, but was explicitly stated). If the user did not specify a value, but one is found in the config file, I would like to use that. If the user did not specify one on the command line, and there is not a value in the config file, then I would like to use the default value that was displayed in the usage statement above.
So, I might set up the parse like
parser = parser = argparse.ArgumentParser( description="""Tool with many ways to get values""", formatter_class=argparse.ArgumentDefaultsHelpFormatter )
parser.add_argument( '-p', '--path', help="The path to a file to read", default="data.csv" )
parser.add_argument( '-c', '--conf', help="The config file to use", default="config.txt" )
and perhaps there are many other parameters.
Now, I would like to also read a config file, which may include a value
data_path = data2.csv
So if the user specifies a -p
on the command line, I would like to read that file; if they do not and I use that config file, I would like to read data2.csv
; and if I use a config file that does not define data_path
and they do not specify -p
, I would like to use the default data.csv
.
The main complicating case for me would be if the user specifies -p data.csv
then it will have the value of the default value, but should take precedence over the config file.
Does argparse
or another similar tool have a way to tell if the parameter was set by falling through to the default or was explicitly set by the user?