示例#1
0
        /// <summary>
        /// Retrieves the <c>IMenuItem</c> associated with the specified keyword.
        ///
        /// If no single item matches perfectly, the search will broaden to all items starting with the keyword.
        ///
        /// In case sensitive mode, missing match which could be solved by different casing will re reported if
        /// <c>complain</c> is specified.
        ///
        /// If <c>useDefault</c> is set and a default item is present, it will be returned and no complaint
        /// will be generated.
        /// </summary>
        ///
        /// <param name="cmd">
        /// In: The command, possibly with arguments, from which the keyword is extracted which uniquely
        /// identifies the searched menu item.
        /// Out: The keyword uniquely identifying a menu item, or null if no such menu item was found.
        /// </param>
        /// <param name="args">
        /// Out: The arguments which were supplied in addition to a keyword.
        /// </param>
        /// <param name="complain">
        /// If true, clarifications about missing or superfluous matches will be written to stdout.
        /// </param>
        /// <param name="useDefault">
        /// The single closest matching menu item, or the default item if no better fit was found, or null in
        /// case of 0 or multiple matches.
        /// </param>
        /// <param name="includeDisabled">
        /// Disabled menu items are included iff this is set.
        /// </param>
        public CMenuItem GetMenuItem(
            ref string cmd,
            out string args,
            bool complain,
            bool useDefault,
            bool includeDisabled)
        {
            if (cmd == null)
            {
                throw new ArgumentNullException("cmd");
            }

            /*
             * Is there a fitting child menu?
             */
            var original = cmd;

            args = cmd;
            cmd  = MenuUtil.SplitFirstWord(ref args);

            var its = GetCommands(cmd, StringComparison, includeDisabled);

            if (its.Length == 1)
            {
                return(its[0]);
            }
            if (its.Length > 1)
            {
                if (complain)
                {
                    var s = cmd == ""
                                                ? "Command incomplete."
                                                : "Command <" + cmd + "> not unique.";
                    OnWriteLine(
                        s + " Candidates: " +
                        string.Join(", ", its.Select(it => it.Selector)));
                }
                return(null);
            }

            /*
             * Is there a fallback?
             */
            var def = this[null];

            if (def != null)
            {
                cmd  = null;
                args = original;
                return(def);
            }

            /*
             * We found nothing. Display this failure?
             */
            if (complain)
            {
                OnWriteLine("Unknown command: " + cmd);

                if (StringComparison.IsCaseSensitive())
                {
                    var suggestions = GetCommands(cmd, StringComparison.InvariantCultureIgnoreCase, includeDisabled);
                    if (suggestions.Length > 0)
                    {
                        if (suggestions.Length == 1)
                        {
                            OnWriteLine("Did you mean \"" + suggestions[0].Selector + "\"?");
                        }
                        else if (suggestions.Length <= 5)
                        {
                            var sugs = string.Join(", ", suggestions
                                                   .Take(suggestions.Length - 1)
                                                   .Select(sug => "\"" + sug.Selector + "\""));
                            var s = "Did you mean " + sugs + " or \"" + suggestions.Last().Selector + "\"?";
                            OnWriteLine(s);
                        }
                    }
                }
            }

            return(null);
        }
示例#2
0
        /// <summary>
        /// Retrieves the IMenuItem associated with the specified keyword.
        ///
        /// If no single item matches perfectly, the search will broaden to all items starting with the keyword.
        ///
        /// In case sensitive mode, missing match which could be solved by different casing will re reported if complain is specified.
        ///
        /// If <c>useDefault</c> is set and a default item is present, it will be returned and no complaint will be generated.
        /// </summary>
        /// <param name="cmd">
        /// In: The command, possibly with arguments, from which the keyword is extracted which uniquely identifies the searched menu item.
        /// Out: The keyword uniquely identifying a menu item, or null if no such menu item was found.
        /// </param>
        /// <param name="args">
        /// Out: The arguments which were supplied in addition to a keyword.
        /// </param>
        /// <param name="complain">
        /// If true, clarifications about missing or superfluous matches will be written to stdout.
        /// </param>
        /// <param name="useDefault">
        /// The single closest matching menu item, or the default item if no better fit was found, or null in case of 0 or multiple matches.
        /// </param>
        /// <returns></returns>
        public CommandMenuItem GetMenuItem(ref string cmd, out string args, bool complain, bool useDefault, bool useCache)
        {
            if (cmd == null)
            {
                throw new ArgumentNullException("cmd");
            }

            // Is there a fitting child menu?
            string original = cmd;

            args = cmd;
            cmd  = StringHelper.SplitFirstWord(ref args);

            string[]          selectors;
            CommandMenuItem[] its = GetCommands(cmd, out selectors, StringComparison, useCache);

            if (its.Length == 1)
            {
                return(its[0]);
            }

            if (its.Length > 1)
            {
                if (complain)
                {
                    IO.SetForeColor(ConsoleColor.Red);
                    string s = cmd == ""
                        ? Lang.Get("Command_Incomplete")
                        : Lang.Get("Command_Not_Unique", cmd);

                    IO.WriteLine(s + " Candidates: " + string.Join(", ", selectors));
                }
                return(null);
            }

            // Is there a fallback?
            CommandMenuItem def = this[null];

            if (def != null)
            {
                cmd  = null;
                args = original;
                return(def);
            }

            // We found nothing. Display this failure?
            if (complain)
            {
                IO.SetForeColor(ConsoleColor.Red);
                IO.WriteLine(Lang.Get("Unknown_Command", cmd));

                if (StringComparison.IsCaseSensitive())
                {
                    /*  CommandMenuItem[] suggestions =*/
                    GetCommands(cmd, out selectors, StringComparison.InvariantCultureIgnoreCase, useCache);
                    if (selectors != null && selectors.Length > 0)
                    {
                        if (selectors.Length == 1)
                        {
                            IO.WriteLine("Did you mean \"" + selectors[0] + "\"?");
                        }
                        else if (selectors.Length <= 5)
                        {
                            IO.Write("Did you mean ");
                            IO.Write(string.Join(", ", selectors.Take(selectors.Length - 1).Select(sug => "\"" + sug + "\"")));
                            IO.Write(" or \"" + selectors.Last() + "\"?");
                            IO.WriteLine("");
                        }
                    }
                }
            }

            return(null);
        }