Example #1
0
        private bool ReadHelp(PropertyInfo pi, out object value)
        {
            value = null;
            HelpAttribute ha = pi.GetCustomAttribute <HelpAttribute>( );

            if (ha == null)
            {
                return(false);
            }

            value = _pr.IsSet(_pr.CliSpecification.HelpOption);
            return(true);
        }
Example #2
0
        public override IViolation <IParserResult> Check(IParserResult subject, IBehaviors context = null)
        {
            if (!subject.IsSet(Target))
            {
                return(NonViolation <IParserResult> .Instance);
            }

            try {
                Target.Parse(subject.GetValue((IArgument)Target), true);
            } catch (Exception exc) {
                return(new Violation <IParserResult>(
                           this,
                           new List <ISolution <IParserResult> > {
                    new Solution <IParserResult>(
                        new[] {
                        new PromptValue <T>(Target)
                    })
                }.Union(
                               Target.HasDefaultValue || Target.ValueType.IsValueType
                              ? new List <ISolution <IParserResult> > {
                    new Solution <IParserResult>(
                        new IAction <IParserResult>[] {
                        new SetValue <T>(
                            Target,
                            Target.HasDefaultValue
                                                            ? Target.DefaultValue
                                                            : Activator.CreateInstance <T>( ))
                    })
                }
                              : Enumerable.Empty <ISolution <IParserResult> >( )),
                           $"invalid value: {exc.Message}"));
            }

            return(NonViolation <IParserResult> .Instance);
        }
Example #3
0
        public void AutoAddedHelpOption_IsNotSet( )
        {
            ICliSpecification spec = new CliSpecification( );
            CommandLineParser clp  = new CommandLineParser(spec);
            IParserResult     pr   = clp.Parse(new string[0]);

            Assert.IsNotNull(spec.HelpOption);
            Assert.IsFalse(pr.IsSet(spec.HelpOption));
        }
Example #4
0
        public void AutoAddedHelpOption_IsSetFlag( )
        {
            ICliSpecification spec = new CliSpecification( );
            CommandLineParser clp  = new CommandLineParser(spec);
            IParserResult     pr   = clp.Parse(new[] { "-h" });

            Assert.IsNotNull(spec.HelpOption);
            Assert.IsTrue(pr.IsSet(spec.HelpOption));
        }
Example #5
0
        public void TestTemplate1(
            string[] args,
            bool arg1ShouldBeSet,
            bool arg2ShouldBeSet,
            bool optShouldBeSet,
            bool flagShouldBeSet,
            string arg1ShouldBe = null,
            string arg2ShouldBe = null)
        {
            IArgument        arg1 = new Argument <string>("Argument 1", null);
            IArgument        arg2 = new Argument <string>("Argument 2", null);
            CliSpecification spec = new CliSpecification(
                args: new[] {
                arg1
            });
            IFlag flag = new Flag('x');

            spec.Flags.Add(flag);
            IOption opt = new Option(
                new UnifiedName("my", "opt"),
                true,
                'm',
                null,
                arg2);

            spec.Options.Add(opt);

            ICommandLineParser clp = new CommandLineParser(spec);

            IParserResult pr = clp.Parse(args);

            Assert.AreEqual(arg1ShouldBeSet, pr.IsSet(arg1));
            Assert.AreEqual(arg2ShouldBeSet, pr.IsSet(arg2));
            Assert.AreEqual(optShouldBeSet, pr.IsSet(opt));
            Assert.AreEqual(flagShouldBeSet, pr.IsSet(flag));
            if (arg1ShouldBe != null)
            {
                Assert.AreEqual(arg1ShouldBe, pr.GetValue(arg1));
            }
            if (arg2ShouldBe != null)
            {
                Assert.AreEqual(arg2ShouldBe, pr.GetValue(arg2));
            }
        }
Example #6
0
        public void OptionCaseSensitivityCorrectCase( )
        {
            ICliSpecification spec = new CliSpecification( );
            IOption           opt  = new Option(new UnifiedName("opt"));

            spec.Options.Add(opt);
            CommandLineParser clp = new CommandLineParser(spec);
            IParserResult     pr  = clp.Parse(new[] { "--opt" });

            Assert.IsTrue(pr.IsSet(opt));
        }
Example #7
0
        public void FlagCaseSensitivityCorrectCase( )
        {
            ICliSpecification spec = new CliSpecification( );
            IFlag             flag = new Flag('f');

            spec.Flags.Add(flag);
            CommandLineParser clp = new CommandLineParser(spec);
            IParserResult     pr  = clp.Parse(new[] { "-f" });

            Assert.IsTrue(pr.IsSet(flag));
        }
Example #8
0
        public void OptionCaseSensitivityIgnoreCaseUpperUpper( )
        {
            ICliConfig conf = Config.New <ICliConfig>( );

            conf.CaseSensitiveFlags = false;
            ICliSpecification spec = new CliSpecification(conf);
            IFlag             flag = new Flag('F');

            spec.Flags.Add(flag);
            CommandLineParser clp = new CommandLineParser(spec);
            IParserResult     pr  = clp.Parse(new[] { "-F" });

            Assert.IsTrue(pr.IsSet(flag));
        }
Example #9
0
        public void OptionCaseSensitivityIgnoreCaseUpper( )
        {
            ICliConfig conf = Config.New <ICliConfig>( );

            conf.CaseSensitiveOptions = false;
            ICliSpecification spec = new CliSpecification(conf);
            IOption           opt  = new Option(new UnifiedName("opt"));

            spec.Options.Add(opt);
            CommandLineParser clp = new CommandLineParser(spec);
            IParserResult     pr  = clp.Parse(new[] { "--OPT" });

            Assert.IsTrue(pr.IsSet(opt));
        }
Example #10
0
        public override IViolation <IParserResult> Check(IParserResult subject, IBehaviors context = null)
        {
            try {
                if (!subject.IsSet(Target) ||
                    Comparer <T> .Default.Compare(subject.GetValue(Target), MaxVal) <= 0)
                {
                    return(NonViolation <IParserResult> .Instance);
                }
            } catch (Exception) {
                // the value might simply be invalid at this point
                return(NonViolation <IParserResult> .Instance);
            }

            return(new Violation <IParserResult>(
                       this,
                       MakeValid( )));
        }