Open macfreek opened 6 years ago
I did a quick code review to see what should be changed to make this happen.
At first sight, all it takes is adjusting convert_item_to_command_line_arg
, the lines:
args.append( command_line_key )
args.append( value )
with
args.append( command_line_key + '=' + value)
Unfortunately, this will fail: the already_on_command_line
function relies on the fact that all keys have their own argument.
I wonder why keys and values are added to the command line arguments at all if there is no associated action.
For me, the only reason to put ignore_unknown_config_file_keys=False
instead of ignore_unknown_config_file_keys=True
in my code is that I want to print a proper warning and still continue the execution of my program (I overriding the error()
method to achieve this).
Steps to reproduce
Consider the following code:
I expected this to print
error: unrecognized arguments: opt2 = 42
. (This is the error given forparser.parse_args(['bacon'])
)The actual result is that it prints
error: argument COMMAND: invalid choice: '42' (choose from 'bacon', 'egg', 'sausage', 'spam')
.Suggested solution
This is caused by a limitation of
argparse.ArgumentParser().parse_known_args()
, which is used by configargparse: ifignore_unknown_config_file_keys
is False, configargparse adds all arguments from the config file to the list of arguments, and feeds it toargparse.ArgumentParser().parse_known_args()
, with the key name and value as separate arguments. E.g.parse_known_args('--opt2', '42')
. My suggestion is to add this as combined arguments instead. E.g.parse_known_args('--opt2=42')
This will result in a much more readableerror: unrecognized arguments: --opt0=42
.