Search in sources :

Example 1 with ArgumentParser

use of net.morimekta.console.args.ArgumentParser in project providence by morimekta.

the class TestRunner method main.

public static void main(String... args) throws InterruptedException, TException, IOException {
    System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "off");
    ArgumentParser parser = new ArgumentParser("it-serialization", "SNAPSHOT", "Serialization speed Integration test");
    TestOptions options = new TestOptions();
    try {
        parser.add(new Flag("--help", "h?", "Show this help message", options.help::set));
        parser.add(new Flag("--no_progress", "s", "No progress output", options.no_progress::set));
        parser.add(new Option("--runs", "R", "RUNS", "Number of runs to do", i32(options.runs::set), "100"));
        parser.add(new Option("--generate", "g", "NUM", "Number of messages to generate", i32(options.generate::set), "10"));
        parser.add(new Option("--fill_rate", "r", "FRACT", "Fill rate, 0.0 - 1.0 of fields in generated classes", dbl(options.generator::setFillRate), "1.0"));
        parser.add(new Option("--load", null, "FILE", "File to load data from. Required test to match content", file(options.file::set)));
        parser.add(new Option("--format", "f", "FMT", "Which format to test", oneOf(Format.class, options.format::set), "all"));
        parser.add(new Argument("test", "Which test to run", oneOf(TestOptions.Test.class, options.test::set), "all"));
        parser.parse(args);
        if (options.help.get()) {
            System.out.println("Usage: " + parser.getSingleLineUsage());
            System.out.println();
            parser.printUsage(System.out);
            System.exit(0);
        }
        parser.validate();
        if (options.test.get() == null || options.test.get() == TestOptions.Test.containers) {
            new TestRunner<>(net.morimekta.test.providence.serialization.containers.ManyContainers.kDescriptor, net.morimekta.test.thrift.serialization.containers.ManyContainers::new, options).run();
        }
        if (options.test.get() == null || options.test.get() == TestOptions.Test.fields) {
            new TestRunner<>(net.morimekta.test.providence.serialization.messages.ManyFields.kDescriptor, net.morimekta.test.thrift.serialization.messages.ManyFields::new, options).run();
        }
        if (options.test.get() == null || options.test.get() == TestOptions.Test.r_fields) {
            new TestRunner<>(net.morimekta.test.providence.serialization.messages.ManyRequiredFields.kDescriptor, net.morimekta.test.thrift.serialization.messages.ManyRequiredFields::new, options).run();
        }
        if (options.test.get() == null || options.test.get() == TestOptions.Test.deep) {
            new TestRunner<>(net.morimekta.test.providence.serialization.deep.DeepStructure.kDescriptor, net.morimekta.test.thrift.serialization.deep.DeepStructure::new, options).run();
        }
        System.out.println();
        System.exit(0);
    } catch (ArgumentException e) {
        System.err.println("Argument: " + e.getMessage());
        System.err.println();
        System.err.println("Usage: " + parser.getSingleLineUsage());
        System.err.println();
        parser.printUsage(System.err);
    } catch (TException e) {
        e.printStackTrace();
        System.err.println("TException" + e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.exit(1);
}
Also used : TException(org.apache.thrift.TException) Argument(net.morimekta.console.args.Argument) ArgumentParser(net.morimekta.console.args.ArgumentParser) Flag(net.morimekta.console.args.Flag) TException(org.apache.thrift.TException) IOException(java.io.IOException) ArgumentException(net.morimekta.console.args.ArgumentException) Parser.file(net.morimekta.console.util.Parser.file) Option(net.morimekta.console.args.Option) ArgumentException(net.morimekta.console.args.ArgumentException)

Example 2 with ArgumentParser

use of net.morimekta.console.args.ArgumentParser in project providence by morimekta.

the class ConvertOptions 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", "Include from directories.", dir(this::addInclude), "${PWD}", 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 Flag("--strict", "S", "Read incoming messages strictly.", this::setStrict));
    parser.add(new Flag("--list-types", "L", "List the parsed types based on the input files", this::setListTypes));
    parser.add(new Argument("type", "Qualified identifier name from definitions to use for parsing source file.", this::setType));
    return parser;
}
Also used : Argument(net.morimekta.console.args.Argument) Option(net.morimekta.console.args.Option) ArgumentParser(net.morimekta.console.args.ArgumentParser) Flag(net.morimekta.console.args.Flag) ConvertStreamParser(net.morimekta.providence.tools.common.formats.ConvertStreamParser)

Example 3 with ArgumentParser

use of net.morimekta.console.args.ArgumentParser in project providence by morimekta.

the class GeneratorOptions method getArgumentParser.

