Skip to content

Commit

Permalink
Repl demo: removed now obsolete SubCommands class example
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 26, 2020
1 parent 8be9242 commit 9cb2708
Showing 1 changed file with 18 additions and 71 deletions.
89 changes: 18 additions & 71 deletions demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,62 +63,35 @@
*/
public class Repl {

/**
* CommandRegistry that have commands which manage in different way command parameters.
*
*/
private static class SubCommands extends JlineCommandRegistry implements CommandRegistry {
private static class MyCommands extends JlineCommandRegistry implements CommandRegistry {
private LineReader reader;
private Supplier<Path> workDir;

public SubCommands() {
public MyCommands(Supplier<Path> workDir) {
super();
this.workDir = workDir;
Map<String,CommandMethods> commandExecute = new HashMap<>();
commandExecute.put("cmdKo1", new CommandMethods(this::cmd1, this::defaultCompleter));
commandExecute.put("cmdKo2", new CommandMethods(this::cmd2, this::defaultCompleter));
commandExecute.put("cmdOk", new CommandMethods(this::cmd3, this::defaultCompleter));
commandExecute.put("tput", new CommandMethods(this::tput, this::tputCompleter));
commandExecute.put("testkey", new CommandMethods(this::testkey, this::defaultCompleter));
commandExecute.put("clear", new CommandMethods(this::clear, this::defaultCompleter));
commandExecute.put("!", new CommandMethods(this::shell, this::defaultCompleter));
commandExecute.put("objarg", new CommandMethods(this::objarg, this::defaultCompleter));
registerCommands(commandExecute);
}

private Object cmd1(CommandInput input) {
final String[] usage = {
"cmdKo1 - parse input.args, return opt.argObjects[0]",
" works only with string parameters",
"Usage: cmdKo1 [OBJECT]",
" -? --help Displays command help"
};
Object out = null;
try {
Options opt = parseOptions(usage, input.args());
List<Object> xargs = opt.argObjects();
out = xargs.size() > 0 ? xargs.get(0) : null;
} catch (Exception e) {
saveException(e);
}
return out;
public void setLineReader(LineReader reader) {
this.reader = reader;
}

private Object cmd2(CommandInput input) {
final String[] usage = {
"cmdKo2 - parse input.xargs, return opt.args[0]",
" works only with string parameters",
"Usage: cmdKo2 [OBJECT]",
" -? --help Displays command help"
};
Object out = null;
try {
Options opt = parseOptions(usage, input.xargs());
List<String> args = opt.args();
out = args.size() > 0 ? args.get(0) : null;
} catch (Exception e) {
saveException(e);
}
return out;
private Terminal terminal() {
return reader.getTerminal();
}

private Object cmd3(CommandInput input) {
private Object objarg(CommandInput input) {
final String[] usage = {
"cmdOk - parse input.xargs, return opt.argObjects[0]",
" manage correctly object parameters",
"Usage: cmdOk [OBJECT]",
"objarg - manage correctly object parameters",
" parse input.xargs, return opt.argObjects[0]",
"Usage: objarg [OBJECT]",
" -? --help Displays command help"
};
Object out = null;
Expand All @@ -132,31 +105,6 @@ private Object cmd3(CommandInput input) {
return out;
}

}

private static class MyCommands extends JlineCommandRegistry implements CommandRegistry {
private LineReader reader;
private Supplier<Path> workDir;

public MyCommands(Supplier<Path> workDir) {
super();
this.workDir = workDir;
Map<String,CommandMethods> commandExecute = new HashMap<>();
commandExecute.put("tput", new CommandMethods(this::tput, this::tputCompleter));
commandExecute.put("testkey", new CommandMethods(this::testkey, this::defaultCompleter));
commandExecute.put("clear", new CommandMethods(this::clear, this::defaultCompleter));
commandExecute.put("!", new CommandMethods(this::shell, this::defaultCompleter));
registerCommands(commandExecute);
}

public void setLineReader(LineReader reader) {
this.reader = reader;
}

private Terminal terminal() {
return reader.getTerminal();
}

private void tput(CommandInput input) {
final String[] usage = {
"tput - put terminal capability",
Expand Down Expand Up @@ -325,7 +273,6 @@ public static void main(String[] args) {
Builtins builtins = new Builtins(Repl::workDir, configPath, (String fun)-> {return new ConsoleEngine.WidgetCreator(consoleEngine, fun);});
MyCommands myCommands = new MyCommands(Repl::workDir);
SystemRegistryImpl systemRegistry = new SystemRegistryImpl(parser, terminal, Repl::workDir, configPath);
systemRegistry.register("command", new SubCommands());
systemRegistry.register("groovy", new GroovyCommand(scriptEngine, consoleEngine));
systemRegistry.setCommandRegistries(consoleEngine, builtins, myCommands);
//
Expand Down

0 comments on commit 9cb2708

Please sign in to comment.