Skip to content

Commit 72acc2c

Browse files
authored
Merge pull request #9444 from aws/doc-constraints
[v2] Document parameter constraints
2 parents 74fadaa + 503622b commit 72acc2c

File tree

4 files changed

+37
-40
lines changed

4 files changed

+37
-40
lines changed

awscli/botocore/model.py

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class Shape:
8989
'contextParam',
9090
'clientContextParams',
9191
'requiresLength',
92+
'pattern',
9293
]
9394
MAP_TYPE = OrderedDict
9495

awscli/clidocs.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def doc_option(self, arg_name, help_command, **kwargs):
210210
self._add_tagged_union_note(argument.argument_model, doc)
211211
if hasattr(argument, 'argument_model'):
212212
self._document_enums(argument.argument_model, doc)
213+
self._document_constraints(argument.argument_model, doc)
213214
self._document_nested_structure(argument.argument_model, doc)
214215
doc.style.dedent()
215216
doc.style.new_paragraph()
@@ -289,6 +290,8 @@ def _do_doc_member(self, doc, member_name, member_shape, stack):
289290
doc.include_doc_string(docs)
290291
if is_tagged_union_type(member_shape):
291292
self._add_tagged_union_note(member_shape, doc)
293+
self._document_enums(member_shape, doc)
294+
self._document_constraints(member_shape, doc)
292295
doc.style.new_paragraph()
293296
member_type_name = member_shape.type_name
294297
if member_type_name == 'structure':
@@ -327,6 +330,23 @@ def _add_tagged_union_note(self, shape, doc):
327330
doc.writeln(msg)
328331
doc.style.end_note()
329332

333+
def _document_constraints(self, model, doc):
334+
"""Documents parameter value constraints"""
335+
if not hasattr(model, 'metadata'):
336+
return
337+
constraints = ['min', 'max', 'pattern']
338+
if not any(
339+
[constraint in model.metadata for constraint in constraints]
340+
):
341+
return
342+
doc.style.new_paragraph()
343+
doc.write('Constraints:')
344+
doc.style.start_ul()
345+
for constraint in constraints:
346+
if (val := model.metadata.get(constraint)) is not None:
347+
doc.style.li(f'{constraint}: ``{val}``')
348+
doc.style.end_ul()
349+
330350

331351
class ProviderDocumentEventHandler(CLIDocumentEventHandler):
332352
def doc_breadcrumbs(self, help_command, event_name, **kwargs):
@@ -609,10 +629,6 @@ def doc_option_example(self, arg_name, help_command, event_name, **kwargs):
609629
member, include_enum_values=False
610630
)
611631
doc.write('%s %s ...' % (example_type, example_type))
612-
if isinstance(member, StringShape) and member.enum:
613-
# If we have enum values, we can tell the user
614-
# exactly what valid values they can provide.
615-
self._write_valid_enums(doc, member.enum)
616632
doc.style.end_codeblock()
617633
doc.style.new_paragraph()
618634
elif cli_argument.cli_type_name not in SCALAR_TYPES:
@@ -623,13 +639,6 @@ def doc_option_example(self, arg_name, help_command, event_name, **kwargs):
623639
doc.style.end_codeblock()
624640
doc.style.new_paragraph()
625641

626-
def _write_valid_enums(self, doc, enum_values):
627-
doc.style.new_paragraph()
628-
doc.write("Where valid values are:\n")
629-
for value in enum_values:
630-
doc.write(" %s\n" % value)
631-
doc.write("\n")
632-
633642
def doc_output(self, help_command, event_name, **kwargs):
634643
doc = help_command.doc
635644
doc.style.h2('Output')

tests/functional/docs/test_help_output.py

-29
Original file line numberDiff line numberDiff line change
@@ -367,35 +367,6 @@ def test_description_from_rst_file(self):
367367
self.assert_contains('aws_access_key_id')
368368

369369

370-
class TestEnumDocsArentDuplicated(BaseAWSHelpOutputTest):
371-
def test_enum_docs_arent_duplicated(self):
372-
# Test for: https://github.com/aws/aws-cli/issues/609
373-
# What's happening is if you have a list param that has
374-
# an enum, we document it as:
375-
# a|b|c|d a|b|c|d
376-
# Except we show all of the possible enum params twice.
377-
# Each enum param should only occur once. The ideal documentation
378-
# should be:
379-
#
380-
# string1 string2
381-
#
382-
# Where each value is one of:
383-
# value1
384-
# value2
385-
self.driver.main(['cloudformation', 'list-stacks', 'help'])
386-
# "CREATE_IN_PROGRESS" is a enum value, and should only
387-
# appear once in the help output.
388-
contents = self.renderer.rendered_contents
389-
self.assertTrue(
390-
contents.count("CREATE_IN_PROGRESS") == 1,
391-
(
392-
"Enum param was only suppose to be appear once in "
393-
"rendered doc output, appeared: %s"
394-
% contents.count("CREATE_IN_PROGRESS")
395-
),
396-
)
397-
398-
399370
class TestParametersCanBeHidden(BaseAWSHelpOutputTest):
400371
def mark_as_undocumented(self, argument_table, **kwargs):
401372
argument_table['starting-sequence-number']._UNDOCUMENTED = True

tests/unit/test_clidocs.py

+16
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,22 @@ def test_tagged_union_comes_after_docstring_output(self):
499499
rendered = help_command.doc.getvalue().decode('utf-8')
500500
self.assertRegex(rendered, r'FooBar[\s\S]*Tagged Union')
501501

502+
def test_documents_constraints(self):
503+
shape = {'type': 'string', 'min': 0, 'max': 10, 'pattern': '.*'}
504+
shape = StringShape('ConstrainedArg', shape)
505+
arg = CustomArgument('ConstrainedArg', argument_model=shape)
506+
help_command = self.create_help_command()
507+
help_command.arg_table = {'ConstrainedArg': arg}
508+
operation_handler = OperationDocumentEventHandler(help_command)
509+
operation_handler.doc_option(
510+
arg_name='ConstrainedArg', help_command=help_command
511+
)
512+
rendered = help_command.doc.getvalue().decode('utf-8')
513+
self.assertIn('Constraints', rendered)
514+
self.assertIn('min: ``0``', rendered)
515+
self.assertIn('max: ``10``', rendered)
516+
self.assertIn('pattern: ``.*``', rendered)
517+
502518

503519
class TestTopicDocumentEventHandlerBase(unittest.TestCase):
504520
def setUp(self):

0 commit comments

Comments
 (0)