Search in sources :

Example 11 with ArgumentParser

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);
}
Also used : ArgumentParser(net.morimekta.console.args.ArgumentParser) ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) CProgram(net.morimekta.providence.reflect.contained.CProgram) IOException(java.io.IOException) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) MessageWriter(net.morimekta.providence.mio.MessageWriter) ProgramRegistry(net.morimekta.providence.reflect.util.ProgramRegistry) ArgumentException(net.morimekta.console.args.ArgumentException) CUnionDescriptor(net.morimekta.providence.reflect.contained.CUnionDescriptor) UncheckedIOException(java.io.UncheckedIOException) PService(net.morimekta.providence.descriptor.PService) STTY(net.morimekta.console.util.STTY) PServiceCall(net.morimekta.providence.PServiceCall) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CExceptionDescriptor(net.morimekta.providence.reflect.contained.CExceptionDescriptor) CStructDescriptor(net.morimekta.providence.reflect.contained.CStructDescriptor) MessageReader(net.morimekta.providence.mio.MessageReader) Format(net.morimekta.providence.tools.common.formats.Format) SerializerException(net.morimekta.providence.serializer.SerializerException) ProgramRegistry(net.morimekta.providence.reflect.util.ProgramRegistry) CUnionDescriptor(net.morimekta.providence.reflect.contained.CUnionDescriptor) MessageReader(net.morimekta.providence.mio.MessageReader) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) SerializerException(net.morimekta.providence.serializer.SerializerException) IOException(java.io.IOException) ArgumentException(net.morimekta.console.args.ArgumentException) UncheckedIOException(java.io.UncheckedIOException) SerializerException(net.morimekta.providence.serializer.SerializerException) Format(net.morimekta.providence.tools.common.formats.Format) ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CProgram(net.morimekta.providence.reflect.contained.CProgram) PServiceCall(net.morimekta.providence.PServiceCall) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) MessageWriter(net.morimekta.providence.mio.MessageWriter) CStructDescriptor(net.morimekta.providence.reflect.contained.CStructDescriptor) ArgumentException(net.morimekta.console.args.ArgumentException) CExceptionDescriptor(net.morimekta.providence.reflect.contained.CExceptionDescriptor) PService(net.morimekta.providence.descriptor.PService)

Example 12 with ArgumentParser

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);
}
Also used : ProgramParser(net.morimekta.providence.reflect.parser.ProgramParser) TypeLoader(net.morimekta.providence.reflect.TypeLoader) IOException(java.io.IOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) GeneratorException(net.morimekta.providence.generator.GeneratorException) IOException(java.io.IOException) ParseException(net.morimekta.providence.reflect.parser.ParseException) ArgumentException(net.morimekta.console.args.ArgumentException) GeneratorFactory(net.morimekta.providence.generator.GeneratorFactory) ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) ArgumentException(net.morimekta.console.args.ArgumentException) ParseException(net.morimekta.providence.reflect.parser.ParseException) GeneratorException(net.morimekta.providence.generator.GeneratorException) File(java.io.File) Generator(net.morimekta.providence.generator.Generator)

Example 13 with ArgumentParser

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;
}
Also used : ThriftSerializerProvider(net.morimekta.providence.thrift.ThriftSerializerProvider) Parser.dir(net.morimekta.console.util.Parser.dir) URISyntaxException(java.net.URISyntaxException) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) Parser.i32(net.morimekta.console.util.Parser.i32) HashMap(java.util.HashMap) HttpClientHandler(net.morimekta.providence.client.HttpClientHandler) MessageWriter(net.morimekta.providence.mio.MessageWriter) ParseException(net.morimekta.providence.reflect.parser.ParseException) ConvertStream(net.morimekta.providence.tools.common.formats.ConvertStream) Argument(net.morimekta.console.args.Argument) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SocketClientHandler(net.morimekta.providence.thrift.client.SocketClientHandler) Flag(net.morimekta.console.args.Flag) PService(net.morimekta.providence.descriptor.PService) Map(java.util.Map) GenericUrl(com.google.api.client.http.GenericUrl) NonblockingSocketClientHandler(net.morimekta.providence.thrift.client.NonblockingSocketClientHandler) FormatUtils(net.morimekta.providence.tools.common.formats.FormatUtils) Strings(net.morimekta.util.Strings) ConvertStreamParser(net.morimekta.providence.tools.common.formats.ConvertStreamParser) URI(java.net.URI) Parser.oneOf(net.morimekta.console.util.Parser.oneOf) ArgumentParser(net.morimekta.console.args.ArgumentParser) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) Serializer(net.morimekta.providence.serializer.Serializer) SerializerProvider(net.morimekta.providence.serializer.SerializerProvider) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) CProgram(net.morimekta.providence.reflect.contained.CProgram) HttpTransport(com.google.api.client.http.HttpTransport) PServiceCallHandler(net.morimekta.providence.PServiceCallHandler) SetHeadersInitializer(net.morimekta.providence.tools.rpc.utils.SetHeadersInitializer) Set(java.util.Set) CommonOptions(net.morimekta.providence.tools.common.CommonOptions) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) File(java.io.File) ArgumentException(net.morimekta.console.args.ArgumentException) List(java.util.List) STTY(net.morimekta.console.util.STTY) ThriftProgramParser(net.morimekta.providence.reflect.parser.ThriftProgramParser) Option(net.morimekta.console.args.Option) MessageReader(net.morimekta.providence.mio.MessageReader) Format(net.morimekta.providence.tools.common.formats.Format) TypeLoader(net.morimekta.providence.reflect.TypeLoader) Argument(net.morimekta.console.args.Argument) Option(net.morimekta.console.args.Option) ArgumentException(net.morimekta.console.args.ArgumentException) URISyntaxException(java.net.URISyntaxException) ArgumentParser(net.morimekta.console.args.ArgumentParser) Flag(net.morimekta.console.args.Flag) URI(java.net.URI) ConvertStreamParser(net.morimekta.providence.tools.common.formats.ConvertStreamParser)

Aggregations

ArgumentParser (net.morimekta.console.args.ArgumentParser)13 IOException (java.io.IOException)8 Argument (net.morimekta.console.args.Argument)7 ArgumentException (net.morimekta.console.args.ArgumentException)7 Option (net.morimekta.console.args.Option)7 Flag (net.morimekta.console.args.Flag)6 File (java.io.File)4 STTY (net.morimekta.console.util.STTY)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Parser.dir (net.morimekta.console.util.Parser.dir)3 PService (net.morimekta.providence.descriptor.PService)3 MessageReader (net.morimekta.providence.mio.MessageReader)3 MessageWriter (net.morimekta.providence.mio.MessageWriter)3 TypeLoader (net.morimekta.providence.reflect.TypeLoader)3 ParseException (net.morimekta.providence.reflect.parser.ParseException)3 UncheckedIOException (java.io.UncheckedIOException)2 Map (java.util.Map)2 Parser.file (net.morimekta.console.util.Parser.file)2 PServiceCall (net.morimekta.providence.PServiceCall)2