-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- When registering a command, add `confirmation=True` to enable user confirmation. Also supports a string message or callable. - Add a —force flag for commands that support this feature. - Integrated with configuration system so it can be enabled/disabled by setting AZURE_CORE_DISABLE_CONFIRM_PROMPT Added for the following commands: az group delete az vm delete az network dns zone delete
- Loading branch information
1 parent
a71e140
commit f274679
Showing
12 changed files
with
113 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
import sys | ||
from six.moves import input # pylint: disable=redefined-builtin | ||
|
||
import azure.cli.core._logging as _logging | ||
|
||
logger = _logging.get_az_logger(__name__) | ||
|
||
|
||
class NoTTYException(Exception): | ||
pass | ||
|
||
|
||
def _verify_is_a_tty(): | ||
if not sys.stdin.isatty(): | ||
logger.debug('No tty available.') | ||
raise NoTTYException() | ||
|
||
|
||
def prompt_y_n(msg, default=None): | ||
_verify_is_a_tty() | ||
if default not in [None, 'y', 'n']: | ||
raise ValueError("Valid values for default are 'y', 'n' or None") | ||
y = 'Y' if default == 'y' else 'y' | ||
n = 'N' if default == 'n' else 'n' | ||
while True: | ||
ans = input('{} ({}/{}): '.format(msg, y, n)) | ||
if ans.lower() == n.lower(): | ||
return False | ||
if ans.lower() == y.lower(): | ||
return True | ||
if default and not ans: | ||
return default == y.lower() | ||
|
||
|
||
def prompt_choice_list(msg, a_list, default=1): | ||
'''Prompt user to select from a list of possible choices. | ||
:param str msg:A message displayed to the user before the choice list | ||
:param str a_list:The list of choices (list of strings or list of dicts with 'name' & 'desc') | ||
:param int default:The default option that should be chosen if user doesn't enter a choice | ||
:returns: The list index of the item chosen. | ||
''' | ||
_verify_is_a_tty() | ||
options = '\n'.join([' [{}] {}{}' | ||
.format(i + 1, | ||
x['name'] if isinstance(x, dict) and 'name' in x else x, | ||
' - ' + x['desc'] if isinstance(x, dict) and 'desc' in x else '') | ||
for i, x in enumerate(a_list)]) | ||
allowed_vals = list(range(1, len(a_list) + 1)) | ||
while True: | ||
try: | ||
ans = int(input('{}\n{}\nPlease enter a choice [{}]: '.format(msg, options, default)) or | ||
default) | ||
if ans in allowed_vals: | ||
# array index is 0-based, user input is 1-based | ||
return ans - 1 | ||
raise ValueError | ||
except ValueError: | ||
logger.warning('Valid values are %s', allowed_vals) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters