@@ -265,22 +265,7 @@ class ArgumentParser(argparse.ArgumentParser):
265265 environment variables and .ini or .yaml-style config files.
266266 """
267267
268- def __init__ (self ,
269- add_config_file_help = True ,
270- add_env_var_help = True ,
271- auto_env_var_prefix = None ,
272- default_config_files = [],
273- ignore_unknown_config_file_keys = False ,
274- config_file_parser_class = DefaultConfigFileParser ,
275- args_for_setting_config_path = [],
276- config_arg_is_required = False ,
277- config_arg_help_message = "config file path" ,
278- args_for_writing_out_config_file = [],
279- write_out_config_file_arg_help_message = "takes the current command line "
280- "args and writes them out to a config file at the given path, then "
281- "exits" ,
282- ** kwargs
283- ):
268+ def __init__ (self , * args , ** kwargs ):
284269
285270 """Supports args of the argparse.ArgumentParser constructor
286271 as **kwargs, as well as the following additional args.
@@ -331,11 +316,34 @@ def __init__(self,
331316 write_out_config_file_arg_help_message: The help message to use for
332317 the args in args_for_writing_out_config_file.
333318 """
319+ # This is the only way to make positional args (tested in the argparse
320+ # main test suite) and keyword arguments work across both Python 2 and
321+ # 3. This could be refactored to not need extra local variables.
322+ add_config_file_help = kwargs .pop ('add_config_file_help' , True )
323+ add_env_var_help = kwargs .pop ('add_env_var_help' , True )
324+ auto_env_var_prefix = kwargs .pop ('auto_env_var_prefix' , None )
325+ default_config_files = kwargs .pop ('default_config_files' , [])
326+ ignore_unknown_config_file_keys = kwargs .pop (
327+ 'ignore_unknown_config_file_keys' , False )
328+ config_file_parser_class = kwargs .pop ('config_file_parser_class' ,
329+ DefaultConfigFileParser )
330+ args_for_setting_config_path = kwargs .pop (
331+ 'args_for_setting_config_path' , [])
332+ config_arg_is_required = kwargs .pop ('config_arg_is_required' , False )
333+ config_arg_help_message = kwargs .pop ('config_arg_help_message' ,
334+ "config file path" )
335+ args_for_writing_out_config_file = kwargs .pop (
336+ 'args_for_writing_out_config_file' , [])
337+ write_out_config_file_arg_help_message = kwargs .pop (
338+ 'write_out_config_file_arg_help_message' , "takes the current "
339+ "command line args and writes them out to a config file at the "
340+ "given path, then exits" )
341+
334342 self ._add_config_file_help = add_config_file_help
335343 self ._add_env_var_help = add_env_var_help
336344 self ._auto_env_var_prefix = auto_env_var_prefix
337345
338- argparse .ArgumentParser .__init__ (self , ** kwargs )
346+ argparse .ArgumentParser .__init__ (self , * args , * *kwargs )
339347
340348 # parse the additional args
341349 if config_file_parser_class is None :
@@ -431,6 +439,7 @@ def parse_known_args(self, args = None, namespace = None,
431439
432440 # add env var settings to the commandline that aren't there already
433441 env_var_args = []
442+ nargs = False
434443 actions_with_env_var_values = [a for a in self ._actions
435444 if not a .is_positional_arg and a .env_var and a .env_var in env_vars
436445 and not already_on_command_line (args , a .option_strings )]
@@ -439,20 +448,23 @@ def parse_known_args(self, args = None, namespace = None,
439448 value = env_vars [key ]
440449 # Make list-string into list.
441450 if action .nargs or isinstance (action , argparse ._AppendAction ):
451+ nargs = True
442452 element_capture = re .match ('\[(.*)\]' , value )
443453 if element_capture :
444454 value = [val .strip () for val in element_capture .group (1 ).split (',' ) if val .strip ()]
445455 env_var_args += self .convert_item_to_command_line_arg (
446456 action , key , value )
447457
448- args = args + env_var_args
458+ if nargs :
459+ args = args + env_var_args
460+ else :
461+ args = env_var_args + args
449462
450463 if env_var_args :
451464 self ._source_to_settings [_ENV_VAR_SOURCE_KEY ] = OrderedDict (
452465 [(a .env_var , (a , env_vars [a .env_var ]))
453466 for a in actions_with_env_var_values ])
454467
455-
456468 # before parsing any config files, check if -h was specified.
457469 supports_help_arg = any (
458470 a for a in self ._actions if isinstance (a , argparse ._HelpAction ))
@@ -484,6 +496,7 @@ def parse_known_args(self, args = None, namespace = None,
484496
485497 # add each config item to the commandline unless it's there already
486498 config_args = []
499+ nargs = False
487500 for key , value in config_items .items ():
488501 if key in known_config_keys :
489502 action = known_config_keys [key ]
@@ -503,9 +516,14 @@ def parse_known_args(self, args = None, namespace = None,
503516 if source_key not in self ._source_to_settings :
504517 self ._source_to_settings [source_key ] = OrderedDict ()
505518 self ._source_to_settings [source_key ][key ] = (action , value )
519+ if (action and action .nargs or
520+ isinstance (action , argparse ._AppendAction )):
521+ nargs = True
506522
507- args = args + config_args
508-
523+ if nargs :
524+ args = args + config_args
525+ else :
526+ args = config_args + args
509527
510528 # save default settings for use by print_values()
511529 default_settings = OrderedDict ()
0 commit comments