示例#1
0
        public static Action <CommandContext> ParseCommandFromCli(CommandRegistry commandRegistry, string line)
        {
            var commandElements = line.Split(" ");
            int i = 0;

            string?GetNextArgument() => i < commandElements.Length ? commandElements[i++] : null;

            var commandName = GetNextArgument();

            if (commandName == null)
            {
                throw new Exception("Missing command name");
            }

            return(commandRegistry.ParseCommand(commandName, GetNextArgument));
        }
示例#2
0
        public static CommandRegistry RegisterCommands()
        {
            var registry = new CommandRegistry();

            registry.RegisterCommand <DnsEndPoint>("c", "connect", "Connect to a remote server", (target, ctx) =>
            {
                Console.WriteLine($"Connecting to {target.Host}:{target.Port}");
                var tcpClient        = new TcpClient(target.Host, target.Port);
                ctx.ConnectionStream = tcpClient.GetStream();
            });

            registry.RegisterCommand("h", "help", "Display help", ctx =>
            {
                Console.WriteLine("Available commands:");
            });

            registry.RegisterCommand("s", "send", "Send bytes", ctx =>
            {
                if (ctx.ConnectionStream == null)
                {
                    Console.WriteLine("Connect first");
                }
                else
                {
                    var bytes = new byte[ctx.DataLength];
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        bytes[i] = (byte)(i & 0xFF);
                    }

                    Console.WriteLine($"Sending {bytes.Length:N0} bytes");
                    ctx.ConnectionStream.Write(bytes);
                }
            });

            registry.RegisterCommand <int>("l", "length", "Set bytes count", (count, ctx) => ctx.DataLength = count);

            return(registry);
        }
示例#3
0
        public static Action <CommandContext> ParseCommandFromLaunchArguments(CommandRegistry commandRegistry, string[] args, ref int index)
        {
            var i = index;

            string?GetNextArgument() => i < args.Length ? args[i++] : null;

            var commandName = GetNextArgument();

            if (commandName == null)
            {
                throw new Exception("Missing command name");
            }

            if (!commandName.StartsWith("-"))
            {
                throw new Exception($"Unknow option: {commandName}");
            }
            commandName = commandName.Substring(1);

            var command = commandRegistry.ParseCommand(commandName, GetNextArgument);

            index = i;
            return(command);
        }
示例#4
0
 public CommandContext(CommandRegistry commandRegistry)
 {
     CommandRegistry = commandRegistry;
 }