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); }
public bool ChangesSubject(IParserResult subject) { return(Comparer <T> .Default.Compare( Value, subject.GetValue(Target)) != 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)); } }
private object GetValueForArgument(IArgument arg) { if (_pr.IsSet(arg)) { return(arg.Parse(_pr.GetValue(arg), true)); } // if( !arg.Optional ) // throw new Exception( $"non-optional argument {arg.Name} not set in parser result" ); return(null); }
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( ))); }
public override void Apply(IParserResult subject, IBehaviors context = null) { Target.Create(subject.GetValue(Target)); }