Ejemplo n.º 1
0
        public int Execute(string[] args)
        {
            try {
                System.Collections.IEnumerator argsEn = args.GetEnumerator();
                while(argsEn.MoveNext()) {
                    string arg = (string)argsEn.Current;

                    if (arg.StartsWith("-")) {
                        ICommand cmd;
                        if (!_commands.TryGetValue(arg.Substring(1), out cmd)) {
                            // Show usage
                            return 2;
                        }

                        cmd.Init(this);

                        if (cmd.Arguments != null) {
                            foreach (CommandArgument cmdarg in cmd.Arguments) {
                                if (!argsEn.MoveNext() || !cmdarg.TryParse((string)argsEn.Current)) {
                                    Console.WriteLine("Failed to parse argument {0} for -{1} command.", cmdarg.Name, cmd.Trigger);
                                    return 3;
                                }
                            }
                        }

                        cmd.Execute(this);

                    } else {
                        // Show usage
                        return 1;
                    }
                }

                if (Input == null)
                    Input = Console.In;
                if (Output == null)
                    Output = Console.OpenStandardOutput();

                if (Parser == null)
                    Parser = new Plugins.DefaultParserModule();
                if (Writer == null)
                    Writer = new Plugins.DefaultWriterModule();

                var parsed = Parser.Transform(Input);
                Writer.Transform(parsed, Output);
            #if !DEBUG
            } catch(Exception ex) {
                Console.Error.WriteLine("An uncaught exception has occurred:");
                Console.Error.WriteLine(ex.Message);
                Console.Error.WriteLine(ex.StackTrace);
                while((ex = ex.InnerException) != null) {
                    Console.Error.WriteLine("Inner Exception: " + ex.Message);
                    Console.Error.WriteLine(ex.StackTrace);
                }
            #endif
            } finally {
                if (Input != null)
                    Input.Dispose();
                if (Output != null)
                    Output.Dispose();
            }
            return 0;
        }
Ejemplo n.º 2
0
        public int Execute(string[] args)
        {
            try {
                System.Collections.IEnumerator argsEn = args.GetEnumerator();
                while (argsEn.MoveNext())
                {
                    string arg = (string)argsEn.Current;

                    if (arg.StartsWith("-"))
                    {
                        ICommand cmd;
                        if (!_commands.TryGetValue(arg.Substring(1), out cmd))
                        {
                            // Show usage
                            return(2);
                        }

                        cmd.Init(this);

                        if (cmd.Arguments != null)
                        {
                            foreach (CommandArgument cmdarg in cmd.Arguments)
                            {
                                if (!argsEn.MoveNext() || !cmdarg.TryParse((string)argsEn.Current))
                                {
                                    Console.WriteLine("Failed to parse argument {0} for -{1} command.", cmdarg.Name, cmd.Trigger);
                                    return(3);
                                }
                            }
                        }

                        cmd.Execute(this);
                    }
                    else
                    {
                        // Show usage
                        return(1);
                    }
                }

                if (Input == null)
                {
                    Input = Console.In;
                }
                if (Output == null)
                {
                    Output = Console.OpenStandardOutput();
                }

                if (Parser == null)
                {
                    Parser = new Plugins.DefaultParserModule();
                }
                if (Writer == null)
                {
                    Writer = new Plugins.DefaultWriterModule();
                }

                var parsed = Parser.Transform(Input);
                Writer.Transform(parsed, Output);
#if !DEBUG
            } catch (Exception ex) {
                Console.Error.WriteLine("An uncaught exception has occurred:");
                Console.Error.WriteLine(ex.Message);
                Console.Error.WriteLine(ex.StackTrace);
                while ((ex = ex.InnerException) != null)
                {
                    Console.Error.WriteLine("Inner Exception: " + ex.Message);
                    Console.Error.WriteLine(ex.StackTrace);
                }
#endif
            } finally {
                if (Input != null)
                {
                    Input.Dispose();
                }
                if (Output != null)
                {
                    Output.Dispose();
                }
            }
            return(0);
        }