FindMatchingArgument() public method

Finds the first CommandLineArgument that matches the given key.
public FindMatchingArgument ( string key, bool throwIfMoreThanOneMatch = false ) : CommandLineArgument
key string The key as if it was typed in on the command line. This can also be an alias.
throwIfMoreThanOneMatch bool If set to true then this method will throw and InvalidArgDeginitionException if more than 1 match is found
return CommandLineArgument
Example #1
0
        private static bool IsBool(string key, CommandLineArgumentsDefinition definition, ParseResult resultContext)
        {
            var match = definition.FindMatchingArgument(key, true);

            if (match == null)
            {
                var possibleActionContext = resultContext.ImplicitParameters.ContainsKey(0) ? resultContext.ImplicitParameters[0] : null;

                if (possibleActionContext == null)
                {
                    return(false);
                }
                else
                {
                    var actionContext = definition.FindMatchingAction(possibleActionContext, true);
                    if (actionContext == null)
                    {
                        return(false);
                    }

                    match = actionContext.FindMatchingArgument(key, true);
                    if (match == null)
                    {
                        return(false);
                    }
                }
            }

            return(match.ArgumentType == typeof(bool));
        }
Example #2
0
 /// <summary>
 /// Finds the first CommandLineArgument that matches the given key.
 /// </summary>
 /// <param name="key">The key as if it was typed in on the command line.  This can also be an alias. </param>
 /// <param name="throwIfMoreThanOneMatch">If set to true then this method will throw and InvalidArgDeginitionException if more than 1 match is found</param>
 /// <returns>The first argument that matches the key.</returns>
 public CommandLineArgument FindMatchingArgument(string key, bool throwIfMoreThanOneMatch = false)
 {
     return(CommandLineArgumentsDefinition.FindMatchingArgument(key, throwIfMoreThanOneMatch, this.Arguments));
 }
Example #3
0
        private static bool IsBool(string key, CommandLineArgumentsDefinition definition, ParseResult resultContext)
        {
            var match = definition.FindMatchingArgument(key, true);
            if (match == null)
            {
                var possibleActionContext = resultContext.ImplicitParameters.ContainsKey(0) ? resultContext.ImplicitParameters[0] : null;

                if (possibleActionContext == null)
                {
                    return false;
                }
                else
                {
                    var actionContext = definition.FindMatchingAction(possibleActionContext, true);
                    if (actionContext == null)
                    {
                        return false;
                    }

                    match = actionContext.FindMatchingArgument(key, true);
                    if (match == null)
                    {
                        return false;
                    }
                }

            }

            return match.ArgumentType == typeof(bool);
        }
 public void TestNullableIsEnum()
 {
     var def = new CommandLineArgumentsDefinition(typeof(NullableArgs));
     Assert.IsTrue(def.FindMatchingArgument("OptionalDayOfWeek").IsEnum);
 }
Example #5
0
        public void TestOverrideDefaultReviver()
        {
            var intReviver = ArgRevivers.GetReviver(typeof(int));
            try
            {
                ArgRevivers.SetReviver<int>((k, v) => { return -1; });

                var def = new CommandLineArgumentsDefinition();
                def.Arguments.Add(new CommandLineArgument(typeof(int), "theInt"));
                Args.Parse(def, "-theInt", "100");
                Assert.AreEqual(-1, def.FindMatchingArgument("theInt").RevivedValue);
            }
            finally
            {
                ArgRevivers.SetReviver(typeof(int), intReviver);
            }

            // make sure the old reviver is working again
            var newDef = new CommandLineArgumentsDefinition();
            newDef.Arguments.Add(new CommandLineArgument(typeof(int), "theInt"));
            Args.Parse(newDef, "-theInt", "100");
            Assert.AreEqual(100, newDef.FindMatchingArgument("theInt").RevivedValue);
        }
        public void TestREPLReset()
        {
            SetupCleanup.ClearAttributeCache();
            var provider = TestConsoleProvider.SimulateConsoleInput("-b{enter}-s a{enter}quit");
            var def = new CommandLineArgumentsDefinition(typeof(TestArgsWithREPL));
            var boolArg = def.FindMatchingArgument("b");
            Assert.IsNotNull(boolArg);

            bool first = true;
            bool validatedBoth = false;
            var interceptor = new AfterInvokeInterceptor((context) =>
            {
                if (first)
                {
                    first = false;
                    Assert.IsTrue((bool)boolArg.RevivedValue);
                }
                else
                {
                    Assert.IsNull(boolArg.RevivedValue);
                    validatedBoth = true;
                }
            });

            def.Metadata.Add(interceptor);

            var finalResult = Args.InvokeMain(def, "$");
            Assert.IsTrue(validatedBoth);
            Assert.IsFalse((finalResult.Value as TestArgsWithREPL).BoolParam);
        }