コード例 #1
0
        public void MultipleValuesArgumentMustBeTheLastArgument()
        {
            var app = new CommandLineApplication();
            app.Argument("first", "First argument", multipleValues: true);
            var ex = Assert.Throws<InvalidOperationException>(() => app.Argument("second", "Second argument"));

            Assert.Contains($"The last argument 'first' accepts multiple values. No more argument can be added.",
                ex.Message);
        }
コード例 #2
0
        internal void AddCommandLineParameterTo(CommandLineApplication command)
        {
            var isBoolProperty = Property.PropertyType == typeof(bool);
            var optionAttribute = Property.GetOptionAttribute();

            //Note: This means all bool properties are treated as options by default.
            //ArgumentAttribute on such a property is ignored.
            if (isBoolProperty || optionAttribute != null)
            {
                //This is just so that all the below code does not need to
                //check for null on attribute. Not pure but works.
                var nullSafeOptionAttribute = optionAttribute ?? new OptionAttribute();

                var template = GetOptionTemplate(nullSafeOptionAttribute);
                var optionType = isBoolProperty ? CommandOptionType.NoValue : CommandOptionType.SingleValue;

                var option = command.Option(template, nullSafeOptionAttribute.Description ?? "", optionType);

                _valueAccessor = () =>
                {
                    if (isBoolProperty)
                    {
                        return option.HasValue() ? true : (nullSafeOptionAttribute.DefaultValue ?? false);
                    }
                    else
                    {
                        return option.HasValue() ? option.Value() : (nullSafeOptionAttribute.DefaultValue ?? "");
                    }
                };
            }
            else
            {
                //And all other string properties are considered arguments by default
                //even if the ArgumentAttribute is not mentioned on them.
                var argumentAttribute = Property.GetArgumentAttribute();
                var description = argumentAttribute != null && !string.IsNullOrWhiteSpace(argumentAttribute.Description)
                    ? argumentAttribute.Description
                    : "";

                var argument = command.Argument(Property.Name, description);
                _valueAccessor = () => argument.Value;
            }
        }