Skip to content

Commit

Permalink
subcommands: added support for object parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Mar 20, 2020
1 parent 5a24955 commit b00a9a0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public CommandInput(String command, String[] args, Object[] xargs, CommandRegist
this.xargs = xargs;
this.args = new String[xargs.length];
for (int i = 0; i < xargs.length; i++) {
this.args[i] = xargs[i] != null ? xargs[i].toString() : "";
this.args[i] = xargs[i] != null ? xargs[i].toString() : null;
}
}
}
Expand Down
26 changes: 10 additions & 16 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,7 @@ public Object invoke(String command, Object... args) throws Exception {
if (id > -1) {
out = commandRegistries[id].invoke(commandSession(), command, args);
} else if (isLocalCommand(command)) {
String[] _args = new String[args.length];
for (int i = 0; i < args.length; i++) {
if (!(args[i] instanceof String)) {
throw new IllegalArgumentException();
}
_args[i] = args[i].toString();
}
out = localExecute(command, _args);
out = localExecute(command, args);
} else if (consoleId != null) {
out = consoleEngine().invoke(commandSession(), command, args);
}
Expand All @@ -336,12 +329,12 @@ public Object execute(String command, String[] args) throws Exception {
return out;
}

public Object localExecute(String command, String[] args) throws Exception {
public Object localExecute(String command, Object[] args) throws Exception {
if (!isLocalCommand(command)) {
throw new IllegalArgumentException();
}
Object out = commandExecute.get(command).executeFunction()
.apply(new Builtins.CommandInput(command, args, commandSession()));
.apply(new Builtins.CommandInput(command, null, args, commandSession()));
if (exception != null) {
throw exception;
}
Expand Down Expand Up @@ -1039,7 +1032,8 @@ public Object execute(String line) throws Exception {
boolean consoleScript = false;
if (parser.validCommandName(cmd.command())) {
if (isLocalCommand(cmd.command())) {
out = localExecute(cmd.command(), cmd.args());
out = localExecute(cmd.command()
, consoleId != null ? consoleEngine().expandParameters(cmd.args()) : cmd.args());
} else {
int id = registryId(cmd.command());
if (id > -1) {
Expand Down Expand Up @@ -1342,9 +1336,9 @@ private Object exit(Builtins.CommandInput input) {
private Object subcommand(Builtins.CommandInput input) {
Object out = null;
try {
out = subcommands.get(input.command()).execute(input.session()
out = subcommands.get(input.command()).invoke(input.session()
, input.args()[0]
, Arrays.copyOfRange(input.args(), 1, input.args().length));
, Arrays.copyOfRange(input.xargs(), 1, input.xargs().length));
} catch (Exception e) {
exception = e;
}
Expand All @@ -1365,15 +1359,15 @@ private List<OptDesc> commandOptions(String command) {

private List<String> registryNames() {
List<String> out = new ArrayList<>();
out.add("System");
out.add("Builtins");
if (consoleId != null) {
out.add("Scripts");
}
for (CommandRegistry r : commandRegistries) {
if (isBuiltinRegistry(r)) {
continue;
if (!isBuiltinRegistry(r)) {
out.add(r.name());
}
out.add(r.name());
}
return out;
}
Expand Down
60 changes: 33 additions & 27 deletions demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,78 +103,84 @@ public Widgets.CmdDesc commandDescription(String command) {
return out;
}
try {
String[] args = new String[] {};
Object[] args = new Object[] {};
if (!command.equals("help")) {
args = new String[] {"--help"};
args = new Object[] {"--help"};
}
execute(new CommandSession(), command, args);
invoke(new CommandSession(), command, args);
} catch (HelpException e) {
out = Builtins.compileCommandDescription(e.getMessage());
} catch (Exception e) {
}
return out;
}


private void cmd1(Builtins.CommandInput input) {
private Object cmd1(Builtins.CommandInput input) {
final String[] usage = {
"cmd1 - cmd1",
"Usage: cmd1",
"cmd1 - cmd1 parse args, return opt.argObjects().get(0)",
"Usage: cmd1 [OBJECT]",
" -? --help Displays command help"
};
Options opt = Options.compile(usage).parse(input.args());
if (opt.isSet("help")) {
exception = new HelpException(opt.usage());
return;
return null;
}
List<Object> xargs = opt.argObjects();
return xargs.size() > 0 ? xargs.get(0) : null;
}

private void cmd2(Builtins.CommandInput input) {
private Object cmd2(Builtins.CommandInput input) {
final String[] usage = {
"cmd2 - cmd2",
"cmd2 - cmd2 parse xargs, return opt.args().get(0)",
"Usage: cmd2",
" -? --help Displays command help"
};
Options opt = Options.compile(usage).parse(input.args());
Options opt = Options.compile(usage).parse(input.xargs());
if (opt.isSet("help")) {
exception = new HelpException(opt.usage());
return;
return null;
}
List<String> args = opt.args();
return args.size() > 0 ? args.get(0) : null;
}

private void cmd3(Builtins.CommandInput input) {
private Object cmd3(Builtins.CommandInput input) {
final String[] usage = {
"cmd3 - cmd3",
"Usage: cmd3",
"cmd3 - cmd3 parse xargs, return opt.argObjects().get(0)",
"Usage: cmd3 [OBJECT]",
" -? --help Displays command help"
};
Options opt = Options.compile(usage).parse(input.args());
Options opt = Options.compile(usage).parse(input.xargs());
if (opt.isSet("help")) {
exception = new HelpException(opt.usage());
return;
return null;
}
List<Object> xargs = opt.argObjects();
return xargs.size() > 0 ? xargs.get(0) : null;
}

private void help(Builtins.CommandInput input) {
private Object help(Builtins.CommandInput input) {
final String[] usage = {
" - execute cmd subcommands",
"Usage: cmd1",
" cmd2",
" cmd3",
" help",
"Usage: cmd1 [OBJECT]",
" cmd2 [OBJECT]",
" cmd3 [OBJECT]",
" help"
};
Options opt = Options.compile(usage).parse(input.args());
exception = new HelpException(opt.usage());
return;
return null;
}

public Object execute(CommandRegistry.CommandSession session, String command, String[] args) throws Exception {
@Override
public Object invoke(CommandSession session, String command, Object... args) throws Exception {
exception = null;
commandExecute.get(command(command)).execute().accept(new Builtins.CommandInput(command, args, session));
Object out = commandExecute.get(command(command)).executeFunction().apply(new Builtins.CommandInput(command, null, args, session));
if (exception != null) {
throw exception;
}
return null;
return out;
}

public Completers.SystemCompleter compileCompleters() {
Expand All @@ -187,7 +193,7 @@ public Completers.SystemCompleter compileCompleters() {

private List<OptDesc> commandOptions(String command) {
try {
execute(new CommandRegistry.CommandSession(), command, new String[] {"--help"});
invoke(new CommandRegistry.CommandSession(), command, new Object[] {"--help"});
} catch (HelpException e) {
return Builtins.compileCommandOptions(e.getMessage());
} catch (Exception e) {
Expand Down

0 comments on commit b00a9a0

Please sign in to comment.