Exemplo n.º 1
0
        /// <summary>
        /// Adds a new Value.
        /// This is mainly useful to provide custom extension methods which take specific types of <typeparamref name="TProp"/>.
        /// </summary>
        /// <typeparam name="TProp">The type of the target property.</typeparam>
        /// <param name="expression">The property to set.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>The created Value.</returns>
        public Value <TClass, TProp> AddValueCore <TProp>(Expression <Func <TClass, TProp> > expression, NamelessArgConfig <TClass, TProp> config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config), nameof(config) + " cannot be null");
            }
            Action <TClass, TProp> setter = CliParserBuilder.GetSetMethodDelegateFromExpression(expression, out PropertyInfo pi);

            if (config.Converter == null)
            {
                throw new CliParserBuilderException(string.Concat("You need to provide a converter for the property ", pi.Name, " of the class ", typeof(TClass).Name));
            }
            config.configuredDependencies?.Validate();
            ArgumentRequired      ar    = config.configuredDependencies != null ? ArgumentRequired.HasDependencies : config.Required ? ArgumentRequired.Required : ArgumentRequired.Optional;
            Value <TClass, TProp> thing = new(config.DescriptiveName ?? pi.Name, config.HelpText ?? "No help available.", ar, setter, config.DefaultValue, config.configuredDependencies, config.Converter);

            allValues.Add(thing);
            return(thing);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a new Switch.
        /// This is mainly useful to provide custom extension methods which take specific types of <typeparamref name="TProp"/>.
        /// </summary>
        /// <typeparam name="TProp">The type of the target property.</typeparam>
        /// <param name="expression">The property to set.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>The created Switch.</returns>
        public Switch <TClass, TProp> AddSwitchCore <TProp>(Expression <Func <TClass, TProp> > expression, NamedArgConfig <TClass, TProp, bool> config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config), nameof(config) + " cannot be null");
            }
            Action <TClass, TProp> setter = CliParserBuilder.GetSetMethodDelegateFromExpression(expression, out PropertyInfo pi);

            if (config.Converter == null)
            {
                throw new CliParserBuilderException(string.Concat("You need to provide a converter for the property ", pi.Name, " of the class ", typeof(TClass).Name));
            }
            config.configuredDependencies?.Validate();
            string?shortName = config.ShortName;
            string?longName  = config.LongName;

            ApplyDefaultPrefixAndCheck(ref shortName, ref longName, "switch");
            ArgumentRequired       ar  = config.configuredDependencies != null ? ArgumentRequired.HasDependencies : config.Required ? ArgumentRequired.Required : ArgumentRequired.Optional;
            Switch <TClass, TProp> arg = new(shortName, longName, config.DescriptiveName ?? pi.Name, config.HelpText ?? "No help available.", ar, setter, config.DefaultValue, config.configuredDependencies, config.Converter);

            AddToDictionary(arg.ShortName, arg.LongName, arg, allSwitchesByName);
            allSwitches.Add(arg);
            return(arg);
        }