コード例 #1
0
        private bool ParseCommand(string[] args, ref int i, bool noexitSeen, bool isEncoded)
        {
            if (_commandLineCommand != null)
            {
                // we've already set the command, so squawk

                _hostUI.WriteErrorLine(CommandLineParameterParserStrings.CommandAlreadySpecified);
                _showHelp     = true;
                _abortStartup = true;
                _exitCode     = ConsoleHost.ExitCodeBadCommandLineParameter;
                return(false);
            }

            ++i;
            if (i >= args.Length)
            {
                _hostUI.WriteErrorLine(CommandLineParameterParserStrings.MissingCommandParameter);
                _showHelp     = true;
                _abortStartup = true;
                _exitCode     = ConsoleHost.ExitCodeBadCommandLineParameter;
                return(false);
            }

            if (isEncoded)
            {
                try
                {
                    _commandLineCommand = StringToBase64Converter.Base64ToString(args[i]);
                }
                // decoding failed
                catch
                {
                    _hostUI.WriteErrorLine(CommandLineParameterParserStrings.BadCommandValue);
                    _showHelp     = true;
                    _abortStartup = true;
                    _exitCode     = ConsoleHost.ExitCodeBadCommandLineParameter;
                    return(false);
                }
            }
            else if (args[i] == "-")
            {
                // the arg to -command is -, which is secret code for "read the commands from stdin with no prompts"

                _explicitReadCommandsFromStdin = true;
                _noPrompt = true;

                ++i;
                if (i != args.Length)
                {
                    // there are more parameters to -command than -, which is an error.

                    _hostUI.WriteErrorLine(CommandLineParameterParserStrings.TooManyParametersToCommand);
                    _showHelp     = true;
                    _abortStartup = true;
                    _exitCode     = ConsoleHost.ExitCodeBadCommandLineParameter;
                    return(false);
                }

                if (!Console.IsInputRedirected)
                {
                    _hostUI.WriteErrorLine(CommandLineParameterParserStrings.StdinNotRedirected);
                    _showHelp     = true;
                    _abortStartup = true;
                    _exitCode     = ConsoleHost.ExitCodeBadCommandLineParameter;
                    return(false);
                }
            }
            else
            {
                // Collect the remaining parameters and combine them into a single command to be run.

                StringBuilder cmdLineCmdSB = new StringBuilder();

                while (i < args.Length)
                {
                    cmdLineCmdSB.Append(args[i] + " ");
                    ++i;
                }
                if (cmdLineCmdSB.Length > 0)
                {
                    // remove the last blank
                    cmdLineCmdSB.Remove(cmdLineCmdSB.Length - 1, 1);
                }
                _commandLineCommand = cmdLineCmdSB.ToString();
            }

            if (!noexitSeen && !_explicitReadCommandsFromStdin)
            {
                // don't reset this if they've already specified -noexit
                _noExit = false;
            }

            _showBanner = false;

            return(true);
        }
コード例 #2
0
        private bool ParseCommand(string[] args, ref int i, bool noexitSeen, bool isEncoded)
        {
            bool flag;

            if (this.commandLineCommand == null)
            {
                i = i + 1;
                if (i < (int)args.Length)
                {
                    if (!isEncoded)
                    {
                        if (args[i] != "-")
                        {
                            StringBuilder stringBuilder = new StringBuilder();
                            while (i < (int)args.Length)
                            {
                                stringBuilder.Append(string.Concat(args[i], " "));
                                i = i + 1;
                            }
                            if (stringBuilder.Length > 0)
                            {
                                stringBuilder.Remove(stringBuilder.Length - 1, 1);
                            }
                            this.commandLineCommand = stringBuilder.ToString();
                        }
                        else
                        {
                            this.readFromStdin = true;
                            this.noPrompt      = true;
                            i = i + 1;
                            if (i == (int)args.Length)
                            {
                                if (!this.parent.IsStandardInputRedirected)
                                {
                                    this.ui.WriteErrorLine(CommandLineParameterParserStrings.StdinNotRedirected);
                                    this.showHelp     = true;
                                    this.abortStartup = true;
                                    this.exitCode     = -196608;
                                    return(false);
                                }
                            }
                            else
                            {
                                this.ui.WriteErrorLine(CommandLineParameterParserStrings.TooManyParametersToCommand);
                                this.showHelp     = true;
                                this.abortStartup = true;
                                this.exitCode     = -196608;
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        try
                        {
                            this.commandLineCommand = StringToBase64Converter.Base64ToString(args[i]);
                            if (!noexitSeen)
                            {
                                this.noExit = false;
                            }
                            this.showBanner = false;
                            return(true);
                        }
                        catch
                        {
                            this.ui.WriteErrorLine(CommandLineParameterParserStrings.BadCommandValue);
                            this.showHelp     = true;
                            this.abortStartup = true;
                            this.exitCode     = -196608;
                            flag = false;
                        }
                        return(flag);
                    }
                    if (!noexitSeen)
                    {
                        this.noExit = false;
                    }
                    this.showBanner = false;
                    return(true);
                }
                else
                {
                    this.ui.WriteErrorLine(CommandLineParameterParserStrings.MissingCommandParameter);
                    this.showHelp     = true;
                    this.abortStartup = true;
                    this.exitCode     = -196608;
                    return(false);
                }
            }
            else
            {
                this.ui.WriteErrorLine(CommandLineParameterParserStrings.CommandAlreadySpecified);
                this.showHelp     = true;
                this.abortStartup = true;
                this.exitCode     = -196608;
                return(false);
            }
        }