/// <summary>
        /// Constructs a command-line interpreter from the objects and/or System.Types provided.
        /// </summary>
        public CommandInterpreter(DefaultCommands defaultCmds, params object[] handlers)
        {
            _head     = null;
            _prompt   = "> ";
            _commands = new Dictionary <string, ICommand>(StringComparer.OrdinalIgnoreCase);
            _options  = new Dictionary <string, IOption>(StringComparer.OrdinalIgnoreCase);
            _filters  = new List <ICommandFilter>();
            _fnNextCh = GetNextCharacter;

            //defaults to { Redirect, then Pipe, then everything else }
            _filterPrecedence = "<|*";

            _buildInCommands = new BuiltInCommands(
                Command.Make(this, this.GetType().GetMethod("Get")),
                Command.Make(this, this.GetType().GetMethod("Set", new Type[] { typeof(string), typeof(object), typeof(bool) })),
                Command.Make(this, this.GetType().GetMethod("Help", new Type[] { typeof(string), typeof(bool) })),
                Option.Make(this, this.GetType().GetProperty("ErrorLevel")),
                Option.Make(this, this.GetType().GetProperty("Prompt"))
                );

            _buildInCommands.Add(this, defaultCmds);

            foreach (object o in handlers)
            {
                AddHandler(o);
            }
        }
Exemplo n.º 2
0
 public TvList(ISkin skin, IViewport boxModel, ListState <TItem> data, Action <ITvListOptions <TItem> > optionsAction = null) : base(skin, boxModel, data)
 {
     _options = new TvListOptions <TItem>();
     optionsAction?.Invoke(_options);
     OnItemClicked  = new CommandChain <TItem>();
     _styleProvider = new TvListStyleProvider <TItem>(skin.ColorManager);
     _styleProvider.UseSkin(skin);
     _itemsCache = new TvListItemCache <TItem>(State.Columns, _styleProvider);
     State.SetCache(_itemsCache);
 }
Exemplo n.º 3
0
                public static void IORedirect(ICommandInterpreter ci, ICommandChain chain, string[] args)
                {
                    TextReader rin  = null;
                    TextWriter rout = null;
                    int        pos;

                    try
                    {
                        while ((pos = NextRedirect(args)) > 0)
                        {
                            List <string> cmd1 = new List <string>(args);
                            List <string> cmd2 = new List <string>(args);
                            cmd1.RemoveRange(pos, cmd1.Count - pos);
                            cmd2.RemoveRange(0, pos);

                            args = cmd1.ToArray();
                            string file     = String.Join(" ", cmd2.ToArray());
                            bool   isout    = file.StartsWith(">");
                            bool   isappend = isout && file.StartsWith(">>");
                            file = file.TrimStart('>', '<').Trim();

                            if (!isout)
                            {
                                rin = File.OpenText(file);
                            }
                            else
                            {
                                rout = isappend ? File.AppendText(file) : File.CreateText(file);
                            }
                        }
                    }
                    catch
                    {
                        using (rin)
                            using (rout)
                                throw;
                    }
                    TextReader stdin  = ConsoleInput.Capture(rin);
                    TextWriter stdout = ConsoleOutput.Capture(rout);

                    try
                    {
                        chain.Next(args);
                    }
                    finally
                    {
                        using (rin)
                            ConsoleInput.Restore(rin, stdin);
                        using (rout)
                            ConsoleOutput.Restore(rout, stdout);
                    }
                }
Exemplo n.º 4
0
                public static void PipeCommands(ICommandInterpreter ci, ICommandChain chain, string[] args)
                {
                    TextReader rdr = null, stdin = null;
                    int        pos;

                    while ((pos = NextPipe(args)) > 0)
                    {
                        List <string> cmd1 = new List <string>(args);
                        cmd1.RemoveRange(pos, cmd1.Count - pos);
                        List <string> cmd2 = new List <string>(args);
                        cmd2.RemoveRange(0, pos);
                        cmd2[0] = cmd2[0].TrimStart('|');
                        if (cmd2[0].Length == 0)
                        {
                            cmd2.RemoveAt(0);
                        }
                        if (cmd2.Count == 0)
                        {
                            args = cmd1.ToArray();
                            break;
                        }
                        else
                        {
                            args = cmd2.ToArray();
                        }

                        StringWriter wtr    = new StringWriter();
                        TextWriter   stdout = ConsoleOutput.Capture(wtr);
                        stdin = ConsoleInput.Capture(rdr);

                        try { chain.Next(cmd1.ToArray()); }
                        finally
                        {
                            ConsoleInput.Restore(rdr, stdin);
                            ConsoleOutput.Restore(wtr, stdout);
                        }

                        rdr = new StringReader(wtr.ToString());
                    }

                    stdin = ConsoleInput.Capture(rdr);
                    try { chain.Next(args); }
                    finally
                    {
                        ConsoleInput.Restore(rdr, stdin);
                    }
                }