public ArgumentParser getArgumentParser(String prog, String description) throws IOException {
    ArgumentOptions opts = ArgumentOptions.defaults(tty).withMaxUsageWidth(120);
    ArgumentParser parser = new ArgumentParser(prog, Utils.getVersionString(), description, opts);
    parser.add(new Option("--gen", "g", "generator", "Generate files for this language spec.", new GeneratorSpecParser(this::getFactories).andApply(this::setGenerator)));
    parser.add(new HelpOption("--help", "h?", "Show this help or about language.", this::getFactories, this::setHelp));
    parser.add(new Flag("--verbose", "V", "Show verbose output and error messages.", this::setVerbose));
    parser.add(new Flag("--version", "v", "Show program version.", this::setVersion));
    parser.add(new Option("--rc", null, "FILE", "Providence RC to use", file(this::setRc), "~" + File.separator + ".pvdrc"));
    parser.add(new Option("--include", "I", "dir", "Allow includes of files in directory", dir(this::addInclude), null, true, false, false));
    parser.add(new Option("--out", "o", "dir", "Output directory", outputDir(this::setOut), "${PWD}"));
    parser.add(new Flag("--require-field-id", null, "Require all fields to have a defined ID", this::setRequireFieldId));
    parser.add(new Flag("--require-enum-value", null, "Require all enum values to have a defined ID", this::setRequireEnumValue));
    parser.add(new Flag("--allow-language-reserved-names", null, "Allow language-reserved words in type names", this::setAllowLanguageReservedNames, true));
    parser.add(new Flag("--no-language-reserved-names", null, "Do not allow language-reserved words in type names", b -> this.setAllowLanguageReservedNames(!b)));
    parser.add(new Flag("--skip-if-missing-namespace", "N", "Skip generation for files without requested namespace", this::setSkipIfMissingNamespace));
    parser.add(new Option("--add-generator", null, "FILE", "Add extra generator .jar file", file(extraGenerators::add)));
    parser.add(new Argument("file", "Files to compile.", file(this::addFile), null, null, true, true, false));
    return parser;
}
Also used : FactoryLoader(net.morimekta.providence.generator.util.FactoryLoader) HelpSpec(net.morimekta.providence.tools.generator.options.HelpSpec) Parser.dir(net.morimekta.console.util.Parser.dir) URL(java.net.URL) Generator(net.morimekta.providence.generator.Generator) GeneratorSpecParser(net.morimekta.providence.tools.generator.options.GeneratorSpecParser) ProgramParser(net.morimekta.providence.reflect.parser.ProgramParser) Argument(net.morimekta.console.args.Argument) ArrayList(java.util.ArrayList) Flag(net.morimekta.console.args.Flag) HelpOption(net.morimekta.providence.tools.generator.options.HelpOption) Utils(net.morimekta.providence.tools.common.Utils) Map(java.util.Map) GeneratorSpec(net.morimekta.providence.tools.generator.options.GeneratorSpec) ArgumentParser(net.morimekta.console.args.ArgumentParser) Parser.file(net.morimekta.console.util.Parser.file) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) ArgumentOptions(net.morimekta.console.args.ArgumentOptions) GeneratorException(net.morimekta.providence.generator.GeneratorException) CommonOptions(net.morimekta.providence.tools.common.CommonOptions) IOException(java.io.IOException) GeneratorFactory(net.morimekta.providence.generator.GeneratorFactory) File(java.io.File) ArgumentException(net.morimekta.console.args.ArgumentException) List(java.util.List) JsonGeneratorFactory(net.morimekta.providence.generator.format.json.JsonGeneratorFactory) STTY(net.morimekta.console.util.STTY) UncheckedProvidenceConfigException(net.morimekta.providence.config.UncheckedProvidenceConfigException) TreeMap(java.util.TreeMap) FileManager(net.morimekta.providence.generator.util.FileManager) ThriftProgramParser(net.morimekta.providence.reflect.parser.ThriftProgramParser) Option(net.morimekta.console.args.Option) Parser.outputDir(net.morimekta.console.util.Parser.outputDir) TypeLoader(net.morimekta.providence.reflect.TypeLoader) ProvidenceTools(net.morimekta.providence.tools.common.ProvidenceTools) Argument(net.morimekta.console.args.Argument) GeneratorSpecParser(net.morimekta.providence.tools.generator.options.GeneratorSpecParser) HelpOption(net.morimekta.providence.tools.generator.options.HelpOption) HelpOption(net.morimekta.providence.tools.generator.options.HelpOption) Option(net.morimekta.console.args.Option) ArgumentOptions(net.morimekta.console.args.ArgumentOptions) ArgumentParser(net.morimekta.console.args.ArgumentParser) Flag(net.morimekta.console.args.Flag)

Example 4 with ArgumentParser

use of net.morimekta.console.args.ArgumentParser in project providence by morimekta.

the class RPC method run.

