-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Support Force & Confirm #1783
Merged
Merged
Support Force & Confirm #1783
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm not entirely sure I understand this. There are three cases:
Does using #2 mean you essentially are using your own handler and throwing away the default one?
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.
Your understanding of 1 & 3 is correct.
For 2, you can specify your own confirmation prompt handler. You get the command namespace so you can use it if required. e.g. Print a custom prompt based on the name of one of the parameters.
And no we do not currently use 2 or 3 but there are use-cases where they'll be useful.