Exemplo n.º 5
0
            public static void IORedirect(ICommandInterpreter ci, ICommandChain chain, string[] args)
            {
                TextReader rin = null, stdin = Console.In;
                TextWriter rout = null, stdout = Console.Out;

                try
                {
                    int pos;
                    while ((pos = NextRedirect(args)) > 0)
                    {
                        List <string> cmd1 = new List <string>(args);
                        List <string> cmd2 = new List <string>(args);
                        cmd1.RemoveRange(pos, cmd1.Count - pos);
                        cmd2.RemoveRange(0, pos);

                        args = cmd1.ToArray();
                        string file     = String.Join(" ", cmd2.ToArray());
                        bool   isout    = file.StartsWith(">");
                        bool   isappend = isout && file.StartsWith(">>");
                        file = file.TrimStart('>', '<').Trim();

                        if (!isout)
                        {
                            Console.SetIn(rin = File.OpenText(file));
                        }
                        else
                        {
                            Console.SetOut(rout = isappend ? File.AppendText(file) : File.CreateText(file));
                        }
                    }

                    chain.Next(args);
                }
                finally
                {
                    if (rin != null)
                    {
                        Console.SetIn(stdin); rin.Close(); rin.Dispose();
                    }
                    if (rout != null)
                    {
                        Console.SetOut(stdout); rout.Close(); rout.Dispose();
                    }
                }
            }
        /// <summary> returns the chained filters </summary>
        private ICommandChain GetHead()
        {
            ICommandChain chain = _head;

            if (chain == null)
            {
                chain = new LastFilter(this);
                List <ICommandFilter> filters = new List <ICommandFilter>(_filters);
                filters.Sort(PrecedenceOrder);
                filters.Reverse();                //add in reverse order
                foreach (ICommandFilter filter in filters)
                {
                    chain = new FilterChainItem(this, filter, chain);
                }
                _head = chain;
            }
            return(chain);
        }
Exemplo n.º 7
0
 public static void ExceptionFilter(ICommandInterpreter ci, ICommandChain chain, string[] args)
 {
     try { chain.Next(args); }
     catch (System.Threading.ThreadAbortException) { throw; }
     catch (CommandInterpreter.QuitException) { throw; }
     catch (OperationCanceledException) { throw; }
     catch (InterpreterException)
     {
         // Incorrect useage or bad command name...
         throw;
     }
     catch (Exception ex)
     {
         if (args.Length > 0)
         {
             Trace.TraceError("[{0}]: {1}", args[0], ex);
         }
         else
         {
             Trace.TraceError("{0}", ex);
         }
         throw;
     }
 }
Exemplo n.º 8
0
            [CommandFilter]             // <= implied by method signature, exact signature required for all filters
            public static void AddLineNumbers(ICommandInterpreter ci, ICommandChain chain, string[] args)
            {
                string line;

                if (chain == null)
                {
                    // not possible UNLESS you add this filter to the list of commands which is not recommended
                    // since it would generally be easier to just add another method to handle this if/else branch
                    // for you.  However, since it is technically possible to do so this will be tested.
                    Console.WriteLine("{0}", lineNo);
                }
                else
                {
                    bool addLineNumbers = ArgumentList.Remove(ref args, "linenumbers", out line);

                    TextWriter   stdout = Console.Out;
                    StringWriter swout  = new StringWriter();

                    if (addLineNumbers)
                    {
                        Console.SetOut(swout);
                    }

                    chain.Next(args);                     // <= Unless we want to prevent this command from executing, we must call next()

                    if (addLineNumbers)
                    {
                        StringReader r = new StringReader(swout.ToString());

                        while (null != (line = r.ReadLine()))
                        {
                            stdout.WriteLine("{0}: {1}", ++lineNo, line);
                        }
                    }
                }
            }
