Exemple #1
0
        /// <summary>
        /// Creates a new <see cref="CommandArguments"/> with the given <paramref name="args"/> and <paramref name="translateAliases"/>.
        /// </summary>
        /// <param name="args">The arguments to process.</param>
        /// <param name="translateAliases">
        /// <b>True</b> will check for aliases, if found, the command will be changed to the aliased command.
        /// <b>False</b> will clean the command, but not alter it.
        /// </param>
        /// <returns>A <see cref="CommandArguments"/> with a command its and associated arguments.</returns>
        public static CommandArguments Create(IList <string> args, bool translateAliases)
        {
            if (args.Count == 0)
            {
                // no command
                return(new CommandArguments("", new List <string>()));
            }
            // get command
            var command = CommandLineInterfaceHelper.CleanArgument(args[0]);

            // check if translation is allowed
            if (translateAliases)
            {
                command = TranslateAlias(command);
            }
            // get remaining arguments
            var list = new List <string>();

            foreach (var item in args.Skip(1))
            {
                var arg = item.Trim();
                if (!string.IsNullOrWhiteSpace(arg))
                {
                    list.Add(arg);
                }
            }
            // return command and arguments
            return(new CommandArguments(command, list));

            // ################################
            string TranslateAlias(string alias_)
            {
                // check for known command names
                if (CommandLineInterfaceShared.Commands.ContainsKey(alias_))
                {
                    return(alias_);
                }
                // check for aliases
                foreach (var command_ in CommandLineInterfaceShared.Commands)
                {
                    foreach (var candidateAlias in command_.Value.Aliases)
                    {
                        if (alias_.Equals(candidateAlias))
                        {
                            return(command_.Value.Command);
                        }
                    }
                }
                // failed to find command or alias
                return(alias_);
            }
        }
        /// <summary>
        /// Starts the Command Line Interface's main message pump.
        /// </summary>
        public void Start()
        {
            // display header
            Console.WriteLine(CommandLineInterfaceShared.UIHeader);
            Console.WriteLine();
            // start UI message loop
            while (true)
            {
                // get command line from user
                Console.Write(CommandLineInterfaceHelper.CommandLinePrompt);
                var line = CleanLine(Console.ReadLine());

                // extract command line arguments
                var parts = CommandLineInterfaceHelper.ConvertToArgs(line);

                // extract command and arguments
                var commArgs = CommandArguments.Create(parts);

                // process command
                if (!string.IsNullOrWhiteSpace(commArgs.Command))
                {
                    // check for exit command
                    if (commArgs.Command == "exit")
                    {
                        break;
                    }
                    // process command
                    try
                    {
                        CommandLineInterfaceHelper.GetCommandObject(commArgs.Command)?.Execute(commArgs.Arguments);
                    }
                    catch (Exception ex)
                    {
                        CommandLineInterfaceHelper.DisplayError(ex.Message);
                    }
                    // blank line after invocation
                    Console.WriteLine();
                }
            }

            // ######## internal methods

            string CleanLine(string line_)
            {
                // remove tabs
                line_ = line_.Replace('\t', ' ');
                // return processed line
                return(line_);
            }
        }
Exemple #3
0
        internal static string DisplayCommands(bool includeHeader)
        {
            var result = new StringBuilder();

            // header
            if (includeHeader)
            {
                result.AppendLine($"command{new string(' ', HelpLineLength - 7)}description");
                result.AppendLine(CommandLineInterfaceHelper.SeparatorLine());
            }
            // commands
            foreach (var item in Commands.OrderBy(x => x.Key))
            {
                result.AppendLine(CommandLineInterfaceHelper.FormatHelp(item.Value));
            }
            //
            return(result.ToString());
        }