Beispiel #1
0
    private static int CliMain(string[] args)
    {
        int result = -1;

        string temp;
        var wait = ArgumentList.Remove(ref args, "wait", out temp);
        var nologo = ArgumentList.Remove(ref args, "nologo", out temp);

        try
        {
            // Display logo/header information
            var entryAssembly = Assembly.GetEntryAssembly();
            if (entryAssembly != null && nologo == false)
            {
                Console.WriteLine("{0}", entryAssembly.GetName());
                foreach (
                    AssemblyCopyrightAttribute a in entryAssembly.GetCustomAttributes(typeof (AssemblyCopyrightAttribute), false)
                    )
                    Console.WriteLine("{0}", a.Copyright);
                Console.WriteLine();
            }

            // If verbose output was specified, attach a trace listener
            if (ArgumentList.Remove(ref args, "verbose", out temp) || ArgumentList.Remove(ref args, "verbosity", out temp))
            {
                SourceLevels traceLevel;
                try
                {
                    traceLevel = (SourceLevels) Enum.Parse(typeof (SourceLevels), temp);
                }
                catch
                {
                    traceLevel = SourceLevels.All;
                }

                Trace.Listeners.Add(new ConsoleTraceListener()
                {
                    Filter = new EventTypeFilter(traceLevel),
                    IndentLevel = 0,
                    TraceOutputOptions = TraceOptions.None
                });
            }

            // Construct the CommandInterpreter and initialize
            ICommandInterpreter ci = new CommandInterpreter(
                DefaultCommands.Help |
                // Allows a simple ECHO command to output text to std::out
                DefaultCommands.Echo |
                // Allows getting/setting properties, like prompt and errorlevel below
                DefaultCommands.Get | DefaultCommands.Set |
                DefaultCommands.Prompt |
                DefaultCommands.ErrorLevel |
                // uncomment to allow cmd > C:\output.txt or cmd < C:\input.txt
                //DefaultCommands.IORedirect |
                // uncomment to use aaa | bbb | FIND "abc" | MORE
                //DefaultCommands.PipeCommands | DefaultCommands.Find | DefaultCommands.More |
                0,
                // Add the types to use static members
                typeof (Filters),
                // Add classes to use instance members
                new EchoCommands()
                );

            // If you want to hide some options/commands at runtime you can:
            foreach (var name in new[] {"prompt", "errorlevel"})
            {
                IOption opt;
                if (ci.TryGetOption(name, out opt))
                    opt.Visible = false;
            }
            foreach (var name in new[] {"echo", "set", "get"})
            {
                ICommand opt;
                if (ci.TryGetCommand(name, out opt))
                    opt.Visible = false;
            }

            // Apply all DefaultValue values to properties
            ci.SetDefaults();

            // If we have arguments, just run with those arguments...
            if (args.Length > 0)
                ci.Run(args);
            else
            {
                //When run without arguments you can either display help:
                //ci.Help(null);
                //... or run the interpreter:
                ci.Run(Console.In);
            }

            result = ci.ErrorLevel;
        }
        catch (OperationCanceledException)
        {
            result = 3;
        }
        catch (ApplicationException ex)
        {
            Trace.TraceError("{0}", ex);
            Console.Error.WriteLine(ex.Message);
            result = 1;
        }
        catch (Exception ex)
        {
            Trace.TraceError("{0}", ex);
            Console.Error.WriteLine(ex.ToString());
            result = 2;
        }
        finally
        {
            if (wait)
            {
                Console.WriteLine("Exited with result = {0}, Press Enter to quit.", result);
                Console.ReadLine();
            }
        }

        return Environment.ExitCode = result;
    }
Beispiel #2
0
 public LastFilter(CommandInterpreter ci)
 {
     _ci = ci;
 }