Search in sources :

Example 16 with OptionException

use of joptsimple.OptionException in project PayFile by mikehearn.

the class CLI method main.

public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();
    //Logger.getLogger("").setLevel(Level.OFF);
    // allow client to choose another network for testing by passing through an argument.
    OptionParser parser = new OptionParser();
    parser.accepts("network").withRequiredArg().withValuesConvertedBy(regex("(mainnet)|(testnet)|(regtest)")).defaultsTo("mainnet");
    parser.accepts("server").withRequiredArg().required();
    parser.accepts("help").forHelp();
    parser.formatHelpWith(new BuiltinHelpFormatter(120, 10));
    OptionSet options;
    try {
        options = parser.parse(args);
    } catch (OptionException e) {
        System.err.println(e.getMessage());
        System.err.println("");
        parser.printHelpOn(System.err);
        return;
    }
    if (options.has("help")) {
        parser.printHelpOn(System.out);
        return;
    }
    if (options.valueOf("network").equals(("testnet"))) {
        params = TestNet3Params.get();
        filePrefix = "testnet-";
    } else if (options.valueOf("network").equals(("mainnet"))) {
        params = MainNetParams.get();
        filePrefix = "";
    } else if (options.valueOf("network").equals(("regtest"))) {
        params = RegTestParams.get();
        filePrefix = "regtest-";
    }
    String server = options.valueOf("server").toString();
    System.out.println("Connecting to " + server);
    Socket socket = new Socket(server, 18754);
    final CLI cli = new CLI(socket);
    ShellFactory.createConsoleShell(server, "PayFile", cli).commandLoop();
    cli.shutdown();
}
Also used : BuiltinHelpFormatter(joptsimple.BuiltinHelpFormatter) OptionException(joptsimple.OptionException) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser) Socket(java.net.Socket)

Example 17 with OptionException

use of joptsimple.OptionException in project voldemort by voldemort.

the class ReplaceNodeCLI method main.

public static void main(String[] args) throws Exception {
    OptionParser parser = null;
    OptionSet options = null;
    try {
        parser = getParser();
        options = parser.parse(args);
    } catch (OptionException oe) {
        parser.printHelpOn(System.out);
        printUsageAndDie("Exception when parsing arguments : " + oe.getMessage());
        return;
    }
    /* validate options */
    if (options.hasArgument("help")) {
        parser.printHelpOn(System.out);
        printUsage();
        return;
    }
    if (!options.hasArgument("url") || !options.hasArgument("node") || !options.hasArgument("newurl")) {
        parser.printHelpOn(System.out);
        printUsageAndDie("Missing a required argument.");
        return;
    }
    String url = (String) options.valueOf("url");
    String newUrl = (String) options.valueOf("newurl");
    int nodeId = ((Integer) options.valueOf("node")).intValue();
    boolean skipRestore = options.has("skip-restore");
    int parallelism = ((Integer) options.valueOf("parallelism")).intValue();
    if (parallelism <= 0) {
        Utils.croak(" parallelism " + parallelism + " should be a positive integer ");
    }
    ReplaceNodeCLI nodeReplacer = new ReplaceNodeCLI(url, nodeId, newUrl, skipRestore, parallelism);
    try {
        nodeReplacer.execute();
    } catch (VoldemortApplicationException e) {
        logger.error("Error during node replace", e);
        Utils.croak(e.getMessage());
    }
}
Also used : VoldemortApplicationException(voldemort.VoldemortApplicationException) OptionException(joptsimple.OptionException) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser)

Example 18 with OptionException

use of joptsimple.OptionException in project voldemort by voldemort.

the class PartitionAnalysisCLI method getValidOptions.

private static OptionSet getValidOptions(String[] args) {
    OptionSet options = null;
    try {
        options = parser.parse(args);
    } catch (OptionException oe) {
        printUsageAndDie("Exception when parsing arguments : " + oe.getMessage());
    }
    if (options.has("help")) {
        printUsage();
        System.exit(0);
    }
    Set<String> missing = CmdUtils.missing(options, "cluster", "stores");
    if (missing.size() > 0) {
        printUsageAndDie("Missing required arguments: " + Joiner.on(", ").join(missing));
    }
    return options;
}
Also used : OptionException(joptsimple.OptionException) OptionSet(joptsimple.OptionSet)

Example 19 with OptionException

use of joptsimple.OptionException in project voldemort by voldemort.

the class ReadOnlyReplicationHelperCLI method getValidOptions.

private static OptionSet getValidOptions(String[] args) {
    OptionSet options = null;
    try {
        options = parser.parse(args);
    } catch (OptionException oe) {
        printUsageAndDie("Exception when parsing arguments : " + oe.getMessage());
    }
    if (options.has(OPT_HELP)) {
        printUsage();
        System.exit(0);
    }
    Set<String> missing = CmdUtils.missing(options, OPT_URL, OPT_NODE);
    if (missing.size() > 0) {
        printUsageAndDie("Missing required arguments: " + Joiner.on(", ").join(missing));
    }
    return options;
}
Also used : OptionException(joptsimple.OptionException) OptionSet(joptsimple.OptionSet)

Example 20 with OptionException

