コード例 #1
0
        private static string ShellPrefixesToMatch(ISuggestionRegistration suggestionProvider)
        {
            var registrations = suggestionProvider.FindAllRegistrations();

            return(string.Join(Environment.NewLine, Prefixes()));

            IEnumerable <string> Prefixes()
            {
                foreach (var r in registrations)
                {
                    var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(
                        r.ExecutablePath
                        );

                    yield return(fileNameWithoutExtension);

                    if (
                        fileNameWithoutExtension?.StartsWith("dotnet-", StringComparison.Ordinal)
                        == true
                        )
                    {
                        yield return("dotnet "
                                     + fileNameWithoutExtension.Substring("dotnet-".Length));
                    }
                }
            }
        }
コード例 #2
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();
        }
コード例 #3
0
        private static async Task <string> InvokeAsync(
            string[] args,
            ISuggestionRegistration suggestionProvider)
        {
            var dispatcher  = new SuggestionDispatcher(suggestionProvider, new TestSuggestionStore());
            var testConsole = new TestConsole();
            await dispatcher.InvokeAsync(args, testConsole);

            return(testConsole.Out.ToString());
        }
コード例 #4
0
        public void Suggestion_command_path_is_not_case_sensitive()
        {
            ISuggestionRegistration suggestionProvider = GetSuggestionRegistration();

            suggestionProvider.AddSuggestionRegistration(
                new Registration(Path.GetFullPath("commandPath")));

            Registration registration = suggestionProvider.FindRegistration(new FileInfo("COMMANDPATH"));

            registration.ExecutablePath.Should().Be(Path.GetFullPath("commandPath"));
        }
コード例 #5
0
        public void When_duplicate_suggestions_are_registered_the_last_one_is_used()
        {
            ISuggestionRegistration suggestionProvider = GetSuggestionRegistration();

            suggestionProvider.AddSuggestionRegistration(
                new RegistrationPair(Path.GetFullPath("commandPath"), "suggestionCommand2"));

            suggestionProvider.AddSuggestionRegistration(
                new RegistrationPair(Path.GetFullPath("commandPath"), "suggestionCommand2"));

            RegistrationPair registration = suggestionProvider.FindRegistration(new FileInfo("commandPath"));

            registration.CommandPath.Should().Be(Path.GetFullPath("commandPath"));
            registration.SuggestionCommand.Should().Be("suggestionCommand2");
        }
コード例 #6
0
        public void Added_suggestions_can_be_retrieved()
        {
            ISuggestionRegistration suggestionProvider = GetSuggestionRegistration();

            var suggestion1 = new Registration("commandPath1");
            var suggestion2 = new Registration("commandPath2");

            suggestionProvider.AddSuggestionRegistration(suggestion1);
            suggestionProvider.AddSuggestionRegistration(suggestion2);

            var allRegistrations = suggestionProvider.FindAllRegistrations();

            allRegistrations
            .Should()
            .HaveCount(2)
            .And.Contain(x => x.ExecutablePath == suggestion1.ExecutablePath)
            .And.Contain(x => x.ExecutablePath == suggestion2.ExecutablePath);
        }
コード例 #7
0
        private static string List(
            ISuggestionRegistration suggestionProvider,
            bool detailed = false)
        {
            var registrations = suggestionProvider.FindAllRegistrations();

            if (detailed)
            {
                return(string.Join(Environment.NewLine,
                                   registrations.Select(r => $"{r.CommandPath} --> {r.SuggestionCommand}")));
            }
            else
            {
                return(string.Join(" ", registrations
                                   .Select(suggestionRegistration => suggestionRegistration.CommandPath)
                                   .Select(Path.GetFileNameWithoutExtension)));
            }
        }
コード例 #8
0
        public void Added_suggestions_can_be_retrieved()
        {
            ISuggestionRegistration suggestionProvider = GetSuggestionRegistration();

            var suggestion1 = new RegistrationPair("commandPath1", "suggestionCommand1");
            var suggestion2 = new RegistrationPair("commandPath2", "suggestionCommand2");

            suggestionProvider.AddSuggestionRegistration(suggestion1);
            suggestionProvider.AddSuggestionRegistration(suggestion2);

            var allRegistrations = suggestionProvider.FindAllRegistrations();

            allRegistrations
            .Should()
            .HaveCount(2)
            .And
            .Contain(x =>
                     x.CommandPath == suggestion1.CommandPath && x.SuggestionCommand == suggestion1.SuggestionCommand)
            .And
            .Contain(x =>
                     x.CommandPath == suggestion2.CommandPath && x.SuggestionCommand == suggestion2.SuggestionCommand);
        }
コード例 #9
0
        public void Missing_suggestion_can_not_be_found()
        {
            ISuggestionRegistration suggestionProvider = GetSuggestionRegistration();

            var path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            try
            {
                Directory.CreateDirectory(path);
                var unregisteredFile = Path.Combine(path, "im-not-registered");
                File.WriteAllText(unregisteredFile, "");

                var foundRegistration = suggestionProvider.FindRegistration(
                    new FileInfo(unregisteredFile)
                    );

                foundRegistration.Should().BeNull();
            }
            finally
            {
                Directory.Delete(path, true);
            }
        }
コード例 #10
0
        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>());
        }
コード例 #11
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();
        }
コード例 #12
0
        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"
            };
        }