public void MultipleSubParsers()
        {
            var parser = new ArgumentParser();

            parser.AddSubparsers(help: "Help 1");

            try
            {
                parser.AddSubparsers(help: "Help 2");
                Assert.Fail("ArgumentParseException expected");
            }
            catch (ArgumentParseException e)
            {
                Assert.AreEqual("cannot have multiple subparser arguments", e.Message);
            }
        }
        public void SubParserHelp()
        {
            var parser = new ArgumentParser();

            parser.ProgName    = "prog";
            parser.Description = "This is prog description.";
            parser.AddArgument("--foo", action: "toggle", help: "foo help");

            var subParsers = parser.AddSubparsers(help: "sub-command help");

            var parserA = subParsers.AddParser("a", help: "a help");

            parserA.AddArgument("bar", "single", help: "bar help");

            var parserB = subParsers.AddParser("b", help: "b help");

            parserB.AddArgument("--baz", "single", help: "baz help");

            var expected = @"Usage: prog [OPTION]... [ARG]...

This is prog description.

Positional arguments:
  command         sub-command help

Optional arguments:
  --foo           foo help".Replace("\r", string.Empty);

            Assert.AreEqual(expected, parser.HelpText);
        }
        public void SubParserParentArgumentOrder()
        {
            var parser = new ArgumentParser();

            parser.AddArgument("--foo", action: "toggle", help: "foo help");

            var subparsers = parser.AddSubparsers(help: "sub-command help");

            var parserA = subparsers.AddParser("a", help: "a help");

            parserA.AddArgument("bar", "single", help: "bar help");

            var parserB = subparsers.AddParser("b", help: "b help");

            parserB.AddArgument("--baz", "single", help: "baz help");

            try
            {
                var args = parser.ParseArgs(new string[] { "a", "1", "--foo" });
                Assert.Fail("ArgumentParseException expected");
            }
            catch (ArgumentParseException e)
            {
                Assert.AreEqual("Unrecognized arguments: --foo", e.Message);
            }
        }
        public void SubParserDefaults()
        {
            var parser = new ArgumentParser();

            parser.AddArgument("--foo", action: "toggle", help: "foo help");

            var subparsers = parser.AddSubparsers(help: "sub-command help");

            var parserA = subparsers.AddParser("a", help: "a help");

            parserA.AddArgument("bar", "single", help: "bar help");
            parserA.SetDefault("func", "FuncA()");

            var parserB = subparsers.AddParser("b", help: "b help");

            parserB.AddArgument("--baz", "single", help: "baz help");
            parserB.SetDefault("func", "FuncB()");

            var args = parser.ParseArgs(new string[] { "--foo", "b", "--baz", "1" });

            Assert.AreEqual("FuncB()", args["func"]);

            args = parser.ParseArgs(new string[] { "--foo", "a", "1" });
            Assert.AreEqual("FuncA()", args["func"]);
        }
        public void SubParsers()
        {
            var parser = new ArgumentParser();

            parser.AddArgument("--foo", action: "toggle", help: "foo help");

            var subparsers = parser.AddSubparsers(help: "sub-command help");

            var parserA = subparsers.AddParser("a", help: "a help");

            parserA.AddArgument("bar", "single", help: "bar help");

            var parserB = subparsers.AddParser("b", help: "b help");

            parserB.AddArgument("--baz", "single", help: "baz help");

            var args = parser.ParseArgs(new string[] { "--foo", "b", "--baz", "1" });

            Assert.AreEqual(2, args.Count);
            Assert.AreEqual(true, args["foo"]);
            Assert.AreEqual("1", args["baz"]);

            args = parser.ParseArgs(new string[] { "--foo", "a", "1" });
            Assert.AreEqual(2, args.Count);
            Assert.AreEqual(true, args["foo"]);
            Assert.AreEqual("1", args["bar"]);
        }
Пример #6
0
        public static void Run(string[] args)
        {
            var parser = new ArgumentParser();

            parser.AddArgument("--foo", action: "toggle", help: "foo help");

            var subparsers = parser.AddSubparsers(help: "sub-command help");

            var parserA = subparsers.AddParser("a", help: "a help");

            parserA.AddArgument("bar", "single", help: "bar help");

            var parserB = subparsers.AddParser("b", help: "b help");

            parserB.AddArgument("--baz", "single", help: "baz help");

            if (args.Contains("--help"))
            {
                Console.WriteLine(parser.HelpText);
                Environment.Exit(0);
            }

            IDictionary <string, dynamic> parsedArgs = null;

            try
            {
                parsedArgs = parser.ParseArgs(args);
            }
            catch (ArgumentParseException e)
            {
                Console.WriteLine($"Error: {e.Message}");
                Environment.Exit(2);
            }

            foreach (var entry in parsedArgs)
            {
                if (entry.Value is IList <string> )
                {
                    var s = string.Join(", ", entry.Value as IList <string>);
                    Console.WriteLine($"> '{entry.Key}': '{s}'");
                }
                else
                {
                    Console.WriteLine($"> '{entry.Key}': '{entry.Value}'");
                }
            }
        }