use of joptsimple.OptionException in project aic-praise by aic-sri-international.

the class PRAiSE method getArgs.

public static SGSolverArgs getArgs(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
    SGSolverArgs result = new SGSolverArgs();
    OptionParser parser = new OptionParser();
    // Optional
    OptionSpec<String> language = parser.accepts("language", "input model language (code), allowed values are " + getLegalModelLanguageCodesDescription()).withRequiredArg().ofType(String.class);
    OptionSpec<String> query = parser.accepts("query", "query to run over all input models").withRequiredArg().ofType(String.class);
    OptionSpec<File> outputFile = parser.accepts("output", "output file name (defaults to stdout).").withRequiredArg().ofType(File.class);
    // Help
    OptionSpec<Void> help = parser.accepts("help", "command line options help").forHelp();
    //
    String usage = "java " + PRAiSE.class.getName() + " [--help] [--language language_code] [--query global_query_string] [--output output_file_name] inputModelFile ..." + "\n\n" + "This command reads a set of models from input files and executes a set of queries on each of them.\n\n" + "The models are obtained in the following manner:\n" + "- input files may be one or more; they can be .praise files (saved from the PRAiSE editor and solver) or plain text files.\n" + "- each .praise input file contains possibly multiple pages, each with a model and a set of queries for it (see PRAiSE editor and solver).\n" + "- multiple plain text input files are combined into a single model (not combined with .praise models).\n\n" + "The queries are obtained in the following manner:\n" + "- each page in each .praise file contains a list of queries for its specific model\n" + "- queries specified with --query option will apply to all models from all .praise files and to the model from combined plain text input files.\n\n" + "Evidence can be encoded as deterministic statements (see examples in PRAiSE editor and solver).\n";
    List<String> errors = new ArrayList<>();
    List<String> warnings = new ArrayList<>();
    try {
        OptionSet options = parser.parse(args);
        if (options.has(help)) {
            System.out.println(usage);
            parser.printHelpOn(System.out);
            System.exit(0);
        }
        for (Object inputFileName : options.nonOptionArguments()) {
            File inputFile = new File(inputFileName.toString());
            if (inputFile.exists()) {
                result.inputFiles.add(inputFile);
            } else {
                errors.add("Input file [" + inputFileName + "] is not a file or does not exist.");
            }
        }
        if (result.inputFiles.size() == 0) {
            errors.add("No input files specified");
        }
        if (options.has(query)) {
            for (String commandLineQuery : options.valuesOf(query)) {
                result.globalQueries.add(commandLineQuery);
            }
        }
        if (options.has(language)) {
            String languageCode = options.valueOf(language);
            result.inputLanguage = findLanguageModel(languageCode);
            if (result.inputLanguage == null) {
                errors.add("Input language code, " + languageCode + ", is not legal. Legal values are " + getLegalModelLanguageCodesDescription() + ".");
            }
        } else {
            // Guess the language from the input file names
            result.inputLanguage = guessLanguageModel(result.inputFiles);
            if (result.inputLanguage == null) {
                errors.add("Unable to guess the input language from the given input file name extensions. Legal input language codes are " + getLegalModelLanguageCodesDescription() + ".");
            }
        }
        if (options.has(outputFile)) {
            File outFile = options.valueOf(outputFile);
            if (outFile.exists()) {
                if (outFile.isDirectory()) {
                    errors.add("Output file specified [" + outFile.getAbsolutePath() + "] is a directory, must be a legal file name.");
                } else {
                    warnings.add("Warning output file [" + outFile.getAbsolutePath() + "] already exists, will be overwritten.");
                }
            }
            // Only setup file output if there are no errors.
            if (errors.size() == 0) {
                result.out = new PrintStream(outFile, FILE_CHARSET.name());
            }
        }
    } catch (OptionException oe) {
        errors.add(oe.getMessage());
    }
    if (warnings.size() > 0) {
        warnings.forEach(warning -> System.err.println("WARNING: " + warning));
    }
    if (errors.size() > 0) {
        errors.forEach(error -> System.err.println("ERROR: " + error));
        System.err.println(usage);
        parser.printHelpOn(System.err);
        System.exit(1);
    }
    return result;
}
Also used : PrintStream(java.io.PrintStream) ArrayList(java.util.ArrayList) OptionException(joptsimple.OptionException) OptionParser(joptsimple.OptionParser) OptionSet(joptsimple.OptionSet) File(java.io.File)

Aggregations

OptionException (joptsimple.OptionException)23 OptionSet (joptsimple.OptionSet)20 OptionParser (joptsimple.OptionParser)14 File (java.io.File)4 IOException (java.io.IOException)4 List (java.util.List)4 ArrayList (java.util.ArrayList)3 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2 BitsquareEnvironment (io.bitsquare.app.BitsquareEnvironment)2 PrintWriter (java.io.PrintWriter)2 LinkedList (java.util.LinkedList)2 ThreadFactory (java.util.concurrent.ThreadFactory)2 BuiltinHelpFormatter (joptsimple.BuiltinHelpFormatter)2 VoldemortApplicationException (voldemort.VoldemortApplicationException)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 StringWriter (java.io.StringWriter)1 Socket (java.net.Socket)1