Exemplo n.º 1
0
        public void TestPowerArgsRichCommandLineReaderFindContextualAction()
        {
            try
            {
                PowerArgsRichCommandLineReader.FindContextualAction("doesnotmatter");
                Assert.Fail("An exception should have been thrown");
            }
            catch (NullReferenceException ex)
            {
                Assert.IsTrue(ex.Message.Contains("ambient"));
            }

            CommandLineArgumentsDefinition def = new CommandLineArgumentsDefinition();

            Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualAction(null, def));
            Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualAction("", def));
            Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualAction("NonMatchingAction", def));

            var action = new CommandLineAction((d) => { });

            action.Aliases.Add("TheAction");

            def.Actions.Add(action);

            var found = PowerArgsRichCommandLineReader.FindContextualAction("theaction", def);

            Assert.AreSame(action, found);
        }
Exemplo n.º 2
0
        public void TestPowerArgsRichCommandLineReaderFindContextualArgument()
        {
            var args = new PowerArgsRichCommandLineReader.FindContextualArgumentArgs()
            {
                CurrentTokenIndex = 0,
                CommandLine       = "-TheString",
                PreviousToken     = "-TheString",
            };

            try
            {
                PowerArgsRichCommandLineReader.FindContextualArgument(args);
                Assert.Fail("An exception should have been thrown");
            }
            catch (NullReferenceException ex)
            {
                Assert.IsTrue(ex.Message.Contains("ambient"));
            }

            CommandLineArgumentsDefinition def = new CommandLineArgumentsDefinition();
            var globalArg = new CommandLineArgument(typeof(string), "TheString");

            def.Arguments.Add(globalArg);

            args.Definition = def;
            var found = PowerArgsRichCommandLineReader.FindContextualArgument(args);

            Assert.AreSame(globalArg, found);

            args.CommandLine   = "/TheString";
            args.PreviousToken = args.CommandLine;
            found = PowerArgsRichCommandLineReader.FindContextualArgument(args);
            Assert.AreSame(globalArg, found);

            args.CommandLine   = "-ActionInt";
            args.PreviousToken = args.CommandLine;
            Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualArgument(args));
            Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualArgument(args));

            var action = new CommandLineAction((d) => { });

            action.Aliases.Add("TheAction");

            var actionArg = new CommandLineArgument(typeof(int), "ActionInt");

            action.Arguments.Add(actionArg);
            def.Actions.Add(action);

            args.CommandLine   = "-TheString";
            args.PreviousToken = args.CommandLine;
            args.ActionContext = action;
            found = PowerArgsRichCommandLineReader.FindContextualArgument(args);
            Assert.AreSame(globalArg, found);

            args.CommandLine   = "-ActionInt";
            args.PreviousToken = args.CommandLine;
            found = PowerArgsRichCommandLineReader.FindContextualArgument(args);
            Assert.AreSame(actionArg, found);
        }
        public void TestArgumentAwareSmartTabCompletion()
        {
            var input = "m\t -a\t \t"; // should expand to 'meats -animal Chicken;
            ConsoleProvider.Current = new TestConsoleProvider(input);
            var definition = new CommandLineArgumentsDefinition(typeof(ConflictingArgumentsThatAwesomeTabCompletionMakesBetter));

            PowerArgsRichCommandLineReader reader = new PowerArgsRichCommandLineReader(definition, new List<ConsoleString>());
            reader.ThrowOnSyntaxHighlightException = true;
            reader.TabHandler.ThrowOnTabCompletionHandlerException = true;
            var completed = string.Join(" ", reader.ReadCommandLine());
            Assert.AreEqual("meats -animal Chicken", completed);
        }
Exemplo n.º 4
0
        public void TestPowerArgsRichCommandLineReaderFindCurrentTokenArgument()
        {
            bool expect;

            try
            {
                PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, null, out expect);
            }
            catch (NullReferenceException ex)
            {
                Assert.IsTrue(ex.Message.Contains("ambient"));
            }

            CommandLineArgumentsDefinition def = new CommandLineArgumentsDefinition();
            var globalArg = new CommandLineArgument(typeof(int), "TheInt");

            def.Arguments.Add(globalArg);
            Assert.IsNull(PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, null, out expect, def));
            Assert.IsFalse(expect);

            var found = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, "-TheInt", out expect, def);

            Assert.AreSame(globalArg, found);
            Assert.IsTrue(expect);

            found = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, "/TheInt", out expect, def);
            Assert.AreSame(globalArg, found);
            Assert.IsTrue(expect);

            found = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, "TheInt", out expect, def);
            Assert.IsNull(found);
            Assert.IsFalse(expect);

            found = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, "-ActionInt", out expect, def);
            Assert.IsNull(found);
            Assert.IsTrue(expect);

            var action = new CommandLineAction((d) => { });

            action.Aliases.Add("TheAction");
            var actionArg = new CommandLineArgument(typeof(int), "ActionInt");

            action.Arguments.Add(actionArg);
            def.Actions.Add(action);

            found = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(action, "-ActionInt", out expect, def);
            Assert.AreSame(actionArg, found);
            Assert.IsTrue(expect);
        }
        public void TestArgumentAwareSmartTabCompletion()
        {
            var input = "m\t -a\t \t"; // should expand to 'meats -animal Chicken;

            ConsoleProvider.Current = new TestConsoleProvider(input);
            var definition = new CommandLineArgumentsDefinition(typeof(ConflictingArgumentsThatAwesomeTabCompletionMakesBetter));

            PowerArgsRichCommandLineReader reader = new PowerArgsRichCommandLineReader(definition, new List <ConsoleString>());

            reader.ThrowOnSyntaxHighlightException = true;
            reader.TabHandler.ThrowOnTabCompletionHandlerException = true;
            var completed = string.Join(" ", reader.ReadCommandLine());

            Assert.AreEqual("meats -animal Chicken", completed);
        }
