Beispiel #1
0
        public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISuggestionStore suggestionStore = null)
        {
            _suggestionRegistration = suggestionRegistration ?? throw new ArgumentNullException(nameof(suggestionRegistration));

            _suggestionStore = suggestionStore ?? new SuggestionStore();

            CompleteScriptCommand = new Command("script", "Print complete script for specific shell")
            {
                new Argument <ShellType>
                {
                    Name = nameof(ShellType)
                }
            };
            CompleteScriptCommand.Handler = CommandHandler.Create <IConsole, ShellType>(SuggestionShellScriptHandler.Handle);

            ListCommand = new Command("list")
            {
                Description = "Lists apps registered for suggestions",
                Handler     = CommandHandler.Create <IConsole>(
                    c => c.Out.WriteLine(ShellPrefixesToMatch(_suggestionRegistration)))
            };

            GetCommand = new Command("get", "Gets suggestions from the specified executable")
            {
                ExecutableOption,
                PositionOption
            };
            GetCommand.Handler = CommandHandler.Create <ParseResult, IConsole>(Get);

            RegisterCommand = new Command("register", "Registers an app for suggestions")
            {
                new Option <string>("--command-path", "The path to the command for which to register suggestions"),
                new Option <string>("--suggestion-command", "The command to invoke to retrieve suggestions")
            };

            RegisterCommand.Handler = CommandHandler.Create <string, string, IConsole>(Register);

            var root = new RootCommand
            {
                ListCommand,
                GetCommand,
                RegisterCommand,
                CompleteScriptCommand
            };

            Parser = new CommandLineBuilder(root)
                     .EnableLegacyDoubleDashBehavior()
                     .UseVersionOption()
                     .UseHelp()
                     .UseParseDirective()
                     .UseDebugDirective()
                     .UseSuggestDirective()
                     .UseParseErrorReporting()
                     .UseExceptionHandler()
                     .Build();
        }
Beispiel #2
0
        private static async Task <string> InvokeAsync(
            string[] args,
            ISuggestionRegistration suggestionProvider,
            ISuggestionStore suggestionStore = null)
        {
            var dispatcher  = new SuggestionDispatcher(suggestionProvider, suggestionStore ?? new TestSuggestionStore());
            var testConsole = new TestConsole();
            await dispatcher.InvokeAsync(args, testConsole);

            return(testConsole.Out.ToString());
        }
        public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISuggestionStore suggestionStore = null)
        {
            _suggestionRegistration = suggestionRegistration ?? throw new ArgumentNullException(nameof(suggestionRegistration));
            _suggestionStore        = suggestionStore ?? new SuggestionStore();

            _parser = new CommandLineBuilder()
                      .UseVersionOption()
                      .UseHelp()
                      .UseParseDirective()
                      .UseDebugDirective()
                      .UseSuggestDirective()
                      .UseParseErrorReporting()
                      .UseExceptionHandler()

                      .AddCommand(ListCommand())
                      .AddCommand(GetCommand())
                      .AddCommand(RegisterCommand())
                      .AddCommand(CompleteScriptCommand())

                      .Build();

            Command GetCommand() =>
            new Command("get",
                        "Gets suggestions from the specified executable",
                        new[] { ExecutableOption(), PositionOption() })
            {
                Handler = CommandHandler.Create <ParseResult, IConsole>(Get)
            };

            Option ExecutableOption() =>
            new Option(new[] { "-e", "--executable" },
                       "The executable to call for suggestions",
                       new Argument <string>().LegalFilePathsOnly());

            Option PositionOption() =>
            new Option(new[] { "-p", "--position" },
                       "The current character position on the command line",
                       new Argument <int>());

            Command ListCommand() =>
            new Command(
                "list",
                "Lists apps registered for suggestions",
                new[] { DetailedOption() })
            {
                Handler = CommandHandler.Create <IConsole, bool>(
                    (c, detailed) => c.Out.WriteLine(List(_suggestionRegistration, detailed)))
            };

            Command CompleteScriptCommand() =>
            new Command(
                "complete-script",
                "Print complete script for specific shell",
                new[]
            {
                new Option("--shell",
                           "Shell name",
                           new Argument <string>())
            })
            {
                Handler = CommandHandler.Create <IConsole, string>(
                    SuggestionShellScriptHandler.Handle)
            };

            Option DetailedOption() =>
            new Option("--detailed",
                       "Provides detailed output about registered completions",
                       new Argument <bool>());

            Command RegisterCommand() =>
            new Command("register",
                        "Registers an app for suggestions",
                        new[] { CommandPathOption(), SuggestionCommandOption() })
            {
                Handler = CommandHandler.Create <string, string, IConsole>(Register)
            };

            Option CommandPathOption() =>
            new Option("--command-path",
                       "The path to the command for which to register suggestions",
                       new Argument <string>());

            Option SuggestionCommandOption() =>
            new Option("--suggestion-command",
                       "The command to invoke to retrieve suggestions",
                       new Argument <string>());
        }
