Exemplo n.º 1
0
        /// <summary>
        /// Adds a new Option to set the ulong specified by <paramref name="expression"/>. By default this Option is required.
        /// </summary>
        /// <param name="expression">The property.</param>
        /// <param name="config">The action to configure the Option.</param>
        /// <returns>A configured Option.</returns>
        public Option <TClass, ulong> AddOption(Expression <Func <TClass, ulong> > expression, Action <NamedArgConfig <TClass, ulong, string> > config)
        {
            var obj = new NamedArgConfig <TClass, ulong, string>(true, ToULong);

            config(obj);
            return(AddOptionCore(expression, obj));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a new Option to set the Guid? specified by <paramref name="expression"/>. By default this Option is not required.
        /// </summary>
        /// <param name="expression">The property.</param>
        /// <param name="config">The action to configure the Option.</param>
        /// <returns>A configured Option.</returns>
        public Option <TClass, Guid?> AddOption(Expression <Func <TClass, Guid?> > expression, Action <NamedArgConfig <TClass, Guid?, string> > config)
        {
            var obj = new NamedArgConfig <TClass, Guid?, string>(false, ToNullableGuid);

            config(obj);
            return(AddOptionCore(expression, obj));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds a new Option to set the Uri? specified by <paramref name="expression"/>. By default this Option is not required.
        /// </summary>
        /// <param name="expression">The property.</param>
        /// <param name="config">The action to configure the Option.</param>
        /// <returns>A configured Option.</returns>
        public Option <TClass, Uri?> AddOptionNullable(Expression <Func <TClass, Uri?> > expression, Action <NamedArgConfig <TClass, Uri?, string> > config)
        {
            var obj = new NamedArgConfig <TClass, Uri?, string>(false, ToAbsoluteNullableUri);

            config(obj);
            return(AddOptionCore(expression, obj));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds a new Option to set the string? specified by <paramref name="expression"/>. By default this Option is not required.
        /// </summary>
        /// <param name="expression">The property.</param>
        /// <param name="config">The action to configure the Option.</param>
        /// <returns>A configured Option.</returns>
        public Option <TClass, string?> AddOptionNullable(Expression <Func <TClass, string?> > expression, Action <NamedArgConfig <TClass, string?, string> > config)
        {
            var obj = new NamedArgConfig <TClass, string?, string>(false, NoConversionNullable);

            config(obj);
            return(AddOptionCore(expression, obj));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds a new Option to set the TEnum? specified by <paramref name="expression"/>. By default this Option is not required.
        /// </summary>
        /// <param name="expression">The property.</param>
        /// <param name="config">The action to configure the Option.</param>
        /// <returns>A configured Option.</returns>
        public Option <TClass, TEnum?> AddOption <TEnum>(Expression <Func <TClass, TEnum?> > expression, Action <NamedArgConfig <TClass, TEnum?, string> > config) where TEnum : struct, Enum
        {
            var obj = new NamedArgConfig <TClass, TEnum?, string>(false, (x) => ToNullableEnum <TEnum>(x, this.config.IsCaseSensitive));

            config(obj);
            return(AddOptionCore(expression, obj));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds a new Option to set the Uri specified by <paramref name="expression"/>. By default this Option is required.
        /// </summary>
        /// <param name="expression">The property.</param>
        /// <param name="config">The action to configure the Option.</param>
        /// <returns>A configured Option.</returns>
        public Option <TClass, Uri> AddOption(Expression <Func <TClass, Uri> > expression, Action <NamedArgConfig <TClass, Uri, string> > config)
        {
            var obj = new NamedArgConfig <TClass, Uri, string>(true, ToAbsoluteUri);

            config(obj);
            return(AddOptionCore(expression, obj));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Adds a new Option to set the TimeSpan specified by <paramref name="expression"/>. By default this Option is required.
        /// </summary>
        /// <param name="expression">The property.</param>
        /// <param name="config">The action to configure the Option.</param>
        /// <returns>A configured Option.</returns>
        public Option <TClass, TimeSpan> AddOption(Expression <Func <TClass, TimeSpan> > expression, Action <NamedArgConfig <TClass, TimeSpan, string> > config)
        {
            var obj = new NamedArgConfig <TClass, TimeSpan, string>(true, ToTimeSpan);

            config(obj);
            return(AddOptionCore(expression, obj));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Adds a new Option to set the decimal specified by <paramref name="expression"/>. By default this Option is required.
        /// </summary>
        /// <param name="expression">The property.</param>
        /// <param name="config">The action to configure the Option.</param>
        /// <returns>A configured Option.</returns>
        public Option <TClass, decimal> AddOption(Expression <Func <TClass, decimal> > expression, Action <NamedArgConfig <TClass, decimal, string> > config)
        {
            var obj = new NamedArgConfig <TClass, decimal, string>(true, ToDecimal);

            config(obj);
            return(AddOptionCore(expression, obj));
        }
Exemplo n.º 9
0
        public Switch <TClass, bool> AddSwitch(Expression <Func <TClass, bool> > expression, Action <NamedArgConfig <TClass, bool, bool> > config)
        {
            var obj = new NamedArgConfig <TClass, bool, bool>(false, NoConversion);

            config(obj);
            return(AddSwitchCore(expression, obj));
        }
Exemplo n.º 10
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);
        }