Ejemplo n.º 1
0
    public static int Main(string[] args)
    {
        var commands = new CommandSet("commands")
        {
            "usage: commands COMMAND [OPTIONS]",
            "",
            "Mono.Options.CommandSet sample app.",
            "",
            "Global options:",
            { "v:",
              "Output verbosity.",
              (int?n) => Verbosity = n.HasValue ? n.Value : Verbosity + 1 },
            "",
            "Available commands:",
            new Command("echo", "Echo arguments to the screen")
            {
                Run = ca => Console.WriteLine("{0}", string.Join(" ", ca)),
            },
            new Command("equinox", "Does something with the equinox?")
            {
                Run = ca => Console.WriteLine("{0}", string.Join(" ", ca)),
            },
            new RequiresArgsCommand(),
            "Commands with spaces are supported:",
            new Command("has spaces", "spaces?!")
            {
                Run = ca => Console.WriteLine("spaces, yo! {0}", string.Join(" ", ca)),
            },
            "Nested CommandSets are also supported. They're invoked similarly to commands with spaces.",
            new CommandSet("set")
            {
                new Command("file type", "Does something or other.")
                {
                    Run = ca => Console.WriteLine("File type set to: {0}", string.Join(" ", ca)),
                },
                new Command("output", "Sets  output location")
                {
                    Run = ca => Console.WriteLine("Output set to: {0}", string.Join(" ", ca)),
                },
            },
        };

        commands.Add(new Command("completions", "Show CommandSet completions")
        {
            Run = ca => {
                var start = ca.Any() ? string.Join(" ", ca) : "";
                Console.WriteLine($"Showing CommandSet completions for prefix '{start}':");
                foreach (var completion in commands.GetCompletions(start))
                {
                    Console.WriteLine($"\tcompletion: {completion}");
                }
            },
        });
        commands.Add(commands);
        return(commands.Run(args));
    }
Ejemplo n.º 2
0
        public void GetCompletions()
        {
            var commands = new CommandSet("example")
            {
                new Command("a"),
                new Command("aa"),
                new Command("a a"),
                new Command("cs c"),
                new CommandSet("cs")
                {
                    new CommandSet("cs2")
                    {
                        new CommandSet("cs3")
                        {
                            new Command("cs-cs2-cs3-c"),
                        },
                    },
                },
            };

            Assert.IsTrue(new[] {
                "a",
                "aa",
                "a a",
                "cs c",
                "cs cs2 cs3 cs-cs2-cs3-c",
            }.SequenceEqual(commands.GetCompletions()));

            Assert.IsTrue(new[] {
                "a",
                "aa",
                "a a",
            }.SequenceEqual(commands.GetCompletions("a")));

            Assert.IsTrue(new[] {
                "cs c",
                "cs cs2 cs3 cs-cs2-cs3-c",
            }.SequenceEqual(commands.GetCompletions("cs")));
        }