From cb5e0005a6ed34cae0700a2faad4e41016108422 Mon Sep 17 00:00:00 2001 From: "Tejeda, Engelbert" Date: Mon, 23 Sep 2019 12:31:42 -0400 Subject: [PATCH] Fixed bug in cli re-ordering logic for Python 2.7.x Parameter mapping, a crucial step in the reordering of cli options/variables was not being computed correctly This was due to bad dictionary update logic --- ansible_taskrunner/cli.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/ansible_taskrunner/cli.py b/ansible_taskrunner/cli.py index c3b3024..a9a22c7 100644 --- a/ansible_taskrunner/cli.py +++ b/ansible_taskrunner/cli.py @@ -347,24 +347,29 @@ def run(args=None, **kwargs): # We're working with the optional # parameter set in either case if req_parameters: - dict(opt_parameters).update(dict(req_parameters)) - parameter_mapping = opt_parameters + d_req = dict(req_parameters) + d_opt = dict(opt_parameters) + d_req.update(d_opt) + parameter_mapping = d_req else: - dict(opt_parameters) - parameter_mapping = opt_parameters + parameter_mapping = dict(opt_parameters) # Next, we create a dictionary that holds cli arguments # in the order they were called, as per the parameter mapping ordered_args = {} for k, v in parameter_mapping.items(): for a in sys.argv: - if re.search(k, a): + if re.match(k, a): for o in k.split('|'): if o in sys.argv: i = sys.argv.index(o) ordered_args[k] = i # Lastly, we convert our kwargs object to # an ordered dictionary object as per the above - kwargs = OrderedDict([(parameter_mapping[k],kwargs.get(parameter_mapping[k])) for k, v in sorted(ordered_args.items(), key=lambda item: item[1])]) + ordered_args_tuples = [] + for k, v in sorted(ordered_args.items(), key=lambda item: item[1]): + o_tuple = (parameter_mapping[k], kwargs.get(parameter_mapping[k])) + ordered_args_tuples.append(o_tuple) + kwargs = OrderedDict(ordered_args_tuples) for key, value in kwargs.items(): if key.startswith('_'): cli_vars += '{k}="{v}"\n'.format(k=key, v=value)