public void MissingParameterTest()
        {
            ParameterSetCollection sets = ParameterSetCollection.FromTypes(typeof(ParamSet));
            NodeSequence args = this.Parse("");
            ResolveResult result = sets.Resolve(new ParameterSetBinder(),
                                                DefaultActivator.Instance,
                                                new IntransigentTypeConverter(),
                                                CultureInfo.InvariantCulture,
                                                args);

            Assert.False(result.IsMatch, "Expects no match");
            Assert.Equal(1, result.Count);
            Assert.Equal(1, result.BestMatch.Errors.Length);
            ErrorWriter writer = new ErrorWriter();
            using (StringWriter strWriter = new StringWriter())
            {
                writer.Write(strWriter, result);
                string output = strWriter.ToString();
                Assert.Contains("Missing requried paramter: 'ABool'.", output);
                Assert.Contains("A bool", output);
                Assert.Contains("Required?                True", output);
                Assert.Contains("Position?                named", output);
                Assert.Contains("Default value            None", output);
                Assert.Contains("Parameter sets           Set1", output);
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            /*
             *  simple usage
             */

            // set up argument parser
            ArgumentParser<ExecuteCommandBase> argParser = new ArgumentParser<ExecuteCommandBase>(typeof(ExecuteCommandUsingName), typeof(ExecuteCommandUsingPath));
            ExecuteCommandBase paramSet;
            if (argParser.TryParse(args, out paramSet))
            {
                paramSet.Execute();
            }

            Console.ReadLine();

            /*
            *  advanced usage
            */

            // create parameter set collection from types
            ParameterSetCollection sets = ParameterSetCollection.FromTypes(typeof(ExecuteCommandUsingName), typeof(ExecuteCommandUsingPath));

            // parse the command line arguments
            Parser parser = new Parser();
            NodeSequence nodes = parser.Parse(string.Join(" ", args));

            // resolve parameter set against the parsed node set
            ResolveResult result = sets.Resolve(new ParameterSetBinder(),
                                                DefaultActivator.Instance,
                                                new IntransigentTypeConverter(),
                                                CultureInfo.InvariantCulture,
                                                nodes);
            if (result.IsMatch)
            {
                paramSet = (ExecuteCommandBase)result.BestMatch.Object;
                paramSet.Execute();
            }
            else
            {
                ErrorWriter errorWriter = new ErrorWriter();
                errorWriter.Write(new ConsoleWriter(Console.Error), result);

                HelpWriter helpWriter = new HelpWriter();
                helpWriter.Write(new ConsoleWriter(Console.Out), sets, HelpLevel.Parameters);
            }
        }