Exemplo n.º 1
0
        /// <summary>
        /// Test whether switches in an Arguments object that are required for this command exist
        /// </summary>
        /// <param name="arguments">Arguments object that contains switches to test.</param>
        /// <returns>True if all switches that are required exist</returns>
        private bool RequiredSwitchesExist(ConsoleArguments arguments)
        {
            foreach (ConsoleSwitch s in Switches.Values)
            {
                if (s.IsRequired && !arguments.Contains(s.Tag))
                {
                    ConsoleLogger.PrintError("The " + InvokeCommand + " command requires the -" + s.Tag + " switch.");
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        private bool SwitchesHaveValues(ConsoleArguments arguments)
        {
            foreach (ConsoleSwitch s in Switches.Values)
            {
                // at this point we've confirmed that required switches exist, so we can make sure to only test
                // optional switches if they exist, and are not boolean
                if (s.IsBoolean || !arguments.Contains(s.Tag))
                {
                    continue;
                }

                if (arguments[s.Tag] == null)
                {
                    ConsoleLogger.PrintError("No value found for the -" + s.Tag + " switch.");
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Executes this command
        /// </summary>
        /// <param name="args">Array of string _arguments input by the user (does NOT contain the command verb). This ONLY contains switches</param>
        /// <returns>And int based on if the command was executed successfully. Successful execution is defined in your command functions.</returns>
        public int ExecuteCommand(string[] args)
        {
            bool             argumentsPassed = false;
            ConsoleArguments arguments       = new ConsoleArguments(args, this, out argumentsPassed);

            if (!argumentsPassed)
            {
                return(DisplayCmdManagerHelp());
            }

            // Test for help flag
            if (arguments.Contains("h"))
            {
                return(DisplayCmdManagerHelp());
            }

            if (!(RequiredSwitchesExist(arguments) && SwitchesHaveValues(arguments)))
            {
                return(DisplayCmdManagerHelp());
            }

            return(_function.Invoke(arguments));
        }