Beispiel #4
0
        public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISuggestionStore suggestionStore = null)
        {
            _suggestionRegistration = suggestionRegistration ?? throw new ArgumentNullException(nameof(suggestionRegistration));

            _suggestionStore = suggestionStore ?? new SuggestionStore();

            var shellTypeArgument = new Argument <ShellType>
            {
                Name = nameof(ShellType)
            };

            CompleteScriptCommand = new Command("script", "Print complete script for specific shell")
            {
                shellTypeArgument
            };
            CompleteScriptCommand.SetHandler(context =>
            {
                SuggestionShellScriptHandler.Handle(context.Console, context.ParseResult.GetValueForArgument(shellTypeArgument));
            });

            ListCommand = new Command("list")
            {
                Description = "Lists apps registered for suggestions",
            };
            ListCommand.SetHandler(ctx =>
            {
                ctx.Console.Out.WriteLine(ShellPrefixesToMatch(_suggestionRegistration));
                return(Task.FromResult(0));
            });

            GetCommand = new Command("get", "Gets suggestions from the specified executable")
            {
                ExecutableOption,
                PositionOption
            };
            GetCommand.SetHandler(context => Get(context));

            var commandPathOption = new Option <string>("--command-path", "The path to the command for which to register suggestions");

            RegisterCommand = new Command("register", "Registers an app for suggestions")
            {
                commandPathOption,
                new Option <string>("--suggestion-command", "The command to invoke to retrieve suggestions")
            };

            RegisterCommand.SetHandler(context =>
            {
                Register(context.ParseResult.GetValueForOption(commandPathOption), context.Console);
                return(Task.FromResult(0));
            });

            var root = new RootCommand
            {
                ListCommand,
                GetCommand,
                RegisterCommand,
                CompleteScriptCommand
            };

            Parser = new CommandLineBuilder(root)
                     .EnableLegacyDoubleDashBehavior()
                     .UseVersionOption()
                     .UseHelp()
                     .UseParseDirective()
                     .UseSuggestDirective()
                     .UseParseErrorReporting()
                     .UseExceptionHandler()
                     .Build();
        }
        public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISuggestionStore suggestionStore = null)
        {
            _suggestionRegistration = suggestionRegistration ?? throw new ArgumentNullException(nameof(suggestionRegistration));
            _suggestionStore        = suggestionStore ?? new SuggestionStore();

            Parser = new CommandLineBuilder()
                     .UseVersionOption()
                     .UseHelp()
                     .UseParseDirective()
                     .UseDebugDirective()
                     .UseSuggestDirective()
                     .UseParseErrorReporting()
                     .UseExceptionHandler()

                     .AddCommand(ListCommand())
                     .AddCommand(GetCommand())
                     .AddCommand(RegisterCommand())
                     .AddCommand(CompleteScriptCommand())

                     .Build();

            Command GetCommand()
            {
                var command = new Command("get")
                {
                    ExecutableOption(),
                    PositionOption()
                };

                command.Description = "Gets suggestions from the specified executable";
                command.Handler     = CommandHandler.Create <ParseResult, IConsole>(Get);
                return(command);
            }

            Option ExecutableOption() =>
            new Option(new[] { "-e", "--executable" })
            {
                Argument = new Argument <string>().LegalFilePathsOnly(), Description = "The executable to call for suggestions"
            };

            Option PositionOption() =>
            new Option(new[] { "-p", "--position" })
            {
                Argument    = new Argument <int>(),
                Description = "The current character position on the command line"
            };

            Command ListCommand() =>
            new Command("list")
            {
                Description = "Lists apps registered for suggestions",
                Handler     = CommandHandler.Create <IConsole>(
                    c => c.Out.WriteLine(ShellPrefixesToMatch(_suggestionRegistration)))
            };

            Command CompleteScriptCommand() =>
            new Command("script")
            {
                Argument = new Argument <ShellType>
                {
                    Name = nameof(ShellType)
                },
                Description = "Print complete script for specific shell",
                Handler     = CommandHandler.Create <IConsole, ShellType>(SuggestionShellScriptHandler.Handle)
            };

            Command RegisterCommand()
            {
                var description = "Registers an app for suggestions";

                var command = new Command("register")
                {
                    Description = description,
                    Handler     = CommandHandler.Create <string, string, IConsole>(Register)
                };

                command.Add(CommandPathOption());
                command.Add(SuggestionCommandOption());
                return(command);
            }

            Option CommandPathOption() =>
            new Option("--command-path")
            {
                Argument    = new Argument <string>(),
                Description = "The path to the command for which to register suggestions"
            };

            Option SuggestionCommandOption() =>
            new Option("--suggestion-command")
            {
                Argument    = new Argument <string>(),
                Description = "The command to invoke to retrieve suggestions"
            };
        }