private void LoadOptions(IParameterCollection parameterCollection, TypeDefinition commandType) { var optionProperties = commandType.GetAllProperties() .WithAttribute(CommandLineParserTypeNames.OptionAttributeFullName) .Where(x => !x.GetAttribute(CommandLineParserTypeNames.OptionAttributeFullName).GetPropertyValueOrDefault <bool>(s_Hidden)); foreach (var property in optionProperties) { var optionAttribute = property.GetAttribute(CommandLineParserTypeNames.OptionAttributeFullName); var(name, shortName) = GetOptionNames(property); // boolean parameters are treated as switch parameters if (property.PropertyType.FullName == SystemTypeNames.BooleanFullName) { var parameter = parameterCollection.AddSwitchParameter(name, shortName?.ToString()); parameter.Description = optionAttribute.GetPropertyValueOrDefault <string>(s_HelpText); // emit a warning if parameter was flagged as required if (optionAttribute.GetPropertyValueOrDefault <bool>(s_Required)) { m_Logger.LogWarning($"Ignoring 'Required' flag of option '{name}'. Boolean options are treated as switch parameter and cannot be marked as required."); } // emit a warning if a default value other than 'false' was required var defaultValue = GetDefaultValue(property, optionAttribute); if (defaultValue != null && defaultValue != "false") { m_Logger.LogWarning($"Ignoring default value '{defaultValue}' of option '{name}'. Boolean options are treated as switch parameter with a fixed default value of 'false'"); } } else { var parameter = parameterCollection.AddNamedParameter(name, shortName?.ToString()); parameter.Description = optionAttribute.GetPropertyValueOrDefault <string>(s_HelpText); parameter.Required = optionAttribute.GetPropertyValueOrDefault <bool>(s_Required); parameter.DefaultValue = GetDefaultValue(property, optionAttribute); parameter.AcceptedValues = GetAcceptedValues(property); parameter.ValuePlaceHolderName = optionAttribute.GetPropertyValueOrDefault <string?>(s_MetaValue); } } }
/// <summary> /// Adds a new switch-parameter to the application /// </summary> public SwitchParameterDocumentation AddSwitchParameter(string?name, string?shortName) => m_Parameters.AddSwitchParameter(name, shortName);