示例#1
0
 private T GetOption <V, T>(V cliValue, CLIOption <T> option)
 {
     if (cliValue != null && ((option.ValueType == CommandOptionType.NoValue && cliValue is bool boolValue && boolValue == true) || option.ValueType != CommandOptionType.NoValue))
     {
         // Convert the cliValue string to the desired type
         return(ConvertTo(cliValue, option));
     }
示例#2
0
 private T GetOption <T>(CommandOption value, CLIOption <T> defaultValue) where T : IConvertible
 {
     if (value.HasValue())
     {
         //Convert commandOptionValue to desired type
         return((T)Convert.ChangeType(value.Value(), typeof(T)));
     }
     if (config != null)
     {
         // Check if there is a threshold options object and use it when it's available
         string thresholdOptionsSectionKey = "threshold-options";
         if (config.GetSection(thresholdOptionsSectionKey).Exists() &&
             !string.IsNullOrEmpty(config.GetSection(thresholdOptionsSectionKey).GetValue(defaultValue.JsonKey, string.Empty).ToString()))
         {
             return(config.GetSection(thresholdOptionsSectionKey).GetValue <T>(defaultValue.JsonKey));
         }
         //Else return config value
         else if (!string.IsNullOrEmpty(config.GetValue(defaultValue.JsonKey, string.Empty).ToString()))
         {
             return(config.GetValue <T>(defaultValue.JsonKey));
         }
     }
     //Else return default
     return(defaultValue.DefaultValue);
 }
示例#3
0
        private T GetOption <V, T>(V cliValue, CLIOption <T> option)
        {
            if (cliValue != null)
            {
                // Convert the cliValue string to the disired type
                return(ConvertTo(cliValue, option));
            }
            else if (config != null)
            {
                // Try to get the value from the config file
                if (typeof(IEnumerable).IsAssignableFrom(typeof(T)) && typeof(T) != typeof(string))
                {
                    return(config.GetSection(option.JsonKey).Get <T>());
                }
                else
                {
                    string configValue = config.GetValue(option.JsonKey, string.Empty).ToString();
                    if (!string.IsNullOrEmpty(configValue))
                    {
                        return(ConvertTo(configValue, option));
                    }
                }
            }

            // Unable to get value from user, return default value
            return(option.DefaultValue);
        }
示例#4
0
        /// <summary>
        /// Simplify app option creation to prevent code duplication
        /// </summary>
        private CommandOption CreateOption <T>(CommandLineApplication app, CLIOption <T> option)
        {
            var description = option.IsDeprecated
                ? $"(deprecated:{option.DeprecatedMessage})" + option.ArgumentDescription
                : option.ArgumentDescription;

            return(app.Option($"{option.ArgumentName} | {option.ArgumentShortName}",
                              description,
                              option.ValueType));
        }
示例#5
0
        private T GetOption <V, T>(V cliValue, CLIOption <T> option)
        {
            T   value    = default;
            var hasValue = false;

            if (cliValue != null &&
                (option.ValueType == CommandOptionType.NoValue && cliValue is bool boolValue && boolValue ||
                 option.ValueType != CommandOptionType.NoValue))
            {
                // Convert the cliValue string to the desired type
                value    = ConvertTo(cliValue, option);
                hasValue = true;
            }
示例#6
0
 private T GetOption <T>(CommandOption value, CLIOption <T> defaultValue) where T : IConvertible
 {
     if (value.HasValue())
     {
         //Convert commandOptionValue to desired type
         return((T)Convert.ChangeType(value.Value(), typeof(T)));
     }
     if (config != null &&
         !string.IsNullOrEmpty(config.GetValue(defaultValue.JsonKey, string.Empty).ToString()))
     {
         //Else return config value
         return(config.GetValue <T>(defaultValue.JsonKey));
     }
     //Else return default
     return(defaultValue.DefaultValue);
 }
示例#7
0
        private T ConvertTo <V, T>(V value, CLIOption <T> option)
        {
            try
            {
                if (typeof(IEnumerable).IsAssignableFrom(typeof(T)) && typeof(T) != typeof(string))
                {
                    // Convert json array to IEnummerable of desired type
                    var list = JsonConvert.DeserializeObject <T>(value as string);
                    return(list);
                }
                else
                {
                    // Convert value to desired type
                    return((T)Convert.ChangeType(value, typeof(T)));
                }
            }
            catch (Exception ex)
            {
                throw new StrykerInputException("A value passed to an option was not valid.", $@"The option {option.ArgumentName} with value {value} is not valid.
Hint:
{ex.Message}");
            }
        }
示例#8
0
 private T GetOption <T>(string value, CLIOption <T> defaultValue)
 {
     if (value != null)
     {
         return(ConvertTo <T>(value));
     }
     if (config != null)
     {
         // Check if there is a threshold options object and use it when it's available
         string thresholdOptionsSectionKey = "threshold-options";
         if (config.GetSection(thresholdOptionsSectionKey).Exists() &&
             !string.IsNullOrEmpty(config.GetSection(thresholdOptionsSectionKey).GetValue(defaultValue.JsonKey, string.Empty).ToString()))
         {
             return(config.GetSection(thresholdOptionsSectionKey).GetValue <T>(defaultValue.JsonKey));
         }
         //Else return config value
         else if (!string.IsNullOrEmpty(config.GetValue(defaultValue.JsonKey, string.Empty).ToString()))
         {
             return(config.GetValue <T>(defaultValue.JsonKey));
         }
     }
     //Else return default
     return(defaultValue.DefaultValue);
 }
示例#9
0
 /// <summary>
 /// Simplify app option creation to prevent code duplication
 /// </summary>
 private CommandOption CreateOption <T>(CommandLineApplication app, CLIOption <T> option) where T : IConvertible
 {
     return(app.Option($"{option.ArgumentName} | {option.ArgumentShortName}",
                       option.ArgumentDescription,
                       CommandOptionType.SingleValue));
 }
示例#10
0
 /// <summary>
 /// Simplify app option creation to prevent code duplication
 /// </summary>
 private CommandOption CreateOption <T>(CommandLineApplication app, CLIOption <T> option)
 {
     return(app.Option($"{option.ArgumentName} | {option.ArgumentShortName}",
                       option.ArgumentDescription,
                       option.ValueType));
 }