A class that provides a framework for building interactive command line interfaces.
        public void TestBasicAssistEndOfLineAfterASpace()
        {
            ConsoleProvider.Current = new TestConsoleProvider("choice: {control} {w}{down}{down}{enter}");
            CliHelper cli = new CliHelper();
            var picker = new ContextAssistPicker();
            picker.Options.Add(ContextAssistSearchResult.FromString("Option 1"));
            picker.Options.Add(ContextAssistSearchResult.FromString("Option 2"));
            picker.Options.Add(ContextAssistSearchResult.FromString("Option 3"));
            cli.Reader.ContextAssistProvider = picker;

            var line = cli.Reader.ReadLine();
            Assert.AreEqual("choice: Option 3", line.ToString());
        }
        public void TestBasicAssistMiddleOfLineReplacingCurrentToken()
        {
            ConsoleProvider.Current = new TestConsoleProvider("choice: abc after{left}{left}{left}{left}{left}{left}{control} {w}{down}{down}{enter}");
            CliHelper cli = new CliHelper();
            var picker = new ContextAssistPicker();
            picker.Options.Add(ContextAssistSearchResult.FromString("Option 1"));
            picker.Options.Add(ContextAssistSearchResult.FromString("Option 2"));
            picker.Options.Add(ContextAssistSearchResult.FromString("Option 3"));
            cli.Reader.ContextAssistProvider = picker;

            var line = cli.Reader.ReadLine();
            Assert.AreEqual("choice: Option 3 after", line.ToString());
        }
