use of net.morimekta.console.args.ArgumentParser in project providence by morimekta.
the class Convert method run.
@SuppressWarnings("unchecked")
void run(String... args) {
try {
ArgumentParser cli = options.getArgumentParser("pvd", "Providence Converter");
try {
cli.parse(args);
if (options.showHelp()) {
System.out.println(cli.getProgramDescription());
System.out.println("Usage: " + cli.getSingleLineUsage());
System.out.println();
System.out.println("Example code to run:");
System.out.println("$ cat call.json | pvd -I thrift/ -S cal.Calculator");
System.out.println("$ pvd -i binary,file:my.data -o json_protocol -I thrift/ cal.Operation");
System.out.println();
System.out.println("Note that when handling service calls, only 1 call can be converted.");
System.out.println();
cli.printUsage(System.out);
System.out.println();
System.out.println("Available formats are:");
for (Format format : Format.values()) {
System.out.println(String.format(" - %-20s : %s", format.name(), format.desc));
}
return;
}
if (options.showVersion()) {
System.out.println(cli.getProgramDescription());
return;
}
if (options.listTypes) {
ProgramRegistry registry = options.getProgramRegistry();
for (ProgramTypeRegistry pr : registry.getLoadedRegistries()) {
CProgram program = pr.getProgram();
System.out.println(program.getProgramFilePath() + ":");
for (PDeclaredDescriptor dd : program.getDeclaredTypes()) {
if (dd instanceof CStructDescriptor) {
System.out.println(" struct " + dd.getQualifiedName());
} else if (dd instanceof CUnionDescriptor) {
System.out.println(" union " + dd.getQualifiedName());
} else if (dd instanceof CExceptionDescriptor) {
System.out.println(" exception " + dd.getQualifiedName());
}
}
for (PService s : program.getServices()) {
System.out.println(" service " + s.getQualifiedName());
}
}
return;
}
cli.validate();
if (options.getDefinition() == null) {
MessageReader in = options.getServiceInput();
MessageWriter out = options.getServiceOutput();
PService service = options.getServiceDefinition();
PServiceCall call = in.read(service);
in.close();
out.write(call);
out.separator();
out.close();
// TODO: Validate we don't have garbage data after call.
if (options.out.base64mime && options.out.file == null) {
System.out.println();
}
} else {
AtomicInteger num = new AtomicInteger(0);
int size = options.getInput().peek(m -> num.incrementAndGet()).collect(options.getOutput());
if (num.get() == 0 || size == 0) {
throw new IOException("No data");
}
if (options.out.base64mime && options.out.file == null) {
System.out.println();
}
}
return;
} catch (ArgumentException e) {
System.err.println(e.getMessage());
System.out.println("Usage: " + cli.getSingleLineUsage());
System.err.println();
System.err.println("Run $ pvd --help # for available options.");
if (options.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (SerializerException e) {
System.out.flush();
System.err.println();
System.err.println(e.asString());
if (options.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (UncheckedIOException | IOException e) {
System.out.flush();
System.err.println();
System.err.println("I/O error: " + e.getMessage());
if (options.verbose()) {
System.err.println();
e.printStackTrace();
}
}
} catch (Exception e) {
System.out.flush();
System.err.println();
System.err.println("Unchecked exception: " + e.getMessage());
if (options.verbose()) {
System.err.println();
e.printStackTrace();
}
}
exit(1);
}
use of net.morimekta.console.args.ArgumentParser in project providence by morimekta.
the class GeneratorMain method run.
public void run(String... args) {
try {
ArgumentParser cli = options.getArgumentParser("pvdgen", "Providence generator");
cli.parse(args);
if (options.isHelp()) {
System.out.println("Providence generator - " + Utils.getVersionString());
System.out.println("Usage: pvdgen [-I dir] [-o dir] -g generator[:opt[,opt]*] file...");
System.out.println();
if (options.help.factory != null) {
System.out.format("%s : %s%n", options.help.factory.generatorName(), options.help.factory.generatorDescription());
System.out.println();
System.out.println("Available options");
System.out.println();
options.help.factory.printGeneratorOptionsHelp(System.out);
} else {
System.out.println("Example code to run:");
System.out.println("$ pvdgen -I thrift/ --out target/ --gen java:android thrift/the-one.thrift");
System.out.println();
cli.printUsage(System.out);
System.out.println();
System.out.println("Available generators:");
Map<String, GeneratorFactory> factories = options.getFactories();
for (GeneratorFactory lang : factories.values()) {
System.out.format(" - %-10s : %s%n", lang.generatorName(), lang.generatorDescription());
}
}
return;
} else if (options.version) {
System.out.println("Providence generator - " + Utils.getVersionString());
return;
}
cli.validate();
ProgramParser parser = options.getParser();
List<File> includes = options.getIncludes();
List<File> input = options.getInputFiles();
TypeLoader loader = new TypeLoader(includes, parser);
Generator generator = options.getGenerator(loader);
for (File f : input) {
ProgramTypeRegistry registry = loader.load(f);
if (options.skipIfMissingNamespace && registry.getProgram().getNamespaceForLanguage(options.gen.factory.generatorName()) == null) {
System.out.println("Skipping (no " + options.gen.factory.generatorName() + " namespace) " + f.getCanonicalFile().getAbsolutePath());
}
generator.generate(loader.load(f));
}
generator.generateGlobal(loader.getProgramRegistry(), input);
return;
} catch (ArgumentException e) {
System.err.println("Usage: pvdgen [-I dir] [-o dir] -g generator[:opt[,opt]*] file...");
System.err.println(e.getLocalizedMessage());
System.err.println();
System.err.println("Run $ pvdgen --help # for available options.");
if (options.verbose) {
e.printStackTrace();
}
} catch (ParseException e) {
System.err.println(e.asString());
if (options.verbose) {
e.printStackTrace();
}
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
if (options.verbose) {
e.printStackTrace();
}
} catch (GeneratorException e) {
System.err.println("Generator error: " + e.getMessage());
if (options.verbose) {
e.printStackTrace();
}
} catch (IOException e) {
System.err.println("I/O error: " + e.getMessage());
if (options.verbose) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
exit(1);
}
use of net.morimekta.console.args.ArgumentParser in project providence by morimekta.
the class RPCOptions method getArgumentParser.
@Override
public ArgumentParser getArgumentParser(String prog, String description) throws IOException {
ArgumentParser parser = super.getArgumentParser(prog, description);
parser.add(new Option("--include", "I", "dir", "Allow includes of files in directory", dir(this::addInclude), null, true, false, false));
parser.add(new Option("--in", "i", "spec", "Input specification", new ConvertStreamParser(in).andApply(this::setIn), in.toString()));
parser.add(new Option("--out", "o", "spec", "Output Specification", new ConvertStreamParser(out).andApply(this::setOut), out.toString()));
parser.add(new Option("--service", "s", "srv", "Qualified identifier name from definitions to use for parsing source file.", this::setService, null, false, true, false));
parser.add(new Option("--format", "f", "fmt", "Request RPC format", oneOf(Format.class, this::setFormat), format.name()));
parser.add(new Option("--connect_timeout", "C", "ms", "Connection timeout in milliseconds. 0 means infinite.", i32(this::setConnectTimeout), "10000"));
parser.add(new Option("--read_timeout", "R", "ms", "Request timeout in milliseconds. 0 means infinite.", i32(this::setReadTimeout), "10000"));
parser.add(new Option("--header", "H", "hdr", "Header to set on the request, K/V separated by ':'.", this::addHeaders, null, true, false, false));
parser.add(new Flag("--strict", "S", "Read incoming messages strictly.", this::setStrict));
parser.add(new Argument("URI", "The endpoint URI", this::setEndpoint, null, s -> {
try {
if (!s.contains("://"))
return false;
URI uri = new URI(s);
if (isNullOrEmpty(uri.getAuthority())) {
throw new ArgumentException("Missing authority in URI: '" + s + "'");
}
return true;
} catch (URISyntaxException e) {
throw new ArgumentException(e, e.getMessage());
}
}, false, true, false));
return parser;
}
Aggregations