Example usage for org.apache.commons.cli BasicParser BasicParser

List of usage examples for org.apache.commons.cli BasicParser BasicParser

Introduction

In this page you can find the example usage for org.apache.commons.cli BasicParser BasicParser.

Prototype

BasicParser

Source Link

Usage

From source file:org.apache.atlas.hive.bridge.HiveMetaStoreBridge.java

public static void main(String[] args) throws AtlasHookException {
    try {//w  w w.j a va 2  s  .  co m
        Configuration atlasConf = ApplicationProperties.get();
        String[] atlasEndpoint = atlasConf.getStringArray(ATLAS_ENDPOINT);
        if (atlasEndpoint == null || atlasEndpoint.length == 0) {
            atlasEndpoint = new String[] { DEFAULT_DGI_URL };
        }
        AtlasClient atlasClient;

        if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) {
            String[] basicAuthUsernamePassword = AuthenticationUtil.getBasicAuthenticationInput();
            atlasClient = new AtlasClient(atlasEndpoint, basicAuthUsernamePassword);
        } else {
            UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
            atlasClient = new AtlasClient(ugi, ugi.getShortUserName(), atlasEndpoint);
        }

        Options options = new Options();
        CommandLineParser parser = new BasicParser();
        CommandLine cmd = parser.parse(options, args);

        boolean failOnError = false;
        if (cmd.hasOption("failOnError")) {
            failOnError = true;
        }

        HiveMetaStoreBridge hiveMetaStoreBridge = new HiveMetaStoreBridge(atlasConf, new HiveConf(),
                atlasClient);
        hiveMetaStoreBridge.importHiveMetadata(failOnError);
    } catch (Exception e) {
        throw new AtlasHookException("HiveMetaStoreBridge.main() failed.", e);
    }
}

From source file:org.apache.avalon.merlin.cli.Main.java

/**
 * Main command line enty point.//from w ww .j  a  v  a2 s .co m
 * @param args the command line arguments
 */
public static void main(String[] args) {
    boolean debug = false;
    try {
        //
        // parse the commandline
        //

        CommandLineParser parser = new BasicParser();
        CommandLine line = parser.parse(CL_OPTIONS, args);

        File dir = getWorkingDirectory(line);
        File cache = getMerlinSystemRepository(line);
        Artifact artifact = getDefaultImplementation(dir, line);

        debug = line.hasOption("debug");

        if (line.hasOption("version")) {
            Main.printVersionInfo(cache, artifact);
            return;
        } else if (line.hasOption("help")) {
            if (line.hasOption("lang")) {
                ResourceManager.clearResourceCache();
                String language = line.getOptionValue("lang");
                Locale locale = new Locale(language, "");
                Locale.setDefault(locale);
                REZ = ResourceManager.getPackageResources(Main.class);
            }

            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("merlin [block]", " ", buildCommandLineOptions(), "", true);
            return;
        } else {
            //
            // setup the initial context
            //

            InitialContextFactory factory = new DefaultInitialContextFactory("merlin", dir);
            factory.setCacheDirectory(cache);
            factory.setOnlineMode(!line.hasOption("offline"));
            InitialContext context = factory.createInitialContext();

            //
            // process the commandline and do the real work
            //

            MAIN = new Main(context, artifact, line);
        }
    } catch (Exception exception) {
        String msg = ExceptionHelper.packException(exception, debug);
        System.err.println(msg);
        System.exit(-1);
    } catch (Throwable throwable) {
        String msg = ExceptionHelper.packException(throwable, true);
        System.err.println(msg);
        System.exit(-1);
    }
}

From source file:org.apache.avalon.repository.cli.Main.java

