/// <summary>
        /// Initializes a new instance of the CommandLineMode class
        /// </summary>
        /// <param name="driverType">Type of the driver</param>
        /// <param name="instance">Instance on which the mode was declared.</param>
        /// <param name="method">Method to invoke to activate the mode.</param>
        public CommandLineMode(Type driverType, object instance, MethodInfo method)
        {
            if (driverType == null)
            {
                throw new ArgumentNullException(nameof(driverType));
            }

            _instance = instance ?? throw new ArgumentNullException(nameof(instance));
            _method   = method ?? throw new ArgumentNullException(nameof(method));

            Debug.Assert(method.DeclaringType != null);
            if (!method.DeclaringType.GetTypeInfo().IsInstanceOfType(instance))
            {
                throw new ArgumentException("Expect method to be callable on instance", nameof(method));
            }

            if (!driverType.GetTypeInfo().IsAssignableFrom(method.ReturnType))
            {
                var message
                    = string.Format(
                          CultureInfo.CurrentCulture,
                          "Expected method return type to be compatible with {0}",
                          driverType.Name);
                throw new ArgumentException(message, nameof(method));
            }

            Description = CommandLineOptionBase.FindDescription(method);
            Name        = CamelCase.ToDashedName(method.Name);
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandLineSwitch"/> class
        /// </summary>
        /// <param name="instance">Instance we're configuring with this switch.</param>
        /// <param name="method">Method to invoke if this switch is present.</param>
        public CommandLineSwitch(object instance, MethodInfo method)
            : base(method)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            Debug.Assert(method.DeclaringType != null);
            if (!method.DeclaringType.GetTypeInfo().IsInstanceOfType(instance))
            {
                throw new ArgumentException("Expect method to be callable on instance", nameof(method));
            }

            _instance = instance;
            _method   = method;

            ShortName          = "-" + CamelCase.ToShortName(method.Name);
            AlternateShortName = "/" + CamelCase.ToShortName(method.Name);
            LongName           = "--" + CamelCase.ToDashedName(method.Name);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandLineParameter{V}"/> class
        /// </summary>
        /// <param name="instance">Instance that we're configuring with this parameter.</param>
        /// <param name="method">Method to invoke if this parameter is used.</param>
        public CommandLineParameter(object instance, MethodInfo method)
            : base(method)
        {
            _instance = instance ?? throw new ArgumentNullException(nameof(instance));

            Debug.Assert(method.DeclaringType != null);
            if (!method.DeclaringType.GetTypeInfo().IsInstanceOfType(instance))
            {
                throw new ArgumentException(
                          "Expect method to be callable on instance", nameof(method));
            }

            _method        = method;
            _parameterInfo = method.GetParameters().Single();

            var name = method.Name;

            ShortName          = "-" + CamelCase.ToShortName(name);
            AlternateShortName = "/" + CamelCase.ToShortName(name);
            LongName           = "--" + CamelCase.ToDashedName(name);

            IsRequired    = method.GetCustomAttribute <RequiredAttribute>() != null;
            IsMultivalued = _parameterInfo.ParameterType.IsIEnumerable();
        }