@SuppressWarnings("unchecked")
void run(String... args) {
    try {
        ArgumentParser cli = options.getArgumentParser("pvdrpc", "Providence RPC Tool");
        try {
            cli.parse(args);
            if (options.showHelp()) {
                System.out.println("Providence RPC Tool - " + getVersionString());
                System.out.println("Usage: " + cli.getSingleLineUsage());
                System.out.println();
                System.out.println("Example code to run:");
                System.out.println("$ cat call.json | pvdrpc -I thrift/ -s cal.Calculator http://localhost:8080/service");
                System.out.println("$ pvdrpc -i binary,file:my.data -f json_protocol -I thrift/ -s cal.Calculator http://localhost:8080/service");
                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;
            } else if (options.showVersion()) {
                System.out.println("Providence RPC Tool - " + getVersionString());
                return;
            }
            cli.validate();
            MessageReader in = options.getInput();
            MessageWriter out = options.getOutput();
            PService service = options.getDefinition();
            PServiceCallHandler handler = options.getHandler();
            PServiceCall call = in.read(service);
            in.close();
            PServiceCall resp = handler.handleCall(call, service);
            out.write(resp);
            out.separator();
            out.close();
            if (options.out.base64mime && options.out.file == null) {
                System.out.println();
            }
            return;
        } catch (ConnectException e) {
            System.out.flush();
            System.err.format("Unable to connect to %s: %s%n", options.endpoint, e.getMessage());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (HttpResponseException e) {
            System.out.flush();
            System.err.println("Received " + e.getStatusCode() + " " + e.getStatusMessage());
            System.err.println(" - from: " + options.endpoint);
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (ArgumentException e) {
            System.out.flush();
            System.err.println(e.getMessage());
            System.err.println("Usage: " + cli.getSingleLineUsage());
            System.err.println();
            System.err.println("Run $ pvdrpc --help # for available options.");
        } catch (SerializerException e) {
            System.out.flush();
            System.err.println("Serializer error: " + e.asString());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (UncheckedIOException | IOException e) {
            System.out.flush();
            System.err.println("I/O error: " + e.getMessage());
            if (options.verbose()) {
                System.out.flush();
                e.printStackTrace();
            }
        } catch (IllegalArgumentException e) {
            System.out.flush();
            System.err.println("Internal Error: " + e.getMessage());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        }
    } catch (RuntimeException | IOException e) {
        System.out.flush();
        System.err.println("Unchecked exception: " + e.getMessage());
        if (options.verbose()) {
            System.out.flush();
            e.printStackTrace();
        }
    }
    System.err.flush();
    exit(1);
}
Also used : MessageReader(net.morimekta.providence.mio.MessageReader) HttpResponseException(com.google.api.client.http.HttpResponseException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) SerializerException(net.morimekta.providence.serializer.SerializerException) Format(net.morimekta.providence.tools.common.formats.Format) PServiceCallHandler(net.morimekta.providence.PServiceCallHandler) PServiceCall(net.morimekta.providence.PServiceCall) MessageWriter(net.morimekta.providence.mio.MessageWriter) ArgumentException(net.morimekta.console.args.ArgumentException) PService(net.morimekta.providence.descriptor.PService) ConnectException(java.net.ConnectException)

Example 5 with ArgumentParser

use of net.morimekta.console.args.ArgumentParser in project providence by morimekta.

the class Config method run.

public void run(String... args) {
    ConfigOptions op = new ConfigOptions(tty);
    try {
        ArgumentParser cli = op.getArgumentParser("pvdcfg", "Providence Config Tool");
        try {
            cli.parse(args);
            if (op.showHelp()) {
                System.out.println(cli.getDescription() + " - " + getVersionString());
                System.out.println("Usage: " + cli.getSingleLineUsage());
                System.out.println();
                cli.printUsage(System.out);
                System.out.println();
                System.out.println("Available Commands:");
                System.out.println();
                op.getCommandSet().printUsage(System.out);
                return;
            } else if (op.showVersion()) {
                System.out.println(cli.getDescription() + " - " + getVersionString());
                return;
            }
            cli.validate();
            op.execute();
            return;
        } catch (ArgumentException e) {
            System.err.println("Invalid argument: " + e.getMessage());
            System.err.println("Usage: " + cli.getSingleLineUsage());
            if (op.verbose()) {
                e.printStackTrace();
            }
        } catch (ParseException e) {
            System.out.flush();
            System.err.println(e.asString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (TokenizerException e) {
            System.out.flush();
            System.err.println(e.asString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (SerializerException e) {
            System.out.flush();
            System.err.println("Serialization error: " + e.toString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (IOException | RuntimeException e) {
            System.out.flush();
            System.err.println("IO Error: " + e.toString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        System.out.flush();
        System.err.println("Unhandled exception: " + e.toString());
        if (op.verbose()) {
            System.err.println();
            e.printStackTrace();
        }
    }
    exit(1);
}
Also used : TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) ArgumentException(net.morimekta.console.args.ArgumentException) ParseException(net.morimekta.providence.reflect.parser.ParseException) IOException(java.io.IOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) SerializerException(net.morimekta.providence.serializer.SerializerException) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) IOException(java.io.IOException) ParseException(net.morimekta.providence.reflect.parser.ParseException) SerializerException(net.morimekta.providence.serializer.SerializerException) ArgumentException(net.morimekta.console.args.ArgumentException)

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