示例#1
0
        internal static OptionResult CreateImplicit(
            IOption option,
            CommandResult parent)
        {
            var result = new OptionResult(option,
                                          option.Token());

            result.IsImplicit = true;

            if (option.Argument.HasDefaultValue)
            {
                var value = option.Argument.GetDefaultValue();

                switch (value)
                {
                case string arg:
                    result.TryTakeToken(new Token(arg, TokenType.Argument));
                    break;

                default:
                    result.Result = ArgumentParseResult.Success(value);
                    break;
                }
            }

            return(result);
        }
示例#2
0
        public void Custom_types_and_conversion_logic_can_be_specified()
        {
            var argument = new Argument <MyCustomType>(parsed =>
            {
                var custom = new MyCustomType();
                foreach (var a in parsed.Arguments)
                {
                    custom.Add(a);
                }

                return(ArgumentParseResult.Success(custom));
            })
            {
                Arity = ArgumentArity.ZeroOrMore
            };

            var parser = new Parser(
                new Command("custom", "",
                            argument: argument));

            var result = parser.Parse("custom one two three");

            var customType = result.CommandResult.GetValueOrDefault <MyCustomType>();

            customType
            .Values
            .Should()
            .BeEquivalentTo("one", "two", "three");
        }
示例#3
0
        public void Argument_defaults_arity_to_One_for_non_IEnumerable_types()
        {
            var argument = new Argument <int>(s => ArgumentParseResult.Success(1));

            argument.Arity.Should().BeEquivalentTo(ArgumentArity.ExactlyOne);
        }