예제 #1
0
파일: Command.cs 프로젝트: danzhu/launcher
        public Level Analyze(Session ses, Tokens tokens)
        {
            Level lvl = new Level(this);
            ses.Leaf = lvl;
            tokens.SetLevel(lvl);

            // Match parameters
            for (int i = 0; i < Parameters.Count; i++)
            {
                Parameter p = Parameters[i];
                p.Update(ses);

                MatchResult res = p.Analyze(ses, tokens, lvl);

                // if no tokens left, leave unmatched alone (to match previous params)
                // else, update so that later ones would not go back before the param
                // this also ensures auto completion even if the word is already completed
                // TODO: unmatched might jump back (e.g. "task name -s")
                if (tokens.Count == 0)
                    break;

                if (res == MatchResult.Matched)
                    lvl.FirstUnmatchedParam = i + 1;
                else if (res == MatchResult.Extensible)
                    lvl.FirstUnmatchedParam = i;
                else if (p.Required) // implied "Failed"
                {
                    // TODO: handle "required"
                    ses.Error(new ArgumentException($"{p} requires a valid value"));
                    break;
                }
            }

            // Raise event
            InputChanged?.Invoke(this, new ScriptEventArgs(ses, lvl.Arguments, lvl.Child));

            return lvl;
        }
예제 #2
0
파일: Command.cs 프로젝트: danzhu/launcher
        public void InitializeScript(Session ses)
        {
            if (ScriptSource == null || Script != null)
                return;

            ses.Engine.LoadScriptEngine();

            try
            {
                Script = ses.Engine.ScriptEngine.CreateScope();
                Script.SetVariable("engine", ses.Engine);
                Script.SetVariable("command", this);
                ses.Engine.ScriptEngine.ExecuteFile(ScriptSource, Script);
            }
            catch (Exception e)
            {
                ses.Error(e);
            }
        }
예제 #3
0
파일: Command.cs 프로젝트: danzhu/launcher
        public bool Execute(Session ses, Arguments arg, Level child)
        {
            if (!Executable)
                return false;

            try
            {
                if (Action != null)
                    Process.Start(Action);

                if (Executed != null)
                {
                    ScriptEventArgs e = new ScriptEventArgs(ses, arg, child);
                    Executed?.Invoke(this, e);
                    return e.Succeeded;
                }

                return true;
            }
            catch (Exception e)
            {
                ses.Error(e);
                return false;
            }
        }