Archived
0

передача параметров в команду

This commit is contained in:
2020-05-06 13:52:44 +03:00
parent fba59bb009
commit cf16e9373f
4 changed files with 21 additions and 8 deletions

View File

@@ -31,15 +31,18 @@ public class Main {
final ParsedLine parsedLine = reader.getParser().parse(line, 0); final ParsedLine parsedLine = reader.getParser().parse(line, 0);
final String commandName = parsedLine.word(); final String commandName = parsedLine.word();
final String[] argv = parsedLine.words().subList(1, parsedLine.words().size()).toArray(new String[0]);
if (commandService.hasCommand(commandName)) { if (commandService.hasCommand(commandName)) {
commandService.execute(session, commandName); commandService.execute(session, commandName, argv);
} }
terminalWriter.println("line = " + String.join(", ", parsedLine.words())); terminalWriter.println("line = " + String.join(", ", parsedLine.words()));
} }
} catch (UserInterruptException ignore) { } catch (UserInterruptException ignore) {
// ignore // ignore
} catch (Exception e) {
e.printStackTrace();
} }
} }
} }

View File

@@ -6,5 +6,5 @@ public interface CommandService extends CommandRegistry {
void register(String commandName, Command command); void register(String commandName, Command command);
void execute(CommandSession session, String commandName); Object execute(CommandSession session, String commandName);
} }

View File

@@ -21,8 +21,14 @@ public class CommandServiceImpl implements CommandService {
)); ));
} }
public void execute(CommandSession session, String commandName) { public Object execute(CommandSession session, String command) {
commands.get(commandName).execute().accept(new Builtins.CommandInput(commandName, new Object[0], session)); return execute(session, command, new String[0]);
}
@Override
public Object execute(CommandSession session, String command, String[] args) {
commands.get(command).execute().accept(new Builtins.CommandInput(command, args, session));
return null;
} }
@Override @Override

View File

@@ -1,15 +1,19 @@
package mc.server.shell; package mc.server.shell;
import org.jline.builtins.Builtins; import org.jline.builtins.Builtins.CommandInput;
import java.io.PrintWriter; import java.io.PrintWriter;
public class SystemPropertiesCommand implements Command { public class SystemPropertiesCommand implements Command {
@Override @Override
public void execute(Builtins.CommandInput commandInput) { public void execute(CommandInput input) {
final PrintWriter writer = commandInput.terminal().writer(); final PrintWriter writer = input.terminal().writer();
final String[] args = input.args();
writer.printf("args = [%s]\n", String.join(", ", args));
System.getProperties().forEach((key, value) -> System.getProperties().forEach((key, value) ->
writer.println(String.format("%s = %s", key, value.toString()))); writer.printf("%s = %s\n", key, value.toString()));
} }
} }