|
30 | 30 | ScopedConfigProvider, |
31 | 31 | ) |
32 | 32 | from botocore.context import start_as_current_context |
| 33 | +from botocore.exceptions import ProfileNotFound |
33 | 34 | from botocore.history import get_global_history_recorder |
34 | 35 |
|
35 | 36 | from awscli import __version__ |
|
49 | 50 | ListArgument, |
50 | 51 | UnknownArgumentError, |
51 | 52 | ) |
52 | | -from awscli.autoprompt.core import AutoPromptDriver |
53 | 53 | from awscli.commands import CLICommand |
54 | 54 | from awscli.compat import ( |
55 | 55 | default_pager, |
|
58 | 58 | validate_preferred_output_encoding, |
59 | 59 | ) |
60 | 60 | from awscli.constants import PARAM_VALIDATION_ERROR_RC |
| 61 | +from awscli.customizations.exceptions import ParamValidationError |
61 | 62 | from awscli.errorhandler import ( |
62 | 63 | construct_cli_error_handlers_chain, |
63 | 64 | construct_entry_point_handlers_chain, |
|
90 | 91 | ) |
91 | 92 | HISTORY_RECORDER = get_global_history_recorder() |
92 | 93 | METADATA_FILENAME = 'metadata.json' |
| 94 | +_NO_AUTO_PROMPT_ARGS = ['help', '--version'] |
| 95 | +_CLI_AUTO_PROMPT_OPTION = '--cli-auto-prompt' |
| 96 | +_NO_CLI_AUTO_PROMPT_OPTION = '--no-cli-auto-prompt' |
93 | 97 | # Don't remove this line. The idna encoding |
94 | 98 | # is used by getaddrinfo when dealing with unicode hostnames, |
95 | 99 | # and in some cases, there appears to be a race condition |
@@ -126,6 +130,36 @@ def create_clidriver(args=None): |
126 | 130 | return driver |
127 | 131 |
|
128 | 132 |
|
| 133 | +def validate_auto_prompt_args_are_mutually_exclusive(args): |
| 134 | + no_cli_auto_prompt = _NO_CLI_AUTO_PROMPT_OPTION in args |
| 135 | + cli_auto_prompt = _CLI_AUTO_PROMPT_OPTION in args |
| 136 | + if cli_auto_prompt and no_cli_auto_prompt: |
| 137 | + raise ParamValidationError( |
| 138 | + 'Both --cli-auto-prompt and --no-cli-auto-prompt cannot be ' |
| 139 | + 'specified at the same time.' |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +def resolve_auto_prompt_mode(args, session): |
| 144 | + # Order of precedence to check: |
| 145 | + # - check if any arg from _NO_AUTO_PROMPT_ARGS in args |
| 146 | + # - check if '--no-cli-auto-prompt' was specified |
| 147 | + # - check if '--cli-auto-prompt' was specified |
| 148 | + # - check configuration chain |
| 149 | + validate_auto_prompt_args_are_mutually_exclusive(args) |
| 150 | + if any(arg in args for arg in _NO_AUTO_PROMPT_ARGS): |
| 151 | + return 'off' |
| 152 | + if _NO_CLI_AUTO_PROMPT_OPTION in args: |
| 153 | + return 'off' |
| 154 | + if _CLI_AUTO_PROMPT_OPTION in args: |
| 155 | + return 'on' |
| 156 | + try: |
| 157 | + config = session.get_config_variable('cli_auto_prompt') |
| 158 | + return config.lower() |
| 159 | + except ProfileNotFound: |
| 160 | + return 'off' |
| 161 | + |
| 162 | + |
129 | 163 | def _get_distribution_source(): |
130 | 164 | metadata_file = os.path.join( |
131 | 165 | os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data'), |
@@ -220,12 +254,17 @@ def _do_main(self, args): |
220 | 254 | driver = self._driver |
221 | 255 | if driver is None: |
222 | 256 | driver = create_clidriver(args) |
223 | | - autoprompt_driver = AutoPromptDriver(driver) |
224 | | - auto_prompt_mode = autoprompt_driver.resolve_mode(args) |
| 257 | + auto_prompt_mode = resolve_auto_prompt_mode(args, driver.session) |
225 | 258 | if auto_prompt_mode == 'on': |
| 259 | + from awscli.autoprompt.core import AutoPromptDriver |
| 260 | + |
| 261 | + autoprompt_driver = AutoPromptDriver(driver) |
226 | 262 | args = autoprompt_driver.prompt_for_args(args) |
227 | 263 | rc = self._run_driver(driver, args, prompt_mode='on') |
228 | 264 | elif auto_prompt_mode == 'on-partial': |
| 265 | + from awscli.autoprompt.core import AutoPromptDriver |
| 266 | + |
| 267 | + autoprompt_driver = AutoPromptDriver(driver) |
229 | 268 | autoprompt_driver.inject_silence_param_error_msg_handler(driver) |
230 | 269 | rc = self._run_driver(driver, args, prompt_mode='off') |
231 | 270 | if rc == PARAM_VALIDATION_ERROR_RC: |
|
0 commit comments