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
This commit is contained in:
Jordan Pittier 2017-04-23 17:42:47 +02:00
parent e5e8f5617f
commit ff2c06c305
2 changed files with 15 additions and 1 deletions

View File

@ -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('_', '-')

View File

@ -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],