use of org.jline.builtins.Options in project felix by apache.
the class Procedural method doNot.
protected Boolean doNot(CommandSession session, Process process, Object[] argv) throws Exception {
String[] usage = { "not - return the opposite condition", "Usage: not { condition }", " -? --help Show help" };
Options opt = parseOptions(session, usage, argv);
List<Function> functions = getFunctions(opt);
if (functions == null || functions.size() != 1) {
process.err().println("usage: not { condition }");
process.error(2);
return null;
}
return !isTrue(session, functions.get(0));
}
use of org.jline.builtins.Options in project karaf 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 } [do] { action }", " -? --help Show help" };
Options opt = parseOptions(session, usage, argv);
Function condition = null;
Function action = null;
int step = 0;
boolean error = false;
for (Object obj : opt.argObjects()) {
if (condition == null) {
if (obj instanceof Function) {
condition = (Function) obj;
} else {
error = true;
break;
}
step = 1;
} else if ("do".equals(obj)) {
if (step != 1) {
error = true;
break;
}
step = 2;
} else if (step == 1 || step == 2) {
if (obj instanceof Function) {
action = (Function) obj;
} else {
error = true;
break;
}
step = 3;
} else {
error = true;
break;
}
}
error |= condition == null;
error |= action == null;
if (error) {
process.err().println("usage: until { condition } [do] { action }");
process.error(2);
return null;
}
while (!isTrue(session, condition)) {
try {
action.execute(session, null);
} catch (BreakException e) {
break;
} catch (ContinueException c) {
continue;
}
}
return null;
}
use of org.jline.builtins.Options in project karaf 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 karaf by apache.
the class Procedural method doEach.
protected List<Object> doEach(CommandSession session, Process process, Object[] argv) throws Exception {
String[] usage = { "each - loop over the elements", "Usage: each [-r] elements [do] { closure }", " elements an array to iterate on", " closure a closure to call", " -? --help Show help", " -r --result Return a list containing each iteration result" };
Options opt = parseOptions(session, usage, argv);
Collection<Object> elements = getElements(opt);
if (opt.argObjects().size() > 0 && "do".equals(opt.argObjects().get(0))) {
opt.argObjects().remove(0);
}
List<Function> functions = getFunctions(opt);
if (elements == null || functions == null || functions.size() != 1) {
process.err().println("usage: each elements [do] { closure }");
process.err().println(" elements: an array to iterate on");
process.err().println(" closure: a function or closure to call");
process.error(2);
return null;
}
List<Object> args = new ArrayList<>();
List<Object> results = new ArrayList<>();
args.add(null);
for (Object x : elements) {
checkInterrupt();
args.set(0, x);
try {
results.add(functions.get(0).execute(session, args));
} catch (BreakException b) {
break;
} catch (ContinueException c) {
continue;
}
}
return opt.isSet("result") ? results : null;
}
use of org.jline.builtins.Options in project felix by apache.
the class Builtin method tac.
/*
* the following methods depend on the internals of the runtime implementation.
* ideally, they should be available via some API.
*/
public Object tac(CommandSession session, String[] argv) throws IOException {
final String[] usage = { "tac - capture stdin as String or List and optionally write to file.", "Usage: tac [-al] [FILE]", " -a --append append to FILE", " -l --list return List<String>", " -? --help show help" };
Process process = Process.Utils.current();
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
opt.usage(process.err());
return null;
}
List<String> args = opt.args();
BufferedWriter fw = null;
if (args.size() == 1) {
Path path = session.currentDir().resolve(args.get(0));
Set<OpenOption> options = new HashSet<>();
options.add(StandardOpenOption.WRITE);
options.add(StandardOpenOption.CREATE);
if (opt.isSet("append")) {
options.add(StandardOpenOption.APPEND);
} else {
options.add(StandardOpenOption.TRUNCATE_EXISTING);
}
fw = Files.newBufferedWriter(path, StandardCharsets.UTF_8, options.toArray(new OpenOption[options.size()]));
}
StringWriter sw = new StringWriter();
BufferedReader rdr = new BufferedReader(new InputStreamReader(process.in()));
ArrayList<String> list = null;
if (opt.isSet("list")) {
list = new ArrayList<String>();
}
boolean first = true;
String s;
while ((s = rdr.readLine()) != null) {
if (list != null) {
list.add(s);
} else {
if (!first) {
sw.write(' ');
}
first = false;
sw.write(s);
}
if (fw != null) {
fw.write(s);
fw.newLine();
}
}
if (fw != null) {
fw.close();
}
return list != null ? list : sw.toString();
}
Aggregations