-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[v2] Add --help alias for rendering help docs #10657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
Changes from all commits
c7c9670
61a7224
f9b31e0
ae8b30e
ce114d8
f8f80a3
cb1193f
de6ba42
408e817
80f0d9e
33b16dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "type": "enhancement", | ||
| "category": "help", | ||
| "description": "Add a ``--help`` parameter that renders the same help as the ``help`` parameter on every command." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ | |
| MainArgParser, | ||
| ServiceArgParser, | ||
| SubCommandArgParser, | ||
| detect_help_flag, | ||
| ) | ||
| from awscli.argprocess import unpack_argument | ||
| from awscli.arguments import ( | ||
|
|
@@ -94,7 +95,7 @@ | |
| HISTORY_RECORDER = get_global_history_recorder() | ||
| METADATA_FILENAME = 'metadata.json' | ||
| INSTALL_FILENAME = 'install.json' | ||
| _NO_AUTO_PROMPT_ARGS = ['help', '--version'] | ||
| _NO_AUTO_PROMPT_ARGS = ['help', '--help', '--version'] | ||
| _CLI_AUTO_PROMPT_OPTION = '--cli-auto-prompt' | ||
| _NO_CLI_AUTO_PROMPT_OPTION = '--no-cli-auto-prompt' | ||
| # Don't remove this line. The idna encoding | ||
|
|
@@ -589,8 +590,11 @@ def main(self, args=None): | |
| command_table = self._get_command_table() | ||
| parser = self.create_parser(command_table) | ||
| self._add_aliases(command_table, parser) | ||
| args, help_flag = detect_help_flag(args) | ||
| parsed_args = None | ||
| try: | ||
| if help_flag: | ||
| return self._route_help(args, command_table) | ||
| # Because _handle_top_level_args emits events, it's possible | ||
| # that exceptions can be raised, which should have the same | ||
| # general exception handling logic as calling into the | ||
|
|
@@ -615,6 +619,41 @@ def main(self, args=None): | |
| parsed_globals=parsed_args, | ||
| ) | ||
|
|
||
| def _route_help(self, args, command_table): | ||
| # Follow the user's command path (e.g. "s3api delete-object") to | ||
| # find the deepest recognized command, then render its help | ||
| # directly. This avoids injecting a bare 'help' token that | ||
| # could be consumed as a flag value. | ||
| current_cmd = None | ||
| for arg in args: | ||
| if arg.startswith('-'): | ||
| continue | ||
| if arg in command_table: | ||
| current_cmd = command_table[arg] | ||
| command_table = getattr( | ||
| current_cmd, 'subcommand_table', {} | ||
| ) | ||
| if not command_table: | ||
| # No further subcommands (e.g. we reached an | ||
| # operation). Stop scanning so remaining bare | ||
| # words (positional param values) aren't | ||
| # misinterpreted as commands. | ||
| break | ||
| else: | ||
| # Bare word that isn't a known command — let the | ||
| # real parser produce the "invalid choice" error. | ||
| if current_cmd is not None: | ||
| current_cmd([arg, 'help'], None) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly to my other comment |
||
| else: | ||
| parser = self.create_parser(command_table) | ||
| parser.parse_known_args(args) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return | ||
| if current_cmd is None: | ||
| return self.create_help_command()([], None) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work for aliases?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like |
||
| help_cmd = current_cmd.create_help_command() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every other double-help collapses to one rendering ( |
||
| if help_cmd is not None: | ||
| return help_cmd([], None) | ||
|
|
||
| def _emit_session_event(self, parsed_args): | ||
| # This event is guaranteed to run after the session has been | ||
| # initialized and a profile has been set. This was previously | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,3 +116,7 @@ | |
| * enhanced | ||
|
|
||
|
|
||
| ``--help`` (boolean) | ||
|
|
||
| Display help for the command/subcommand. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,4 @@ | |
| [--cli-auto-prompt] | ||
| [--no-cli-auto-prompt] | ||
| [--cli-error-format <value>] | ||
| [--help] | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allowing abbreviations means that any existing top level parameter that turns into a CLI flag and shares a prefix with
helpis now superseded by the help system. For example,aws shield associate-health-checkhas a parameter--health-check-arnPreviously,--he arn:123:abcwould pass the valuearn:123:abcto theHealthCheckArnproperty:However, with this change, this now opens the help. While this example may be mitigated by determining that a value follows the flag, there's no limitation that a top level parameter with a shared prefix to
helphas to.