forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chocolateyGH-181) Add prompt_for_confirmation_short
Allows the same behavior as prompt_for_confirmation, but in a short display. Example: `Do you want to continue? (yes/no): `
- Loading branch information
1 parent
31e3a62
commit 22af53b
Showing
2 changed files
with
212 additions
and
0 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 |
---|---|---|
|
@@ -39,6 +39,40 @@ private static IConsole Console | |
get { return _console.Value; } | ||
} | ||
|
||
public static string prompt_for_confirmation_short(string prompt, IEnumerable<string> choices, int repeat = 10) | ||
{ | ||
if (repeat < 0) throw new ApplicationException("Too many bad attempts. Stopping before application crash."); | ||
Ensure.that(() => prompt).is_not_null(); | ||
Ensure.that(() => choices).is_not_null(); | ||
Ensure | ||
.that(() => choices) | ||
.meets( | ||
c => c.Any(), | ||
(name, value) => { throw new ApplicationException("No choices passed in. Please ensure you pass choices."); }); | ||
Ensure | ||
.that(() => choices) | ||
.meets( | ||
c => c.Select(entry => entry.FirstOrDefault()).Distinct().Count() == c.Count(), | ||
(name, value) => { throw new ApplicationException("Multiple choices have the same first letter. Please ensure you pass choices with different first letters."); }); | ||
|
||
"chocolatey".Log().Info(ChocolateyLoggers.Important, "{0} ({1}): ".format_with(prompt, String.Join("/", choices))); | ||
|
||
var selection = Console.ReadLine(); | ||
|
||
// check to see if value was passed | ||
foreach (var choice in choices) | ||
{ | ||
if (choice.is_equal_to(selection) || choice.FirstOrDefault().ToString().is_equal_to(selection)) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
christianrondeau
Author
Owner
|
||
{ | ||
selection = choice; | ||
return selection; | ||
} | ||
} | ||
|
||
"chocolatey".Log().Error(ChocolateyLoggers.Important, "Your choice of '{0}' is not a valid selection.".format_with(selection)); | ||
return prompt_for_confirmation_short(prompt, choices, repeat - 1); | ||
} | ||
|
||
public static string prompt_for_confirmation(string prompt, IEnumerable<string> choices, string defaultChoice, bool requireAnswer, int repeat = 10) | ||
{ | ||
if (repeat < 0) throw new ApplicationException("Too many bad attempts. Stopping before application crash."); | ||
|
I'm gathering that
.FirstOrDefault()
returns the first letter of a string? Why not use something more explicit (and less confusing) likechoice.Substring(0,1)
? Not sure what the perf penalty is for either here.Also use
.to_string()
(it might have the word safe in it) instead of.ToString()
- it's null safe.