Search in sources :

Example 31 with JCommander

use of com.beust.jcommander.JCommander in project logging-log4j2 by apache.

the class BasicCommandLineArguments method parseCommandLine.

public static <T extends BasicCommandLineArguments> T parseCommandLine(final String[] mainArgs, final Class<?> clazz, final T args) {
    final JCommander jCommander = new JCommander(args);
    jCommander.setProgramName(clazz.getName());
    // for sanity
    jCommander.setCaseSensitiveOptions(false);
    jCommander.parse(mainArgs);
    if (args.isHelp()) {
        jCommander.usage();
    }
    return args;
}
Also used : JCommander(com.beust.jcommander.JCommander)

Example 32 with JCommander

use of com.beust.jcommander.JCommander in project GeoGig by boundlessgeo.

the class GeogigCLI method newCommandParser.

public JCommander newCommandParser() {
    JCommander jc = new JCommander(this);
    jc.setProgramName("geogig");
    for (Key<?> cmd : findCommands()) {
        Object obj = commandsInjector.getInstance(cmd);
        if (obj instanceof CLICommand || obj instanceof CLICommandExtension) {
            jc.addCommand(obj);
        }
    }
    return jc;
}
Also used : JCommander(com.beust.jcommander.JCommander)

Example 33 with JCommander

use of com.beust.jcommander.JCommander in project GeoGig by boundlessgeo.

the class GeogigCLI method executeInternal.

/**
     * Executes a command.
     * 
     * @param args
     * @throws exceptions thrown by the executed commands.
     */
private void executeInternal(String... args) throws ParameterException, CommandFailedException, IOException, CannotRunGeogigOperationException {
    JCommander mainCommander = newCommandParser();
    if (null == args || args.length == 0) {
        printShortCommandList(mainCommander);
        return;
    }
    {
        args = unalias(args);
        final String commandName = args[0];
        JCommander commandParser = mainCommander.getCommands().get(commandName);
        if (commandParser == null) {
            consoleReader.println(args[0] + " is not a geogig command. See geogig --help.");
            // check for similar commands
            Map<String, JCommander> candidates = spellCheck(mainCommander.getCommands(), commandName);
            if (!candidates.isEmpty()) {
                String msg = candidates.size() == 1 ? "Did you mean this?" : "Did you mean one of these?";
                consoleReader.println();
                consoleReader.println(msg);
                for (String name : candidates.keySet()) {
                    consoleReader.println("\t" + name);
                }
            }
            consoleReader.flush();
            throw new CommandFailedException(String.format("'%s' is not a command.", commandName));
        }
        Object object = commandParser.getObjects().get(0);
        if (object instanceof CLICommandExtension) {
            args = Arrays.asList(args).subList(1, args.length).toArray(new String[args.length - 1]);
            mainCommander = ((CLICommandExtension) object).getCommandParser();
            if (Lists.newArrayList(args).contains("--help")) {
                printUsage(mainCommander);
                return;
            }
        }
    }
    mainCommander.parse(args);
    final String parsedCommand = mainCommander.getParsedCommand();
    if (null == parsedCommand) {
        if (mainCommander.getObjects().size() == 0) {
            printUsage(mainCommander);
        } else if (mainCommander.getObjects().get(0) instanceof CLICommandExtension) {
            CLICommandExtension extension = (CLICommandExtension) mainCommander.getObjects().get(0);
            printUsage(extension.getCommandParser());
        } else {
            printUsage(mainCommander);
            throw new CommandFailedException();
        }
    } else {
        JCommander jCommander = mainCommander.getCommands().get(parsedCommand);
        List<Object> objects = jCommander.getObjects();
        CLICommand cliCommand = (CLICommand) objects.get(0);
        Class<? extends CLICommand> cmdClass = cliCommand.getClass();
        if (cliCommand instanceof AbstractCommand && ((AbstractCommand) cliCommand).help) {
            ((AbstractCommand) cliCommand).printUsage(this);
            getConsole().flush();
            return;
        }
        Hints hints = gatherHints(cmdClass);
        this.hints = hints;
        if (cmdClass.isAnnotationPresent(RequiresRepository.class) && cmdClass.getAnnotation(RequiresRepository.class).value()) {
            String workingDir;
            Platform platform = getPlatform();
            if (platform == null || platform.pwd() == null) {
                workingDir = "Couln't determine working directory.";
            } else {
                workingDir = platform.pwd().getAbsolutePath();
            }
            if (getGeogig() == null) {
                throw new CommandFailedException("Not in a geogig repository: " + workingDir);
            }
        }
        cliCommand.run(this);
        getConsole().flush();
    }
}
Also used : Hints(org.locationtech.geogig.repository.Hints) DefaultPlatform(org.locationtech.geogig.api.DefaultPlatform) Platform(org.locationtech.geogig.api.Platform) JCommander(com.beust.jcommander.JCommander) Map(java.util.Map) RequiresRepository(org.locationtech.geogig.cli.annotation.RequiresRepository)

