Example #1
0
        /// <summary>
        /// Configures a <see cref="CommandLineApplication"/> to have an option specified by <paramref name="optionConfig"/>.
        /// </summary>
        /// <returns>
        /// The <see cref="EnumOption{T}"/> created from the <see cref="CommandOption"/>
        /// generated by <paramref name="cla"/>.
        /// </returns>
        public static EnumOption <T> EnumOption <T>(
            this CommandLineApplication cla,
            EnumOptionConfig <T> optionConfig
            ) where T : struct, System.Enum // requires c# 7.3
        {
            /*
             * TODO: should package and reuse the checks used in the builder
             * in case someone makes their EnumOptionConfig by hand to mess with the assumptions.
             */

            var optionType = optionConfig.AllowMultiple ?
                             CommandOptionType.MultipleValue :
                             CommandOptionType.SingleValue;

            string description = GenerateDescription(optionConfig);

            var opt = cla.Option(
                optionConfig.Template,
                description,
                optionType,
                optionConfig.Configuration,
                optionConfig.Inherited
                );

            return(new EnumOption <T>(
                       opt,
                       optionConfig.OptionValueMap,
                       optionConfig.ThrowOnInvalidOption
                       ));
        }
Example #2
0
        private static string GenerateDescription <T>(EnumOptionConfig <T> optionConfig) where T : struct, System.Enum
        {
            string description = optionConfig.Description;

            if (optionConfig.DescribeArity)
            {
                const string mono  = " Allows a single value.";
                const string multi = " Allows multiple values.";
                description += optionConfig.AllowMultiple ? multi : mono;
            }

            if (optionConfig.DescribeValues)
            {
                string valueDesc = optionConfig.OptionValueMap.Keys
                                   .Aggregate((l, r) => l + ", " + r);

                description += $" Possible values are: [{valueDesc}]";
            }

            return(description);
        }