Exemplo n.º 9
0
            public static void PipeCommands(ICommandInterpreter ci, ICommandChain chain, string[] args)
            {
                int pos;

                while ((pos = NextPipe(args)) > 0)
                {
                    List <string> cmd1 = new List <string>(args);
                    cmd1.RemoveRange(pos, cmd1.Count - pos);
                    List <string> cmd2 = new List <string>(args);
                    cmd2.RemoveRange(0, pos);
                    cmd2[0] = cmd2[0].TrimStart('|');
                    if (cmd2[0].Length == 0)
                    {
                        cmd2.RemoveAt(0);
                    }
                    if (cmd2.Count == 0)
                    {
                        args = cmd1.ToArray();
                        break;
                    }
                    else
                    {
                        args = cmd2.ToArray();
                    }

                    TextWriter   stdout = Console.Out;
                    StringWriter wtr    = new StringWriter();
                    Console.SetOut(wtr);
                    chain.Next(cmd1.ToArray());
                    Console.SetOut(stdout);

                    Console.SetIn(new StringReader(wtr.ToString()));
                }

                chain.Next(args);
            }
Exemplo n.º 10
0
 public FilterChainItem(ICommandInterpreter ci, ICommandFilter filter, ICommandChain next)
 {
     _ci     = Check.NotNull(ci);
     _filter = Check.NotNull(filter);
     _next   = Check.NotNull(next);
 }
Exemplo n.º 11
0
 public FilterChainItem(ICommandInterpreter ci, ICommandFilter filter, ICommandChain next)
 {
     _ci     = ci;
     _filter = filter;
     _next   = next;
 }
 /// <summary>
 /// Adds a command 'filter' that is called for every command invoked enabling custom processing
 /// of arguments and pre/post processing.
 /// </summary>
 public void AddFilter(ICommandFilter filter)
 {
     _filters.Remove(filter);
     _filters.Add(filter);
     _head = null;
 }
Exemplo n.º 13
0
Arquivo: Main.cs Projeto: andy-uq/Echo
 public static void ExceptionFilter(ICommandInterpreter ci, ICommandChain chain, string[] args)
 {
     try
     {
         chain.Next(args);
     }
     catch (System.Threading.ThreadAbortException)
     {
         throw;
     }
     catch (CommandInterpreter.QuitException)
     {
         throw;
     }
     catch (OperationCanceledException)
     {
         throw;
     }
     catch (InterpreterException)
     {
         // Incorrect useage or bad command name...
         throw;
     }
     catch (Exception ex)
     {
         if (args.Length > 0)
             Trace.TraceError("[{0}]: {1}", args[0], ex);
         else
             Trace.TraceError("{0}", ex);
         throw;
     }
 }
Exemplo n.º 14
0
		public FilterChainItem(ICommandInterpreter ci, ICommandFilter filter, ICommandChain next)
		{
			_ci = Check.NotNull(ci);
			_filter = Check.NotNull(filter);
			_next = Check.NotNull(next);
		}
Exemplo n.º 15
0
		public void Run(ICommandInterpreter ci, ICommandChain chain, string[] arguments)
		{
			_filterProc(ci, chain, arguments);
		}
Exemplo n.º 16
0
 public FilterChainItem(ICommandInterpreter ci, ICommandFilter filter, ICommandChain next)
 {
     _ci = ci;
     _filter = filter;
     _next = next;
 }
Exemplo n.º 17
0
 public void Run(ICommandInterpreter ci, ICommandChain chain, string[] arguments)
 {
     _filterProc(ci, chain, arguments);
 }
Exemplo n.º 18
0
Arquivo: Main.cs Projeto: andy-uq/Echo
 public static void HelpFilter(ICommandInterpreter ci, ICommandChain chain, string[] args)
 {
     if (args.Length == 1 && StringComparer.OrdinalIgnoreCase.Equals("help", args[0]) || args[0] == "?")
     {
         chain.Next(args);
         Console.WriteLine(@"Global Options:
      /nologo:  Suppress the logo/copyright message
       /verbosity:  [All] Verbosity level: Off, Error, Warning, Information, Verbose, or All
     ");
     }
     else
     {
         chain.Next(args);
     }
 }