|
12 | 12 | # language governing permissions and limitations under the License. |
13 | 13 | import logging |
14 | 14 | import os |
| 15 | +import re |
15 | 16 | from botocore import xform_name |
16 | 17 | from botocore.model import StringShape |
17 | 18 | from botocore.utils import is_json_value_header |
|
26 | 27 | ) |
27 | 28 |
|
28 | 29 | LOG = logging.getLogger(__name__) |
| 30 | +EXAMPLES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 31 | + 'examples') |
| 32 | +GLOBAL_OPTIONS_FILE = os.path.join(EXAMPLES_DIR, 'global_options.rst') |
| 33 | +GLOBAL_OPTIONS_SYNOPSIS_FILE = os.path.join(EXAMPLES_DIR, |
| 34 | + 'global_synopsis.rst') |
29 | 35 |
|
30 | 36 |
|
31 | 37 | class CLIDocumentEventHandler(object): |
@@ -146,6 +152,8 @@ def doc_synopsis_option(self, arg_name, help_command, **kwargs): |
146 | 152 |
|
147 | 153 | def doc_synopsis_end(self, help_command, **kwargs): |
148 | 154 | doc = help_command.doc |
| 155 | + # Append synopsis for global options. |
| 156 | + doc.write_from_file(GLOBAL_OPTIONS_SYNOPSIS_FILE) |
149 | 157 | doc.style.end_codeblock() |
150 | 158 | # Reset the documented arg groups for other sections |
151 | 159 | # that may document args (the detailed docs following |
@@ -183,6 +191,11 @@ def doc_option(self, arg_name, help_command, **kwargs): |
183 | 191 | doc.style.dedent() |
184 | 192 | doc.style.new_paragraph() |
185 | 193 |
|
| 194 | + def doc_global_option(self, help_command, **kwargs): |
| 195 | + doc = help_command.doc |
| 196 | + doc.style.h2('Global Options') |
| 197 | + doc.write_from_file(GLOBAL_OPTIONS_FILE) |
| 198 | + |
186 | 199 | def doc_relateditems_start(self, help_command, **kwargs): |
187 | 200 | if help_command.related_items: |
188 | 201 | doc = help_command.doc |
@@ -297,20 +310,10 @@ def doc_synopsis_end(self, help_command, **kwargs): |
297 | 310 | doc.style.new_paragraph() |
298 | 311 |
|
299 | 312 | def doc_options_start(self, help_command, **kwargs): |
300 | | - doc = help_command.doc |
301 | | - doc.style.h2('Options') |
| 313 | + pass |
302 | 314 |
|
303 | 315 | def doc_option(self, arg_name, help_command, **kwargs): |
304 | | - doc = help_command.doc |
305 | | - argument = help_command.arg_table[arg_name] |
306 | | - doc.writeln('``%s`` (%s)' % (argument.cli_name, |
307 | | - argument.cli_type_name)) |
308 | | - doc.include_doc_string(argument.documentation) |
309 | | - if argument.choices: |
310 | | - doc.style.start_ul() |
311 | | - for choice in argument.choices: |
312 | | - doc.style.li(choice) |
313 | | - doc.style.end_ul() |
| 316 | + pass |
314 | 317 |
|
315 | 318 | def doc_subitems_start(self, help_command, **kwargs): |
316 | 319 | doc = help_command.doc |
@@ -348,6 +351,9 @@ def doc_option_example(self, arg_name, help_command, **kwargs): |
348 | 351 | def doc_options_end(self, help_command, **kwargs): |
349 | 352 | pass |
350 | 353 |
|
| 354 | + def doc_global_option(self, help_command, **kwargs): |
| 355 | + pass |
| 356 | + |
351 | 357 | def doc_description(self, help_command, **kwargs): |
352 | 358 | doc = help_command.doc |
353 | 359 | service_model = help_command.obj |
@@ -384,17 +390,8 @@ def doc_description(self, help_command, **kwargs): |
384 | 390 | doc.style.h2('Description') |
385 | 391 | doc.include_doc_string(operation_model.documentation) |
386 | 392 | self._add_webapi_crosslink(help_command) |
387 | | - self._add_top_level_args_reference(help_command) |
388 | 393 | self._add_note_for_document_types_if_used(help_command) |
389 | 394 |
|
390 | | - def _add_top_level_args_reference(self, help_command): |
391 | | - help_command.doc.writeln('') |
392 | | - help_command.doc.write("See ") |
393 | | - help_command.doc.style.internal_link( |
394 | | - title="'aws help'", |
395 | | - page='/reference/index' |
396 | | - ) |
397 | | - help_command.doc.writeln(' for descriptions of global parameters.') |
398 | 395 |
|
399 | 396 | def _add_webapi_crosslink(self, help_command): |
400 | 397 | doc = help_command.doc |
@@ -592,9 +589,6 @@ def doc_output(self, help_command, event_name, **kwargs): |
592 | 589 | for member_name, member_shape in output_shape.members.items(): |
593 | 590 | self._doc_member(doc, member_name, member_shape, stack=[]) |
594 | 591 |
|
595 | | - def doc_options_end(self, help_command, **kwargs): |
596 | | - self._add_top_level_args_reference(help_command) |
597 | | - |
598 | 592 |
|
599 | 593 | class TopicListerDocumentEventHandler(CLIDocumentEventHandler): |
600 | 594 | DESCRIPTION = ( |
@@ -729,3 +723,44 @@ def _line_has_tag(self, line): |
729 | 723 |
|
730 | 724 | def doc_subitems_start(self, help_command, **kwargs): |
731 | 725 | pass |
| 726 | + |
| 727 | + |
| 728 | +class GlobalOptionsDocumenter: |
| 729 | + """Documenter used to pre-generate global options docs.""" |
| 730 | + |
| 731 | + def __init__(self, help_command): |
| 732 | + self._help_command = help_command |
| 733 | + |
| 734 | + def _remove_multilines(self, s): |
| 735 | + return re.sub(r'\n+', '\n', s) |
| 736 | + |
| 737 | + def doc_global_options(self): |
| 738 | + help_command = self._help_command |
| 739 | + for arg in help_command.arg_table: |
| 740 | + argument = help_command.arg_table.get(arg) |
| 741 | + help_command.doc.writeln( |
| 742 | + f"``{argument.cli_name}`` ({argument.cli_type_name})") |
| 743 | + help_command.doc.style.indent() |
| 744 | + help_command.doc.style.new_paragraph() |
| 745 | + help_command.doc.include_doc_string(argument.documentation) |
| 746 | + if argument.choices: |
| 747 | + help_command.doc.style.start_ul() |
| 748 | + for choice in argument.choices: |
| 749 | + help_command.doc.style.li(choice) |
| 750 | + help_command.doc.style.end_ul() |
| 751 | + help_command.doc.style.dedent() |
| 752 | + help_command.doc.style.new_paragraph() |
| 753 | + global_options = help_command.doc.getvalue().decode('utf-8') |
| 754 | + return self._remove_multilines(global_options) |
| 755 | + |
| 756 | + def doc_global_synopsis(self): |
| 757 | + help_command = self._help_command |
| 758 | + for arg in help_command.arg_table: |
| 759 | + argument = help_command.arg_table.get(arg) |
| 760 | + if argument.cli_type_name == 'boolean': |
| 761 | + arg_synopsis = f"[{argument.cli_name}]" |
| 762 | + else: |
| 763 | + arg_synopsis = f"[{argument.cli_name} <value>]" |
| 764 | + help_command.doc.writeln(arg_synopsis) |
| 765 | + global_synopsis = help_command.doc.getvalue().decode('utf-8') |
| 766 | + return self._remove_multilines(global_synopsis) |
0 commit comments