コード例 #1
0
ファイル: Command.cs プロジェクト: alexrp/augmentrex
        protected T Parse <T>(AugmentrexContext context, string[] args)
            where T : class
        {
            return(new Parser(opts =>
            {
                opts.AutoHelp = false;
                opts.AutoVersion = false;
                opts.CaseInsensitiveEnumValues = true;
                opts.CaseSensitive = false;
            }).ParseArguments <T>(args).MapResult(
                       result => result,
                       errors =>
            {
                var builder = SentenceBuilder.Create();

                foreach (var error in errors)
                {
                    context.ErrorLine("{0}", builder.FormatError(error));
                }

                context.Line();
                PrintHelp(context, this, true);
                context.Line();

                return null;
            }));
        }
コード例 #2
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            var opts = Parse <HelpOptions>(context, args);

            if (opts == null)
            {
                return(null);
            }

            if (opts.Commands.Any())
            {
                var cmds    = opts.Commands.Select(x => (x, CommandInterpreter.GetCommand(x)));
                var unknown = false;

                foreach (var(name, cmd) in cmds)
                {
                    if (cmd == null)
                    {
                        unknown = true;
                        context.ErrorLine("Unknown command '{0}'.", name);
                    }
                }

                if (unknown)
                {
                    return(null);
                }

                context.Line();

                foreach (var(_, cmd) in cmds)
                {
                    PrintHelp(context, cmd, true);
                    context.Line();
                }
            }
            else
            {
                context.Line();
                context.InfoLine("Available commands:");
                context.Line();

                foreach (var cmd in CommandInterpreter.Commands)
                {
                    PrintHelp(context, cmd, false);
                    context.Line();
                }
            }

            return(null);
        }
コード例 #3
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            var opts = Parse <KeyOptions>(context, args);

            if (opts == null)
            {
                return(null);
            }

            if (!opts.Add && !opts.Delete)
            {
                foreach (var kvp in _bindings)
                {
                    context.InfoLine("{0} = {1}", kvp.Key, kvp.Value);
                }

                return(null);
            }

            var info = new HotKeyInfo(opts.Key, opts.Alt, opts.Control, opts.Shift);

            if (_handler == null)
            {
                void KeyHandler(HotKeyInfo info)
                {
                    var freq = context.Configuration.HotKeyBeepFrequency;

                    if (freq != 0)
                    {
                        context.Ipc.Channel.Beep(freq, context.Configuration.HotKeyBeepDuration);
                    }

                    if (_bindings.TryGetValue(info, out var command))
                    {
                        context.Interpreter.RunCommand(command, true);
                    }
                }

                _handler = new HotKeyHandler(KeyHandler);
            }

            if (opts.Add)
            {
                var command = string.Join(" ", opts.Fragments);

                if (string.IsNullOrWhiteSpace(command))
                {
                    context.ErrorLine("No command given.");

                    return(null);
                }

                if (_bindings.TryAdd(info, command))
                {
                    context.HotKeys.Add(info, _handler);

                    context.InfoLine("Added key binding: {0} = {1}", info, command);
                }
                else
                {
                    context.ErrorLine("Key binding already exists: {0} = {1}", info, _bindings[info]);
                }
            }

            if (opts.Delete)
            {
                if (_bindings.Remove(info, out var command))
                {
                    context.InfoLine("Deleted key binding: {0} = {1}", info, command);
                }
                else
                {
                    context.ErrorLine("Key binding not found: {0}", info);
                }
            }

            return(null);
        }
コード例 #4
0
ファイル: EvaluateCommand.cs プロジェクト: alexrp/augmentrex
        public override int?Run(AugmentrexContext context, string[] args)
        {
            var opts = Parse <EvaluateOptions>(context, args);

            if (opts == null)
            {
                return(null);
            }

            if (_script == null || opts.Clear)
            {
                _script = CSharpScript.Create(string.Empty,
                                              ScriptOptions.Default.WithFilePath("<hgl>")
                                              .WithImports(ScriptOptions.Default.Imports.AddRange(
                                                               Assembly.GetExecutingAssembly().DefinedTypes.Select(x => x.Namespace).Where(x => x != null).Distinct()))
                                              .WithLanguageVersion(LanguageVersion.CSharp8)
                                              .WithReferences(Assembly.GetExecutingAssembly()),
                                              typeof(Globals));
            }

            var code = string.Join(" ", opts.Fragments);

            if (string.IsNullOrWhiteSpace(code))
            {
                return(null);
            }

            Script script = _script.ContinueWith(code);

            Task <ScriptState> task;

            try
            {
                task = script.RunAsync(new Globals(context));
            }
            catch (CompilationErrorException ex)
            {
                foreach (var diag in ex.Diagnostics)
                {
                    context.ErrorLine("{0}", diag.ToString());
                }

                return(null);
            }

            try
            {
                task.Wait();
            }
            catch (AggregateException ex)
            {
                context.ErrorLine("{0}", ex.InnerException);

                return(null);
            }

            _script = script;

            var result = task.Result.ReturnValue;

            context.InfoLine("{0} it = {1}", result?.GetType() ?? typeof(object), result ?? "null");

            return(null);
        }