Exemple #1
0
        protected void ParseCommandLine(string[] args)
        {
            try
            {
                _commandLineOps["version"] = new TopLevelOption("displays the version", null);

                string command = null;
                Func <string, string[], int> commandMethod = null;
                string unsupportedOp = null;

                if (args == null || args.Length == 0)
                {
                    SetExitCode(ExitCodes.MissingCommandLineOption);
                    _showHelpMenu = true;
                }
                else
                {
                    command = args[0].ToLower();
                    if (command == "version")
                    {
                        _showVersion = true;
                    }

                    commandMethod = GetCommandMethod(command);
                    if (commandMethod == null)
                    {
                        unsupportedOp = command;
                    }
                }

                if (_showVersion)
                {
                    Console.WriteLine("{0} {1}", _versionProvider.GetProgramVersion(), _versionProvider.GetDataVersion());
                    SetExitCode(ExitCodes.Success);
                }
                else
                {
                    if (_showHelpMenu)
                    {
                        CommandLineUtilities.DisplayBanner(_programAuthors);
                        ShowHelpMenu(unsupportedOp);
                    }
                    else
                    {
                        if (FoundParsingErrors())
                        {
                            return;
                        }
                        if (commandMethod != null)
                        {
                            ExitCode = commandMethod(command, args.Skip(1).ToArray());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ExitCode = ExitCodeUtilities.ShowException(e);
            }
        }
        /// <summary>
        /// executes the command-line workflow
        /// </summary>
        public void Execute(string[] args)
        {
            var bench = new Benchmark();

            try
            {
                List <string> unsupportedOps = null;

                if (args == null || args.Length == 0)
                {
                    SetExitCode(ExitCodes.MissingCommandLineOption);
                    _showHelpMenu = true;
                }
                else
                {
                    try
                    {
                        unsupportedOps = _commandLineOps.Parse(args);

                        if (unsupportedOps.Count > 0)
                        {
                            SetExitCode(ExitCodes.UnknownCommandLineOption);
                            _showHelpMenu = true;
                        }
                    }
                    catch (OptionException oe)
                    {
                        _errorBuilder.AppendFormat("{0}ERROR: {1}\n", _errorSpacer, oe.Message);
                        SetExitCode(ExitCodes.UnknownCommandLineOption);
                        _showHelpMenu = true;
                    }
                }

                if (_showVersion)
                {
                    Console.WriteLine("{0} {1}", _versionProvider.GetProgramVersion(), _versionProvider.GetDataVersion());
                    SetExitCode(ExitCodes.Success);
                }
                else
                {
                    if (!Console.IsOutputRedirected)
                    {
                        CommandLineUtilities.DisplayBanner(_programAuthors);
                    }

                    if (_showHelpMenu)
                    {
                        Help.Show(_commandLineOps, _commandLineExample, _programDescription);

                        CommandLineUtilities.ShowUnsupportedOptions(unsupportedOps);

                        Console.WriteLine();
                        Console.WriteLine(_versionProvider.GetDataVersion());
                        Console.WriteLine();

                        // print the errors if any were found
                        if (FoundParsingErrors())
                        {
                            return;
                        }
                    }
                    else
                    {
                        ValidateCommandLine();

                        // print the errors if any were found
                        if (FoundParsingErrors())
                        {
                            return;
                        }

                        ProgramExecution();
                    }
                }
            }
            catch (Exception e)
            {
                ExitCode = ExitCodeUtilities.ShowException(e);
            }

            _peakMemoryUsageBytes = MemoryUtilities.GetPeakMemoryUsage();
            _wallTimeSpan         = bench.GetElapsedTime();

            if (!_showVersion && !_showHelpMenu && !Console.IsOutputRedirected)
            {
                Console.WriteLine();
                if (_peakMemoryUsageBytes > 0)
                {
                    Console.WriteLine("Peak memory usage: {0}", MemoryUtilities.ToHumanReadable(_peakMemoryUsageBytes));
                }
                Console.WriteLine("Time: {0}", Benchmark.ToHumanReadable(_wallTimeSpan));
            }
        }