private T ReadOptionFromUser()

in src/AWS.Deploy.CLI/ConsoleUtilities.cs [630:658]


        private T ReadOptionFromUser<T>(IList<T> options, int defaultValueIndex)
        {
            if(options.Count == 0)
            {
                throw new Exception("No options available for user to select");
            }

            // If defaultValueIndex is used it starts as 1 just like the user sees the list of options.
            if (defaultValueIndex != -1 && (defaultValueIndex < 1 || defaultValueIndex > options.Count))
            {
                throw new Exception($"Invalid default index {defaultValueIndex}");
            }

            while (true)
            {
                var selectedOption = _interactiveService.ReadLine();
                if (string.IsNullOrEmpty(selectedOption) && defaultValueIndex != -1)
                {
                    return options[defaultValueIndex - 1];
                }

                if (int.TryParse(selectedOption, out var intOption) && intOption >= 1 && intOption <= options.Count)
                {
                    return options[intOption - 1];
                }

                _interactiveService.WriteLine($"Invalid option. The selected option should be between 1 and {options.Count}.");
            }
        }