Exemplo n.º 3
0
        public void IsUserSure()
        {
            ConsoleProvider.Current = new TestConsoleProvider("y{enter}n{enter}notagoodanswer{enter}y");

            var cli = new CliHelper();
            var firstAnswer = cli.IsUserSure("Dude this is dangerous");
            var secondAnswer = cli.IsUserSure("Dude this is dangerous");
            var thirdAnswer = cli.IsUserSure("Dude this is dangerous");

            Assert.IsTrue(firstAnswer);
            Assert.IsFalse(secondAnswer);
            Assert.IsTrue(thirdAnswer);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Prompts the user to enter a value for the given property in the case that the option was specified with no value
        /// </summary>
        /// <param name="context">the parser context</param>
        public override void BeforePopulateProperty(ArgHook.HookContext context)
        {
            if (context.ArgumentValue == string.Empty)
            {
                var cli = new CliHelper();

                ITabCompletionHandler tabHandler;
                IHighlighterConfigurator highlighterConfigurator;

                if (TabCompletionHandlerType.TryCreate<ITabCompletionHandler>(out tabHandler))
                {
                    cli.Reader.TabHandler.TabCompletionHandlers.Add(tabHandler);
                }

                if (HighlighterConfiguratorType.TryCreate<IHighlighterConfigurator>(out highlighterConfigurator))
                {
                    cli.Reader.Highlighter = new SimpleSyntaxHighlighter();
                    highlighterConfigurator.Configure(cli.Reader.Highlighter);
                }

                context.ArgumentValue = cli.PromptForLine("Enter value for " + context.CurrentArgument.DefaultAlias);
            }
        }
        public void TestEscapingFromPicker()
        {
            ConsoleProvider.Current = new TestConsoleProvider("{control} {w}{down}{down}{escape}");
            CliHelper cli = new CliHelper();
            var picker = new ContextAssistPicker();
            picker.Options.Add(ContextAssistSearchResult.FromString("Option 1"));
            picker.Options.Add(ContextAssistSearchResult.FromString("Option 2"));
            picker.Options.Add(ContextAssistSearchResult.FromString("Option 3"));
            cli.Reader.ContextAssistProvider = picker;

            var line = cli.Reader.ReadLine();
            Assert.AreEqual("", line.ToString());
        }
Exemplo n.º 6
0
        private bool TryPreventExceptionWithPrompt(ArgHook.HookContext context)
        {
            if (parent.PromptIfMissing && ArgHook.HookContext.Current.Definition.IsNonInteractive == false)
            {

                var cli = new CliHelper();

                ITabCompletionHandler tabHandler;
                IHighlighterConfigurator highlighterConfigurator;

                if (TabCompletionHandlerType.TryCreate<ITabCompletionHandler>(out tabHandler))
                {
                    cli.Reader.TabHandler.TabCompletionHandlers.Add(tabHandler);
                }

                if (HighlighterConfiguratorType.TryCreate<IHighlighterConfigurator>(out highlighterConfigurator))
                {
                    cli.Reader.Highlighter = new SimpleSyntaxHighlighter();
                    highlighterConfigurator.Configure(cli.Reader.Highlighter);
                }

                cli.Reader.UnregisterHandler(ConsoleKey.Escape);
                cli.Reader.RegisterHandler(KeyHandler.FromAction((searchReaderContext) =>
                {
                    TabCompletion tabCompletionInfo;
                    if (context.Definition.IsNonInteractive == false &&
                        context.Definition.Metadata.TryGetMeta<TabCompletion>(out tabCompletionInfo) &&
                        tabCompletionInfo.REPL == true)
                    {
                        // if this is an interactive REPL then continue the REPL in this case as the user may have changed their mind about taking
                        // this action - Note there are two places in this file that have this logic
                        throw new REPLContinueException();
                    }
                    else
                    {
                        throw new MissingArgException("The argument '" + context.CurrentArgument.DefaultAlias + "' is required", new ArgumentNullException(context.CurrentArgument.DefaultAlias));
                    }
                }, ConsoleKey.Escape));

                context.ArgumentValue = cli.PromptForLine("Enter value for " + context.CurrentArgument.DefaultAlias);
                context.CurrentArgument.Populate(context);
                return true;
            }
            else
            {
                return false;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Validates that the user actually specified a value and optionally prompts them when it is missing.
        /// </summary>
        /// <param name="argument">The argument being populated.  This validator doesn't do anything with it.</param>
        /// <param name="arg">The value specified on the command line or null if it wasn't specified</param>
        public override void ValidateAlways(CommandLineArgument argument, ref string arg)
        {
            if (IsConditionallyRequired)
            {
                var matchingHook = (from h in argument.Metadata.Metas<ArgRequiredConditionalHook>() where h.parent == this select h).SingleOrDefault();
                if(matchingHook == null)
                {
                    argument.Metadata.Add(new ArgRequiredConditionalHook(this));
                }
            }

            if (IsConditionallyRequired == false && arg == null && PromptIfMissing && ArgHook.HookContext.Current.Definition.IsNonInteractive == false)
            {
                var cli = new CliHelper();

                ITabCompletionHandler tabHandler;
                IHighlighterConfigurator highlighterConfigurator;

                if (TabCompletionHandlerType.TryCreate<ITabCompletionHandler>(out tabHandler))
                {
                    cli.Reader.TabHandler.TabCompletionHandlers.Add(tabHandler);
                }

                if (HighlighterConfiguratorType.TryCreate<IHighlighterConfigurator>(out highlighterConfigurator))
                {
                    cli.Reader.Highlighter = new SimpleSyntaxHighlighter();
                    highlighterConfigurator.Configure(cli.Reader.Highlighter);
                }

                cli.Reader.UnregisterHandler(ConsoleKey.Escape);
                cli.Reader.RegisterHandler(KeyHandler.FromAction((searchReaderContext) =>
                {
                    TabCompletion tabCompletionInfo;
                    if (ArgHook.HookContext.Current.Definition.IsNonInteractive == false &&
                        ArgHook.HookContext.Current.Definition.Metadata.TryGetMeta<TabCompletion>(out tabCompletionInfo)
                        && tabCompletionInfo.REPL == true)
                    {
                        // if this is an interactive REPL then continue the REPL in this case as the user may have changed their mind about taking
                        // this action - Note there are two places in this file that have this logic
                        throw new REPLContinueException();
                    }
                    else
                    {
                        throw new MissingArgException("The argument '" + argument.DefaultAlias + "' is required", new ArgumentNullException(argument.DefaultAlias));
                    }
                }, ConsoleKey.Escape));

                arg = cli.PromptForLine("Enter value for " + argument.DefaultAlias);
            }

            if (arg == null && IsConditionallyRequired == false)
            {
                throw new MissingArgException("The argument '" + argument.DefaultAlias + "' is required", new ArgumentNullException(argument.DefaultAlias));
            }
        }