use of org.jline.builtins.Options in project felix by apache.
the class Procedural method doTry.
protected Object doTry(CommandSession session, Process process, Object[] argv) throws Exception {
String[] usage = { "try - try / catch / finally construct", "Usage: try { try-action } [ { catch-action } [ { finally-action } ] ]", " -? --help Show help" };
Options opt = parseOptions(session, usage, argv);
List<Function> functions = getFunctions(opt);
if (functions == null || functions.size() < 1 || functions.size() > 3) {
process.err().println("usage: try { try-action } [ { catch-action } [ { finally-action } ] ]");
process.error(2);
return null;
}
try {
return functions.get(0).execute(session, null);
} catch (BreakException b) {
throw b;
} catch (Exception e) {
session.put("exception", e);
if (functions.size() > 1) {
functions.get(1).execute(session, null);
}
return null;
} finally {
if (functions.size() > 2) {
functions.get(2).execute(session, null);
}
}
}
use of org.jline.builtins.Options in project felix by apache.
the class Procedural method parseOptions.
protected Options parseOptions(CommandSession session, String[] usage, Object[] argv) throws HelpException, OptionException {
try {
Options opt = Options.compile(usage, s -> get(session, s)).parse(argv, true);
if (opt.isSet("help")) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
opt.usage(new PrintStream(baos));
throw new HelpException(baos.toString());
}
return opt;
} catch (IllegalArgumentException e) {
throw new OptionException(e.getMessage(), e);
}
}
use of org.jline.builtins.Options in project felix by apache.
the class Procedural method doUntil.
protected Object doUntil(CommandSession session, Process process, Object[] argv) throws Exception {
String[] usage = { "until - until loop", "Usage: until { condition } { action }", " -? --help Show help" };
Options opt = parseOptions(session, usage, argv);
List<Function> functions = new ArrayList<>();
for (Object o : opt.argObjects()) {
if (o instanceof Function) {
functions.add((Function) o);
} else {
functions = null;
break;
}
}
int length = opt.argObjects().size();
if (length != 2 || functions == null) {
process.err().println("usage: until { condition } { action }");
process.error(2);
return null;
}
while (!isTrue(session, functions.get(0))) {
try {
functions.get(1).execute(session, null);
} catch (BreakException e) {
break;
} catch (ContinueException c) {
continue;
}
}
return null;
}
use of org.jline.builtins.Options in project felix by apache.
the class Shell method gosh.
public Object gosh(CommandSession currentSession, String[] argv) throws Exception {
final String[] usage = { "gosh - execute script with arguments in a new session", " args are available as session variables $1..$9 and $args.", "Usage: gosh [OPTIONS] [script-file [args..]]", " -c --command pass all remaining args to sub-shell", " --nointeractive don't start interactive session", " --nohistory don't save the command history", " --login login shell (same session, reads etc/gosh_profile)", " -s --noshutdown don't shutdown framework when script completes", " -x --xtrace echo commands before execution", " -? --help show help", "If no script-file, an interactive shell is started, type $D to exit." };
Options opt = Options.compile(usage).setOptionsFirst(true).parse(argv);
List<String> args = opt.args();
boolean login = opt.isSet("login");
boolean interactive = !opt.isSet("nointeractive");
if (opt.isSet("help")) {
opt.usage(System.err);
if (login && !opt.isSet("noshutdown")) {
shutdown();
}
return null;
}
if (opt.isSet("command") && args.isEmpty()) {
throw opt.usageError("option --command requires argument(s)");
}
CommandSession session;
if (login) {
session = currentSession;
} else {
session = createChildSession(currentSession);
}
if (opt.isSet("xtrace")) {
session.put("echo", true);
}
Terminal terminal = getTerminal(session);
session.put(Shell.VAR_CONTEXT, context);
session.put(Shell.VAR_PROCESSOR, processor);
session.put(Shell.VAR_SESSION, session);
session.put("#TERM", (Function) (s, arguments) -> terminal.getType());
session.put("#COLUMNS", (Function) (s, arguments) -> terminal.getWidth());
session.put("#LINES", (Function) (s, arguments) -> terminal.getHeight());
session.put("#PWD", (Function) (s, arguments) -> s.currentDir().toString());
if (!opt.isSet("nohistory")) {
session.put(LineReader.HISTORY_FILE, Paths.get(System.getProperty("user.home"), ".gogo.history"));
}
if (tio != null) {
PrintWriter writer = terminal.writer();
PrintStream out = new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
write(new byte[] { (byte) b }, 0, 1);
}
public void write(byte[] b, int off, int len) throws IOException {
writer.write(new String(b, off, len));
}
public void flush() throws IOException {
writer.flush();
}
public void close() throws IOException {
writer.close();
}
});
tio.setStreams(terminal.input(), out, out);
}
try {
LineReader reader;
if (args.isEmpty() && interactive) {
CompletionEnvironment completionEnvironment = new CompletionEnvironment() {
@Override
public Map<String, List<CompletionData>> getCompletions() {
return Shell.getCompletions(session);
}
@Override
public Set<String> getCommands() {
return Shell.getCommands(session);
}
@Override
public String resolveCommand(String command) {
return Shell.resolve(session, command);
}
@Override
public String commandName(String command) {
int idx = command.indexOf(':');
return idx >= 0 ? command.substring(idx + 1) : command;
}
@Override
public Object evaluate(LineReader reader, ParsedLine line, String func) throws Exception {
session.put(Shell.VAR_COMMAND_LINE, line);
return session.execute(func);
}
};
reader = LineReaderBuilder.builder().terminal(terminal).variables(((CommandSessionImpl) session).getVariables()).completer(new org.jline.builtins.Completers.Completer(completionEnvironment)).highlighter(new Highlighter(session)).parser(new Parser()).expander(new Expander(session)).build();
reader.setOpt(LineReader.Option.AUTO_FRESH_LINE);
session.put(Shell.VAR_READER, reader);
session.put(Shell.VAR_COMPLETIONS, new HashMap());
} else {
reader = null;
}
if (login || interactive) {
URI uri = baseURI.resolve("etc/" + profile);
if (!new File(uri).exists()) {
URL url = getClass().getResource("/ext/" + profile);
if (url == null) {
url = getClass().getResource("/" + profile);
}
uri = (url == null) ? null : url.toURI();
}
if (uri != null) {
source(session, uri.toString());
}
}
Object result = null;
if (args.isEmpty()) {
if (interactive) {
result = runShell(session, terminal, reader);
}
} else {
CharSequence program;
if (opt.isSet("command")) {
StringBuilder buf = new StringBuilder();
for (String arg : args) {
if (buf.length() > 0) {
buf.append(' ');
}
buf.append(arg);
}
program = buf;
} else {
URI script = session.currentDir().toUri().resolve(args.remove(0));
// set script arguments
session.put("0", script);
session.put("args", args);
for (int i = 0; i < args.size(); ++i) {
session.put(String.valueOf(i + 1), args.get(i));
}
program = readScript(script);
}
result = session.execute(program);
}
if (login && interactive && !opt.isSet("noshutdown")) {
if (terminal != null) {
terminal.writer().println("gosh: stopping framework");
terminal.flush();
}
shutdown();
}
return result;
} finally {
if (tio != null) {
tio.close();
}
}
}
use of org.jline.builtins.Options in project felix by apache.
the class Ssh method sshd.
public void sshd(CommandSession session, String[] argv) throws IOException {
final String[] usage = { "sshd - start an ssh server", "Usage: sshd [-i ip] [-p port] start | stop | status", " -i --ip=INTERFACE listen interface (default=127.0.0.1)", " -p --port=PORT listen port (default=" + defaultPort + ")", " -? --help show help" };
Options opt = Options.compile(usage).parse(argv);
List<String> args = opt.args();
if (opt.isSet("help") || args.isEmpty()) {
opt.usage(System.err);
return;
}
String command = args.get(0);
if ("start".equals(command)) {
if (server != null) {
throw new IllegalStateException("sshd is already running on port " + port);
}
ip = opt.get("ip");
port = opt.getNumber("port");
context = session.get(org.apache.felix.gogo.runtime.activator.Activator.CONTEXT);
start();
status();
} else if ("stop".equals(command)) {
if (server == null) {
throw new IllegalStateException("sshd is not running.");
}
stop();
} else if ("status".equals(command)) {
status();
} else {
throw opt.usageError("bad command: " + command);
}
}
Aggregations