Merge pull request #4562 from ryanpetrello/fix-required-cli-args

fix a formatting bug re: required arguments in the CLI

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot] 2019-08-22 22:03:44 +00:00 committed by GitHub
commit 39a96a620e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 27 deletions

View File

@ -54,14 +54,22 @@ class ResourceOptionsParser(object):
add_output_formatting_arguments(parser, {})
def build_query_arguments(self, method, http_method):
required_group = None
if filter(
lambda param: param.get('required', False) is True,
self.options.get(http_method, {}).values()
):
if method in self.parser.choices:
required_group = self.parser.choices[method].add_argument_group('required arguments')
# put the required group first (before the optional args group)
self.parser.choices[method]._action_groups.reverse()
for k, param in self.options.get(http_method, {}).items():
required = (
method == 'create' and
param.get('required', False) is True
)
help_text = param.get('help_text', '')
if required:
help_text = '[REQUIRED] {}'.format(help_text)
if method == 'list':
help_text = 'only list {} with the specified {}'.format(
@ -98,10 +106,16 @@ class ResourceOptionsParser(object):
elif param['type'] in meta_map:
kwargs['metavar'] = meta_map[param['type']]
self.parser.choices[method].add_argument(
'--{}'.format(k),
**kwargs
)
if required:
required_group.add_argument(
'--{}'.format(k),
**kwargs
)
else:
self.parser.choices[method].add_argument(
'--{}'.format(k),
**kwargs
)
def handle_custom_actions(self):
for _, action in CustomAction.registry.items():

View File

@ -98,26 +98,6 @@ class TestOptions(unittest.TestCase):
self.parser.choices['create'].print_help(out)
assert '--first_name TEXT Please specify your first name' in out.getvalue()
def test_creation_required_argument(self):
page = OptionsPage.from_json({
'actions': {
'POST': {
'username': {
'type': 'string',
'help_text': 'Please specify a username',
'required': True
}
},
}
})
options = ResourceOptionsParser(page, self.parser)
options.build_query_arguments('create', 'POST')
assert 'create' in self.parser.choices
out = StringIO()
self.parser.choices['create'].print_help(out)
assert '--username TEXT [REQUIRED] Please specify a username' in out.getvalue()
def test_creation_required_argument(self):
page = OptionsPage.from_json({
'actions': {
@ -136,7 +116,7 @@ class TestOptions(unittest.TestCase):
out = StringIO()
self.parser.choices['create'].print_help(out)
assert '--username TEXT [REQUIRED] Please specify a username' in out.getvalue()
assert '--username TEXT Please specify a username'
def test_integer_argument(self):
page = OptionsPage.from_json({