mirror of
https://github.com/ansible/awx.git
synced 2026-03-06 03:01:06 -03:30
fix a formatting bug re: required arguments in the CLI
This commit is contained in:
@@ -54,14 +54,22 @@ class ResourceOptionsParser(object):
|
|||||||
add_output_formatting_arguments(parser, {})
|
add_output_formatting_arguments(parser, {})
|
||||||
|
|
||||||
def build_query_arguments(self, method, http_method):
|
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():
|
for k, param in self.options.get(http_method, {}).items():
|
||||||
required = (
|
required = (
|
||||||
method == 'create' and
|
method == 'create' and
|
||||||
param.get('required', False) is True
|
param.get('required', False) is True
|
||||||
)
|
)
|
||||||
help_text = param.get('help_text', '')
|
help_text = param.get('help_text', '')
|
||||||
if required:
|
|
||||||
help_text = '[REQUIRED] {}'.format(help_text)
|
|
||||||
|
|
||||||
if method == 'list':
|
if method == 'list':
|
||||||
help_text = 'only list {} with the specified {}'.format(
|
help_text = 'only list {} with the specified {}'.format(
|
||||||
@@ -98,10 +106,16 @@ class ResourceOptionsParser(object):
|
|||||||
elif param['type'] in meta_map:
|
elif param['type'] in meta_map:
|
||||||
kwargs['metavar'] = meta_map[param['type']]
|
kwargs['metavar'] = meta_map[param['type']]
|
||||||
|
|
||||||
self.parser.choices[method].add_argument(
|
if required:
|
||||||
'--{}'.format(k),
|
required_group.add_argument(
|
||||||
**kwargs
|
'--{}'.format(k),
|
||||||
)
|
**kwargs
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.parser.choices[method].add_argument(
|
||||||
|
'--{}'.format(k),
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
def handle_custom_actions(self):
|
def handle_custom_actions(self):
|
||||||
for _, action in CustomAction.registry.items():
|
for _, action in CustomAction.registry.items():
|
||||||
|
|||||||
@@ -98,26 +98,6 @@ class TestOptions(unittest.TestCase):
|
|||||||
self.parser.choices['create'].print_help(out)
|
self.parser.choices['create'].print_help(out)
|
||||||
assert '--first_name TEXT Please specify your first name' in out.getvalue()
|
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):
|
def test_creation_required_argument(self):
|
||||||
page = OptionsPage.from_json({
|
page = OptionsPage.from_json({
|
||||||
'actions': {
|
'actions': {
|
||||||
@@ -136,7 +116,7 @@ class TestOptions(unittest.TestCase):
|
|||||||
|
|
||||||
out = StringIO()
|
out = StringIO()
|
||||||
self.parser.choices['create'].print_help(out)
|
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):
|
def test_integer_argument(self):
|
||||||
page = OptionsPage.from_json({
|
page = OptionsPage.from_json({
|
||||||
|
|||||||
Reference in New Issue
Block a user