From ff2c06c30538edb9cb47b83f0a194f7893a7f458 Mon Sep 17 00:00:00 2001 From: Jordan Pittier Date: Sun, 23 Apr 2017 17:42:47 +0200 Subject: [PATCH] Make _fix_argv() somewhat compatible with Argparse action='append' Python Argparse supports the 'append' action [1] which is super handy to allow a user to repeat several times the same argument, each time with different values. This doesn't work with occ that tries to "fix argv" but raises this error: os_client_config.exceptions.OpenStackConfigException: The following options were given: '--foo,--foo' which contain duplicates except that one has _ and one has -. There is no sane way for us to know what you're doing. Remove the duplicate option and try again This patch tweak the _fix_argv() function so that it doesn't explode if the duplicate option has no '_' not '-' in its name. Change-Id: I4f06b6aff8d3ab1df45637399bc3a9b4b61764a9 Related-bug: #1685630 --- os_client_config/config.py | 5 ++++- os_client_config/tests/test_config.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/os_client_config/config.py b/os_client_config/config.py index 89b5c6c..96d7f53 100644 --- a/os_client_config/config.py +++ b/os_client_config/config.py @@ -19,6 +19,7 @@ import collections import copy import json import os +import re import sys import warnings @@ -146,7 +147,9 @@ def _fix_argv(argv): # over the place. processed = collections.defaultdict(list) for index in range(0, len(argv)): - if argv[index].startswith('--'): + # If the value starts with '--' and has '-' or '_' in it, then + # it's worth looking at it + if re.match('^--.*(_|-)+.*', argv[index]): split_args = argv[index].split('=') orig = split_args[0] new = orig.replace('_', '-') diff --git a/os_client_config/tests/test_config.py b/os_client_config/tests/test_config.py index dcc841a..09a11d2 100644 --- a/os_client_config/tests/test_config.py +++ b/os_client_config/tests/test_config.py @@ -702,6 +702,17 @@ class TestConfigArgparse(base.TestCase): self.assertEqual(cc.config['auth']['password'], 'pass') self.assertEqual(cc.config['auth']['auth_url'], 'auth-url') + def test_argparse_action_append_no_underscore(self): + c = config.OpenStackConfig(config_files=[self.no_yaml], + vendor_files=[self.no_yaml], + secure_files=[self.no_yaml]) + parser = argparse.ArgumentParser() + parser.add_argument('--foo', action='append') + argv = ['--foo', '1', '--foo', '2'] + c.register_argparse_arguments(parser, argv=argv) + opts, _remain = parser.parse_known_args(argv) + self.assertEqual(opts.foo, ['1', '2']) + def test_argparse_underscores_duplicate(self): c = config.OpenStackConfig(config_files=[self.no_yaml], vendor_files=[self.no_yaml],