Beispiel #1
0
        public ParseCommand(HCContext context)
        {
            _context = context;

            IsCommand("parse", "Parses a word");
            SkipsCommandSummaryBeforeRunning();
            HasAdditionalArguments(1, "<word>");
        }
Beispiel #2
0
        public TracingCommand(HCContext context)
        {
            _context = context;

            IsCommand("tracing", "Turns tracing on and off");

            AllowsAnyAdditionalArguments("[on|off]");
            SkipsCommandSummaryBeforeRunning();
        }
Beispiel #3
0
        public TestCommand(HCContext context)
        {
            _context        = context;
            _expectedParses = new List <string>();

            IsCommand("test", "Test if a word parse is correct");
            SkipsCommandSummaryBeforeRunning();
            HasOption("p|parse=", "expected parse in the format <form1>:<gloss1>|<form2>:<gloss2>|...", p => _expectedParses.Add(p));
            HasAdditionalArguments(1, "<word>");
        }
Beispiel #4
0
        public StatsCommand(HCContext context)
        {
            _context = context;

            IsCommand("stats", "Displays statistics on word parses and tests.");
            SkipsCommandSummaryBeforeRunning();
            HasOption("p|parse", "displays word parse statistics", o => _parse = true);
            HasOption("t|test", "displays test statistics", o => _test         = true);
            HasOption("r|reset", "resets the gathered statistics", o => _reset = true);
        }
Beispiel #5
0
        public static int Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            string inputFile   = null;
            string outputFile  = null;
            string scriptFile  = null;
            bool   showHelp    = false;
            bool   quitOnError = true;

            var p = new OptionSet
            {
                { "i|input-file=", "read configuration from {FILE}", value => inputFile = value },
                { "o|output-file=", "write results to {FILE}", value => outputFile = value },
                { "s|script-file=", "runs commands from {FILE}", value => scriptFile = value },
                { "c|continue", "continues when an error occurs while loading the configuration", value => quitOnError = value == null },
                { "h|help", "show this help message and exit", value => showHelp = value != null }
            };

            try
            {
                p.Parse(args);
            }
            catch (OptionException)
            {
                ShowHelp(p);
                return(-1);
            }

            if (showHelp || string.IsNullOrEmpty(inputFile))
            {
                ShowHelp(p);
                return(-1);
            }

            HCContext  context;
            TextWriter output = null;

            try
            {
                if (!string.IsNullOrEmpty(outputFile))
                {
                    output = new StreamWriter(outputFile);
                }

                Console.Write("Reading configuration file \"{0}\"... ", Path.GetFileName(inputFile));
                Language language = XmlLanguageLoader.Load(inputFile, quitOnError ? (Action <Exception, string>)null : (ex, id) => { });
                Console.WriteLine("done.");

                context = new HCContext(language, output ?? Console.Out);
                Console.Write("Compiling rules... ");
                context.Compile();
                Console.WriteLine("done.");
                Console.WriteLine("{0} loaded.", language.Name);
                Console.WriteLine();
            }
            catch (IOException ioe)
            {
                Console.WriteLine();
                Console.WriteLine("IO Error: " + ioe.Message);
                output?.Close();
                return(-1);
            }
            catch (Exception e)
            {
                Console.WriteLine();
                Console.WriteLine("Load Error: " + e.Message);
                output?.Close();
                return(-1);
            }

            ConsoleCommand[] commands = { new ParseCommand(context), new TracingCommand(context), new TestCommand(context), new StatsCommand(context) };

            string input;

            if (!string.IsNullOrEmpty(scriptFile))
            {
                using (var scriptReader = new StreamReader(scriptFile))
                {
                    input = scriptReader.ReadLine();
                    while (input != null)
                    {
                        if (!input.Trim().StartsWith("#") && input.Trim() != "")
                        {
                            string[] cmdArgs = CommandLineParser.Parse(input);
                            ConsoleCommandDispatcher.DispatchCommand(commands, cmdArgs, context.Out);
                        }
                        input = scriptReader.ReadLine();
                    }
                }
            }
            else
            {
                Console.Write("> ");
                input = Console.ReadLine();
                while (input != null && input.Trim() != "exit")
                {
                    if (input.Trim().IsOneOf("?", "help"))
                    {
                        ConsoleHelp.ShowSummaryOfCommands(commands, Console.Out);
                    }
                    else
                    {
                        string[] cmdArgs = CommandLineParser.Parse(input);
                        ConsoleCommandDispatcher.DispatchCommand(commands, cmdArgs, context.Out);
                    }
                    Console.Write("> ");
                    input = Console.ReadLine();
                }
            }

            output?.Close();

            return(0);
        }