Exemplo n.º 6
0
        public void TestPowerArgsRichCommandLineReaderFindContextualArgument()
        {
            try
            {
                PowerArgsRichCommandLineReader.FindContextualArgument("-TheString", null);
                Assert.Fail("An exception should have been thrown");
            }
            catch (NullReferenceException ex)
            {
                Assert.IsTrue(ex.Message.Contains("ambient"));
            }

            CommandLineArgumentsDefinition def = new CommandLineArgumentsDefinition();
            var globalArg = new CommandLineArgument(typeof(string), "TheString");

            def.Arguments.Add(globalArg);
            var found = PowerArgsRichCommandLineReader.FindContextualArgument("-TheString", null, def);

            Assert.AreSame(globalArg, found);

            found = PowerArgsRichCommandLineReader.FindContextualArgument("/TheString", null, def);
            Assert.AreSame(globalArg, found);

            Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualArgument("-ActionInt", null, def));
            Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualArgument(null, null, def));

            var action = new CommandLineAction((d) => { });

            action.Aliases.Add("TheAction");

            var actionArg = new CommandLineArgument(typeof(int), "ActionInt");

            action.Arguments.Add(actionArg);
            def.Actions.Add(action);

            found = PowerArgsRichCommandLineReader.FindContextualArgument("-TheString", action, def);
            Assert.AreSame(globalArg, found);

            found = PowerArgsRichCommandLineReader.FindContextualArgument("-ActionInt", action, def);
            Assert.AreSame(actionArg, found);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Before PowerArgs parses the args, this hook inspects the command line for the indicator and if found 
        /// takes over the command line and provides tab completion.
        /// </summary>
        /// <param name="context">The context used to inspect the command line arguments.</param>
        public override void BeforeParse(ArgHook.HookContext context)
        {
          
            if (CompletionSourceType != null && 
                CompletionSourceType.GetInterfaces().Contains(typeof(ITabCompletionSource)) == false &&
                CompletionSourceType.GetInterfaces().Contains(typeof(ISmartTabCompletionSource)) == false)
            {
                throw new InvalidArgDefinitionException("Type does not implement ITabCompletionSource or ISmartTabCompletionSource: " + CompletionSourceType.FullName);
            }

            if (context.Definition.IsNonInteractive)
            {
                this.REPL = false;
                return;
            }
            if (Indicator == "" && context.CmdLineArgs.Length != 0)
            {
                this.REPL = false;
                return;
            }
            if (Indicator != "" && (context.CmdLineArgs.Length != 1 || context.CmdLineArgs[0] != Indicator))
            {
                this.REPL = false;
                return;
            }
            
            if (REPL && ShowREPLWelcome)
            {
                ConsoleString.Empty.WriteLine();
                var message = REPLWelcomeMessage.Replace("{{Indicator}}", REPLExitIndicator);
                ConsoleString.WriteLine(message, ConsoleColor.Cyan);
                ConsoleString.Empty.WriteLine();
                ConsoleString.Write(Indicator + "> ", ConsoleColor.Cyan);
                ShowREPLWelcome = false;
            }
            else if (REPL)
            {
                ConsoleString.Write(Indicator + "> ", ConsoleColor.Cyan);
            }
            else
            {

                // This is a little hacky, but I could not find a better way to make the tab completion start on the same lime
                // as the command line input
                try
                {
                    var lastLine = StdConsoleProvider.ReadALineOfConsoleOutput(Console.CursorTop - 1);
                    Console.CursorTop--;
                    Console.WriteLine(lastLine);
                    Console.CursorTop--;
                    Console.CursorLeft = lastLine.Length + 1;
                }
                catch (Exception)
                {
                    Console.WriteLine();
                    Console.Write(Indicator + "> ");
                }
            }

            PowerArgsRichCommandLineReader reader = new PowerArgsRichCommandLineReader(context.Definition, LoadHistory());

            IHighlighterConfigurator customConfigurator;
            if(HighlighterConfiguratorType.TryCreate<IHighlighterConfigurator>(out customConfigurator))
            {
                customConfigurator.Configure(reader.Highlighter);
            }

            var newCommandLineString = reader.ReadLine().ToString();
            var newCommandLineArray = Args.Convert(newCommandLineString);

            if (REPL && newCommandLineArray.Length == 1 && string.Equals(newCommandLineArray[0], REPLExitIndicator, StringComparison.OrdinalIgnoreCase))
            {
                throw new REPLExitException();
            }

            if (REPL && newCommandLineArray.Length == 1 && newCommandLineArray[0] == "cls")
            {
                ConsoleProvider.Current.Clear();
                throw new REPLContinueException();
            }

            else if (REPL && newCommandLineArray.Length == 0 && string.IsNullOrWhiteSpace(REPLExitIndicator) == false)
            {
                throw new REPLContinueException();
            }

            context.CmdLineArgs = newCommandLineArray;
            AddToHistory(newCommandLineString);
        }