예제 #1
0
 internal ParserResult(T value, TypeInfo typeInfo)
 {
     Value         = value ?? throw new ArgumentNullException(nameof(value));
     this.tag      = ParserResultType.Parsed;
     this.typeInfo = typeInfo ?? TypeInfo.Create(value.GetType());
     Errors        = new Error[0];
 }
예제 #2
0
 internal ParserResult(ParserResultType tag, T value, IEnumerable <Error> errors, Maybe <IEnumerable <Type> > verbTypes)
 {
     this.tag       = tag;
     this.value     = value;
     this.errors    = errors;
     this.verbTypes = verbTypes;
 }
예제 #3
0
 internal ParserResult(IEnumerable <Error> errors, TypeInfo typeInfo)
 {
     this.tag      = ParserResultType.NotParsed;
     this.typeInfo = typeInfo ?? TypeInfo.Create(typeof(T));
     Errors        = errors ?? new Error[0];
     Value         = default;
 }
예제 #4
0
        internal ParserResult(ParserResultType tag, T value, IEnumerable <Error> errors, Maybe <IEnumerable <Type> > verbTypes)
        {
            if (object.Equals(value, default(T)))
            {
                throw new ArgumentNullException("value");
            }
            if (errors == null)
            {
                throw new ArgumentNullException("errors");
            }

            this.tag       = tag;
            this.value     = value;
            this.errors    = errors;
            this.verbTypes = verbTypes;
        }
예제 #5
0
        public static CommandOptions ParseCommandLine(string[] args)
        {
            try {
                CommandOptions   thisOptions = new CommandOptions();
                var              writer      = new StringWriter();
                var              parser      = new Parser(with => with.HelpWriter = writer);
                var              options     = parser.ParseArguments <CommandOptions>(args);
                ParserResultType tag         = options.Tag;

                if (tag == ParserResultType.NotParsed)
                {
                    thisOptions.ParserMessage = writer.ToString();
                    return(thisOptions);
                }


                options.WithParsed(opt => thisOptions = opt);  // An arcane way to set thisOptions because the CommandOptions library if going for functional style
                if (!IodClientMethods.TaskTypes.Keys.Contains(thisOptions.TaskType))
                {
                    var help = CommandLine.Text.HelpText.AutoBuild(parser.ParseArguments <CommandOptions>(new[] { "--help" }));
                    thisOptions.ParserMessage = $"Invalid task type -e {thisOptions.TaskType}\n{help}";
                    return(thisOptions);
                }
                if ((thisOptions.UserName == null || thisOptions.Password == null) && (thisOptions.UserName != null || thisOptions.Password != null))
                {
                    var help = CommandLine.Text.HelpText.AutoBuild(parser.ParseArguments <CommandOptions>(new[] { "--help" }));
                    thisOptions.ParserMessage = $"UserName and Password must both be specified or neither specified\n{help}";
                    return(thisOptions);
                }
                return(thisOptions);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error parsing command line arguments: ", ex);
            }
        }
예제 #6
0
        public void Enforce_required_within_mutually_exclusive_set_only(string[] arguments, ParserResultType type, int expected)
        {
            // Exercize system
            var result = InstanceBuilder.Build(
                Maybe.Just<Func<FakeOptionsWithTwoRequiredAndSets>>(() => new FakeOptionsWithTwoRequiredAndSets()),
                arguments,
                StringComparer.Ordinal,
                CultureInfo.InvariantCulture);

            // Verify outcome
            if (type == ParserResultType.NotParsed)
            {
                ((NotParsed<FakeOptionsWithTwoRequiredAndSets>)result).Errors.Should().HaveCount(x => x == expected);
            }
            else if (type == ParserResultType.Parsed)
            {
                result.Should().BeOfType<Parsed<FakeOptionsWithTwoRequiredAndSets>>();
            }
        }