/**
 * Main command line enty point.//from  w  w  w.j a  va2 s  . c  om
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
        //
        // parse the commandline
        //

        CommandLineParser parser = new BasicParser();
        CommandLine line = parser.parse(CL_OPTIONS, args);

        File dir = getWorkingDirectory(line);
        File cache = getCacheDirectory(line);
        Artifact artifact = getDefaultImplementation(dir, line);

        if (line.hasOption("version")) {
            Main.printVersionInfo(cache, artifact);
            return;
        } else if (line.hasOption("help")) {
            doHelp(line);
            return;
        } else {
            //
            // setup the initial context
            //

            ClassLoader parent = Main.class.getClassLoader();
            String[] hosts = getHostsPath(line);

            DefaultInitialContextFactory factory = new DefaultInitialContextFactory("avalon", dir);
            factory.setCacheDirectory(cache);
            factory.setHosts(hosts);

            InitialContext context = factory.createInitialContext();

            //
            // process the commandline and do the real work
            //

            MAIN = new Main(context, line);
        }
    } catch (Throwable e) {
        String msg = ExceptionHelper.packException(e, true);
        System.err.println(msg);
        System.exit(-1);
    }
}

From source file:org.apache.bookkeeper.bookie.BookieShellTest.java

private static CommandLine parseCommandLine(MyCommand cmd, String... args) throws ParseException {
    BasicParser parser = new BasicParser();
    return parser.parse(cmd.getOptions(), args);
}

From source file:org.apache.bookkeeper.bookie.FileSystemUpgrade.java

public static void main(String[] args) throws Exception {
    org.apache.log4j.Logger root = org.apache.log4j.Logger.getRootLogger();
    root.addAppender(//from  w ww.  j  a  va  2  s.  c  om
            new org.apache.log4j.ConsoleAppender(new org.apache.log4j.PatternLayout("%-5p [%t]: %m%n")));
    root.setLevel(org.apache.log4j.Level.ERROR);
    org.apache.log4j.Logger.getLogger(FileSystemUpgrade.class).setLevel(org.apache.log4j.Level.INFO);

    final Options opts = new Options();
    opts.addOption("c", "conf", true, "Configuration for Bookie");
    opts.addOption("u", "upgrade", false, "Upgrade bookie directories");
    opts.addOption("f", "finalize", false, "Finalize upgrade");
    opts.addOption("r", "rollback", false, "Rollback upgrade");
    opts.addOption("h", "help", false, "Print help message");

    BasicParser parser = new BasicParser();
    CommandLine cmdLine = parser.parse(opts, args);
    if (cmdLine.hasOption("h")) {
        printHelp(opts);
        return;
    }

    if (!cmdLine.hasOption("c")) {
        String err = "Cannot upgrade without configuration";
        LOG.error(err);
        printHelp(opts);
        throw new IllegalArgumentException(err);
    }

    String confFile = cmdLine.getOptionValue("c");
    ServerConfiguration conf = new ServerConfiguration();
    try {
        conf.loadConf(new File(confFile).toURI().toURL());
    } catch (MalformedURLException mue) {
        LOG.error("Could not open configuration file " + confFile, mue);
        throw new IllegalArgumentException();
    } catch (ConfigurationException ce) {
        LOG.error("Invalid configuration file " + confFile, ce);
        throw new IllegalArgumentException();
    }

    if (cmdLine.hasOption("u")) {
        upgrade(conf);
    } else if (cmdLine.hasOption("r")) {
        rollback(conf);
    } else if (cmdLine.hasOption("f")) {
        finalizeUpgrade(conf);
    } else {
        String err = "Must specify -upgrade, -finalize or -rollback";
        LOG.error(err);
        printHelp(opts);
        throw new IllegalArgumentException(err);
    }
}

From source file:org.apache.bookkeeper.proto.BookieServer.java

private static ServerConfiguration parseArgs(String[] args) throws IllegalArgumentException {
    try {//from  w  w w.  j a v a2s. co  m
        BasicParser parser = new BasicParser();
        CommandLine cmdLine = parser.parse(bkOpts, args);

        if (cmdLine.hasOption('h')) {
            throw new IllegalArgumentException();
        }

        ServerConfiguration conf = new ServerConfiguration();

        if (cmdLine.hasOption('c')) {
            String confFile = cmdLine.getOptionValue("c");
            loadConfFile(conf, confFile);
        }

        if (cmdLine.hasOption("withAutoRecovery")) {
            conf.setAutoRecoveryDaemonEnabled(true);
        }

        if (cmdLine.hasOption("readOnly")) {
            conf.setForceReadOnlyBookie(true);
        }

        // command line arguments overwrite settings in configuration file
        if (cmdLine.hasOption('z')) {
            String sZK = cmdLine.getOptionValue('z');
            LOG.info("Get cmdline zookeeper instance: " + sZK);
            conf.setZkServers(sZK);
        }

        if (cmdLine.hasOption('m')) {
            String sZkLedgersRootPath = cmdLine.getOptionValue('m');
            LOG.info("Get cmdline zookeeper ledger path: " + sZkLedgersRootPath);
            conf.setZkLedgersRootPath(sZkLedgersRootPath);
        }

        if (cmdLine.hasOption('p')) {
            String sPort = cmdLine.getOptionValue('p');
            LOG.info("Get cmdline bookie port: " + sPort);
            Integer iPort = Integer.parseInt(sPort);
            conf.setBookiePort(iPort.intValue());
        }

        if (cmdLine.hasOption('j')) {
            String sJournalDir = cmdLine.getOptionValue('j');
            LOG.info("Get cmdline journal dir: " + sJournalDir);
            conf.setJournalDirName(sJournalDir);
        }

        if (cmdLine.hasOption('i')) {
            String[] sIndexDirs = cmdLine.getOptionValues('i');
            LOG.info("Get cmdline index dirs: ");
            for (String index : sIndexDirs) {
                LOG.info("indexDir : " + index);
            }
            conf.setIndexDirName(sIndexDirs);
        }

        if (cmdLine.hasOption('l')) {
            String[] sLedgerDirs = cmdLine.getOptionValues('l');
            LOG.info("Get cmdline ledger dirs: ");
            for (String ledger : sLedgerDirs) {
                LOG.info("ledgerdir : " + ledger);
            }
            conf.setLedgerDirNames(sLedgerDirs);
        }

        return conf;
    } catch (ParseException e) {
        LOG.error("Error parsing command line arguments : ", e);
        throw new IllegalArgumentException(e);
    }
}

From source file:org.apache.bookkeeper.replication.AutoRecoveryMain.java

private static ServerConfiguration parseArgs(String[] args) throws IllegalArgumentException {
    try {// www  . j a v a2  s.  c o  m
        BasicParser parser = new BasicParser();
        CommandLine cmdLine = parser.parse(opts, args);

        if (cmdLine.hasOption('h')) {
            throw new IllegalArgumentException();
        }

        ServerConfiguration conf = new ServerConfiguration();
        String[] leftArgs = cmdLine.getArgs();

        if (cmdLine.hasOption('c')) {
            if (null != leftArgs && leftArgs.length > 0) {
                throw new IllegalArgumentException();
            }
            String confFile = cmdLine.getOptionValue("c");
            loadConfFile(conf, confFile);
        }

        if (null != leftArgs && leftArgs.length > 0) {
            throw new IllegalArgumentException();
        }
        return conf;
    } catch (ParseException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:org.apache.bookkeeper.server.Main.java

@SuppressWarnings("deprecation")
private static ServerConfiguration parseArgs(String[] args) throws IllegalArgumentException {
    try {/*from   w  ww .j  a  v  a 2  s.c  o  m*/
        BasicParser parser = new BasicParser();
        CommandLine cmdLine = parser.parse(BK_OPTS, args);

        if (cmdLine.hasOption('h')) {
            throw new IllegalArgumentException();
        }

        ServerConfiguration conf = new ServerConfiguration();

        if (cmdLine.hasOption('c')) {
            String confFile = cmdLine.getOptionValue("c");
            loadConfFile(conf, confFile);
        }

        if (cmdLine.hasOption("withAutoRecovery")) {
            conf.setAutoRecoveryDaemonEnabled(true);
        }

        if (cmdLine.hasOption("r")) {
            conf.setForceReadOnlyBookie(true);
        }

        boolean overwriteMetadataServiceUri = false;
        String sZkLedgersRootPath = "/ledgers";
        if (cmdLine.hasOption('m')) {
            sZkLedgersRootPath = cmdLine.getOptionValue('m');
            log.info("Get cmdline zookeeper ledger path: {}", sZkLedgersRootPath);
            overwriteMetadataServiceUri = true;
        }

        String sZK = conf.getZkServers();
        if (cmdLine.hasOption('z')) {
            sZK = cmdLine.getOptionValue('z');
            log.info("Get cmdline zookeeper instance: {}", sZK);
            overwriteMetadataServiceUri = true;
        }

        // command line arguments overwrite settings in configuration file
        if (overwriteMetadataServiceUri) {
            String metadataServiceUri = "zk://" + sZK + sZkLedgersRootPath;
            conf.setMetadataServiceUri(metadataServiceUri);
            log.info("Overwritten service uri to {}", metadataServiceUri);
        }

        if (cmdLine.hasOption('p')) {
            String sPort = cmdLine.getOptionValue('p');
            log.info("Get cmdline bookie port: {}", sPort);
            Integer iPort = Integer.parseInt(sPort);
            conf.setBookiePort(iPort.intValue());
        }

        if (cmdLine.hasOption('j')) {
            String sJournalDir = cmdLine.getOptionValue('j');
            log.info("Get cmdline journal dir: {}", sJournalDir);
            conf.setJournalDirName(sJournalDir);
        }

        if (cmdLine.hasOption('i')) {
            String[] sIndexDirs = cmdLine.getOptionValues('i');
            log.info("Get cmdline index dirs: ");
            for (String index : sIndexDirs) {
                log.info("indexDir : {}", index);
            }
            conf.setIndexDirName(sIndexDirs);
        }

        if (cmdLine.hasOption('l')) {
            String[] sLedgerDirs = cmdLine.getOptionValues('l');
            log.info("Get cmdline ledger dirs: ");
            for (String ledger : sLedgerDirs) {
                log.info("ledgerdir : {}", ledger);
            }
            conf.setLedgerDirNames(sLedgerDirs);
        }

        return conf;
    } catch (ParseException e) {
        log.error("Error parsing command line arguments : ", e);
        throw new IllegalArgumentException(e);
    }
}