Example 34 with JCommander

use of com.beust.jcommander.JCommander in project GeoGig by boundlessgeo.

the class OSMCommandProxy method getCommandParser.

@Override
public JCommander getCommandParser() {
    JCommander commander = new JCommander();
    commander.setProgramName("geogig osm");
    commander.addCommand("import-history", new OSMHistoryImport());
    commander.addCommand("import", new OSMImport());
    commander.addCommand("export", new OSMExport());
    commander.addCommand("download", new OSMDownload());
    commander.addCommand("create-changeset", new CreateOSMChangeset());
    commander.addCommand("map", new OSMMap());
    commander.addCommand("unmap", new OSMUnmap());
    commander.addCommand("export-shp", new OSMExportShp());
    commander.addCommand("export-pg", new OSMExportPG());
    commander.addCommand("export-sl", new OSMExportSL());
    commander.addCommand("apply-diff", new OSMApplyDiff());
    return commander;
}
Also used : OSMExport(org.locationtech.geogig.osm.cli.commands.OSMExport) OSMExportShp(org.locationtech.geogig.osm.cli.commands.OSMExportShp) OSMHistoryImport(org.locationtech.geogig.osm.cli.commands.OSMHistoryImport) OSMExportPG(org.locationtech.geogig.osm.cli.commands.OSMExportPG) OSMUnmap(org.locationtech.geogig.osm.cli.commands.OSMUnmap) OSMExportSL(org.locationtech.geogig.osm.cli.commands.OSMExportSL) JCommander(com.beust.jcommander.JCommander) CreateOSMChangeset(org.locationtech.geogig.osm.cli.commands.CreateOSMChangeset) OSMMap(org.locationtech.geogig.osm.cli.commands.OSMMap) OSMImport(org.locationtech.geogig.osm.cli.commands.OSMImport) OSMApplyDiff(org.locationtech.geogig.osm.cli.commands.OSMApplyDiff) OSMDownload(org.locationtech.geogig.osm.cli.commands.OSMDownload)

Example 35 with JCommander

use of com.beust.jcommander.JCommander in project GeoGig by boundlessgeo.

the class PGCommandProxy method getCommandParser.

/**
     * @return the JCommander parser for this extension
     * @see JCommander
     */
@Override
public JCommander getCommandParser() {
    JCommander commander = new JCommander();
    commander.setProgramName("geogig pg");
    commander.addCommand("import", new PGImport());
    commander.addCommand("list", new PGList());
    commander.addCommand("describe", new PGDescribe());
    commander.addCommand("export", new PGExport());
    return commander;
}
Also used : PGImport(org.locationtech.geogig.geotools.cli.porcelain.PGImport) PGList(org.locationtech.geogig.geotools.cli.porcelain.PGList) JCommander(com.beust.jcommander.JCommander) PGDescribe(org.locationtech.geogig.geotools.cli.porcelain.PGDescribe) PGExport(org.locationtech.geogig.geotools.cli.porcelain.PGExport)

Aggregations

JCommander (com.beust.jcommander.JCommander)65 ParameterException (com.beust.jcommander.ParameterException)15 IOException (java.io.IOException)10 Map (java.util.Map)6 ParameterDescription (com.beust.jcommander.ParameterDescription)4 FileInputStream (java.io.FileInputStream)3 Console (com.beust.jcommander.internal.Console)2 DefaultCommand (com.evolveum.midpoint.cli.common.DefaultCommand)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Charsets (com.google.common.base.Charsets)2 Throwables (com.google.common.base.Throwables)2 HostAndPort (com.google.common.net.HostAndPort)2 Service (com.google.common.util.concurrent.Service)2 CreationException (com.google.inject.CreationException)2 SystemException (com.torodb.core.exceptions.SystemException)2 BackendPasswordConfig (com.torodb.packaging.config.model.backend.BackendPasswordConfig)2 AbstractDerby (com.torodb.packaging.config.model.backend.derby.AbstractDerby)2 AbstractPostgres (com.torodb.packaging.config.model.backend.postgres.AbstractPostgres)2 AbstractReplication (com.torodb.packaging.config.model.protocol.mongo.AbstractReplication)2 MongoPasswordConfig (com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig)2