예제 #7
0
        public void Empty_set_options_allowed_with_mutually_exclusive_sets(string[] arguments, ParserResultType type, int expected)
        {
            // Exercize system
            var result = InstanceBuilder.Build(
                Maybe.Just<Func<Options_With_Named_And_Empty_Sets>>(() => new Options_With_Named_And_Empty_Sets()),
                arguments,
                StringComparer.Ordinal,
                CultureInfo.InvariantCulture,
                Enumerable.Empty<ErrorType>());

            // Verify outcome
            if (type == ParserResultType.NotParsed)
            {
                ((NotParsed<Options_With_Named_And_Empty_Sets>)result).Errors.Should().HaveCount(x => x == expected);
            }
            else if (type == ParserResultType.Parsed)
            {
                result.Should().BeOfType<Parsed<Options_With_Named_And_Empty_Sets>>();
            }
        }
예제 #8
0
        public void Enforce_required_within_mutually_exclusive_set_only(string[] arguments, ParserResultType type, int expected)
        {
            // Exercize system
            var result = InvokeBuild <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets>(
                arguments);

            // Verify outcome
            if (type == ParserResultType.NotParsed)
            {
                ((NotParsed <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets>)result).Errors.Should().HaveCount(x => x == expected);
            }
            else if (type == ParserResultType.Parsed)
            {
                result.Should().BeOfType <Parsed <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets> >();
            }
        }
예제 #9
0
        public void Empty_set_options_allowed_with_mutually_exclusive_sets(string[] arguments, ParserResultType type, int expected)
        {
            // Exercize system
            var result = InvokeBuild <Options_With_Named_And_Empty_Sets>(
                arguments);

            // Verify outcome
            if (type == ParserResultType.NotParsed)
            {
                ((NotParsed <Options_With_Named_And_Empty_Sets>)result).Errors.Should().HaveCount(x => x == expected);
            }
            else if (type == ParserResultType.Parsed)
            {
                result.Should().BeOfType <Parsed <Options_With_Named_And_Empty_Sets> >();
            }
        }
예제 #10
0
        public void Enforce_required_within_mutually_exclusive_set_only(string[] arguments, ParserResultType type, int expected)
        {
            // Exercize system
            var result = InstanceBuilder.Build(
                Maybe.Just <Func <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets> >(() => new Options_With_Two_Option_Required_Set_To_True_And_Two_Sets()),
                arguments,
                StringComparer.Ordinal,
                CultureInfo.InvariantCulture,
                Enumerable.Empty <ErrorType>());

            // Verify outcome
            if (type == ParserResultType.NotParsed)
            {
                ((NotParsed <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets>)result).Errors.Should().HaveCount(x => x == expected);
            }
            else if (type == ParserResultType.Parsed)
            {
                result.Should().BeOfType <Parsed <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets> >();
            }
        }
예제 #11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="tag"></param>
 internal ParserResult(ParserResultType tag, T value)
 {
     Tag   = tag;
     Value = value;
 }
예제 #12
0
 internal ParserResult(ParserResultType tag, T value, IEnumerable <Type> verbTypes)
 {
     this.tag       = tag;
     this.value     = value;
     this.verbTypes = verbTypes;
 }
예제 #13
0
        public void Empty_set_options_allowed_with_mutually_exclusive_sets(string[] arguments, ParserResultType type, int expected)
        {
            // Exercize system
            var result = InstanceBuilder.Build(
                Maybe.Just <Func <FakeOptionsWithNamedAndEmptySets> >(() => new FakeOptionsWithNamedAndEmptySets()),
                arguments,
                StringComparer.Ordinal,
                CultureInfo.InvariantCulture);

            // Verify outcome
            if (type == ParserResultType.NotParsed)
            {
                ((NotParsed <FakeOptionsWithNamedAndEmptySets>)result).Errors.Should().HaveCount(x => x == expected);
            }
            else if (type == ParserResultType.Parsed)
            {
                result.Should().BeOfType <Parsed <FakeOptionsWithNamedAndEmptySets> >();
            }
        }
예제 #14
0
 internal ParserResult(ParserResultType tag, TypeInfo typeInfo)
 {
     this.tag      = tag;
     this.typeInfo = typeInfo;
 }