From source file:org.apache.cxf.example.wstransferexample.server.resourcefactory.Server.java

public static void main(String[] args) {
    Options options = new Options();
    options.addOption("resourceFactoryUrl", true, "Url where the ResourceFactory will listen.");
    options.addOption("resourceUrl", true, "Url where the Resource will listen.");
    options.addOption("resourceTeachersUrl", true, "Url of the ResourceFactory for teachers.");
    options.addOption("help", false, "Print this message.");
    CommandLineParser parser = new BasicParser();
    try {//from  www.j  a v a  2  s. c om
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption("help")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java -jar client.jar", options);
            System.exit(0);
        }
        Config.getInstance().setResourceFactoryUrl(cmd.getOptionValue("resourceFactoryUrl"));
        Config.getInstance().setResourceUrl(cmd.getOptionValue("resourceUrl"));
        Config.getInstance().setResourceTeachersUrl(cmd.getOptionValue("resourceTeachersUrl"));
    } catch (ParseException ex) {
        System.err.println("Error: " + ex.getLocalizedMessage());
        System.exit(1);
    }

    ResourceManager resourceManager = new MemoryResourceManager();
    createResource(resourceManager);
    createResourceFactory(resourceManager);
}

From source file:org.apache.cxf.example.wstransfexample.server.resource.Server.java

public static void main(String[] args) {
    Options options = new Options();
    options.addOption("serverUrl", true, "Url where server will listen.");
    options.addOption("help", false, "Print this message.");
    CommandLineParser parser = new BasicParser();
    try {/* w  w  w  .j  a v  a  2s .  c  o m*/
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption("help")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java -jar client.jar", options);
            System.exit(0);
        }
        Config.getInstance().setServerUrl(cmd.getOptionValue("serverUrl"));
    } catch (ParseException ex) {
        System.err.println("Error: " + ex.getLocalizedMessage());
        System.exit(1);
    }

    ResourceManager resourceManager = new MemoryResourceManager();
    ResourceRemote resourceRemote = new ResourceRemote();
    resourceRemote.setManager(resourceManager);
    resourceRemote.getResourceTypeIdentifiers().add(new XSDResourceTypeIdentifier(
            new StreamSource(Server.class.getResourceAsStream("/xml/schema/teacher.xsd")),
            new XSLTResourceTransformer(
                    new StreamSource(Server.class.getResourceAsStream("/xml/xslt/teacherDefaultValues.xsl")),
                    new TeacherResourceValidator())));

    createResourceFactoryEndpoint(resourceRemote);
    createResourceEndpoint(resourceRemote);
}