Search in sources :

Example 91 with CommandLine

use of org.apache.commons.cli.CommandLine in project phoenix by apache.

the class BulkLoadToolTest method testParseOptions.

@Test
public void testParseOptions() {
    CommandLine cmdLine = bulkLoadTool.parseOptions(new String[] { "--input", "/input", "--table", "mytable" });
    assertEquals("mytable", cmdLine.getOptionValue(CsvBulkLoadTool.TABLE_NAME_OPT.getOpt()));
    assertEquals("/input", cmdLine.getOptionValue(CsvBulkLoadTool.INPUT_PATH_OPT.getOpt()));
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) Test(org.junit.Test)

Example 92 with CommandLine

use of org.apache.commons.cli.CommandLine in project asterixdb by apache.

the class AsterixApplicationMaster method setArgs.

public CommandLine setArgs(String[] args) throws ParseException {
    Options opts = new Options();
    opts.addOption("app_attempt_id", true, "App Attempt ID. Not to be used unless for testing purposes");
    opts.addOption("priority", true, "Application Priority. Default 0");
    opts.addOption("debug", false, "Dump out debug information");
    opts.addOption("help", false, "Print usage");
    opts.addOption("initial", false, "Initialize existing Asterix instance.");
    opts.addOption("obliterate", false, "Delete asterix instance completely.");
    opts.addOption("backup", false, "Back up AsterixDB instance");
    opts.addOption("restore", true, "Restore an AsterixDB instance");
    CommandLine cliParser = new GnuParser().parse(opts, args);
    if (cliParser.hasOption("help")) {
        printUsage(opts);
    }
    if (cliParser.hasOption("debug")) {
        dumpOutDebugInfo();
    }
    if (cliParser.hasOption("obliterate")) {
        obliterate = true;
    }
    if (cliParser.hasOption("initial")) {
        initial = true;
    }
    if (cliParser.hasOption("backup")) {
        backup = true;
        backupTimestamp = System.currentTimeMillis();
    }
    if (cliParser.hasOption("restore")) {
        restore = true;
        snapName = cliParser.getOptionValue("restore");
        LOG.info(snapName);
    }
    return cliParser;
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser)

Example 93 with CommandLine

use of org.apache.commons.cli.CommandLine in project solo by b3log.

the class Starter method main.

/**
     * Main.
     *
     * @param args the specified arguments
     * @throws java.lang.Exception if start failed
     */
public static void main(final String[] args) throws Exception {
    final Logger logger = Logger.getLogger(Starter.class);
    final Options options = new Options();
    final Option listenPortOpt = Option.builder("lp").longOpt("listen_port").argName("LISTEN_PORT").hasArg().desc("listen port, default is 8080").build();
    options.addOption(listenPortOpt);
    final Option serverSchemeOpt = Option.builder("ss").longOpt("server_scheme").argName("SERVER_SCHEME").hasArg().desc("browser visit protocol, default is http").build();
    options.addOption(serverSchemeOpt);
    final Option serverHostOpt = Option.builder("sh").longOpt("server_host").argName("SERVER_HOST").hasArg().desc("browser visit domain name, default is localhost").build();
    options.addOption(serverHostOpt);
    final Option serverPortOpt = Option.builder("sp").longOpt("server_port").argName("SERVER_PORT").hasArg().desc("browser visit port, default is 8080").build();
    options.addOption(serverPortOpt);
    final Option staticServerSchemeOpt = Option.builder("sss").longOpt("static_server_scheme").argName("STATIC_SERVER_SCHEME").hasArg().desc("browser visit static resource protocol, default is http").build();
    options.addOption(staticServerSchemeOpt);
    final Option staticServerHostOpt = Option.builder("ssh").longOpt("static_server_host").argName("STATIC_SERVER_HOST").hasArg().desc("browser visit static resource domain name, default is localhost").build();
    options.addOption(staticServerHostOpt);
    final Option staticServerPortOpt = Option.builder("ssp").longOpt("static_server_port").argName("STATIC_SERVER_PORT").hasArg().desc("browser visit static resource port, default is 8080").build();
    options.addOption(staticServerPortOpt);
    final Option runtimeModeOpt = Option.builder("rm").longOpt("runtime_mode").argName("RUNTIME_MODE").hasArg().desc("runtime mode (DEVELOPMENT/PRODUCTION), default is DEVELOPMENT").build();
    options.addOption(runtimeModeOpt);
    options.addOption("h", "help", false, "print help for the command");
    final HelpFormatter helpFormatter = new HelpFormatter();
    final CommandLineParser commandLineParser = new DefaultParser();
    CommandLine commandLine;
    final boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
    final String cmdSyntax = isWindows ? "java -cp WEB-INF/lib/*;WEB-INF/classes org.b3log.solo.Starter" : "java -cp WEB-INF/lib/*:WEB-INF/classes org.b3log.solo.Starter";
    final String header = "\nSolo is a blogging system written in Java, feel free to create your or your team own blog.\nSolo 是一个用 Java 实现的博客系统,为你或你的团队创建个博客吧。\n\n";
    final String footer = "\nReport bugs or request features please visit our project website: https://github.com/b3log/solo\n\n";
    try {
        commandLine = commandLineParser.parse(options, args);
    } catch (final ParseException e) {
        helpFormatter.printHelp(cmdSyntax, header, options, footer, true);
        return;
    }
    if (commandLine.hasOption("h")) {
        helpFormatter.printHelp(cmdSyntax, header, options, footer, true);
        return;
    }
    String portArg = commandLine.getOptionValue("listen_port");
    if (!Strings.isNumeric(portArg)) {
        portArg = "8080";
    }
    String serverScheme = commandLine.getOptionValue("server_scheme");
    Latkes.setServerScheme(serverScheme);
    String serverHost = commandLine.getOptionValue("server_host");
    Latkes.setServerHost(serverHost);
    String serverPort = commandLine.getOptionValue("server_port");
    Latkes.setServerPort(serverPort);
    String staticServerScheme = commandLine.getOptionValue("static_server_scheme");
    Latkes.setStaticServerScheme(staticServerScheme);
    String staticServerHost = commandLine.getOptionValue("static_server_host");
    Latkes.setStaticServerHost(staticServerHost);
    String staticServerPort = commandLine.getOptionValue("static_server_port");
    Latkes.setStaticServerPort(staticServerPort);
    String runtimeMode = commandLine.getOptionValue("runtime_mode");
    if (null != runtimeMode) {
        Latkes.setRuntimeMode(RuntimeMode.valueOf(runtimeMode));
    }
    // For Latke IoC 
    Latkes.setScanPath("org.b3log.solo");
    logger.info("Standalone mode, see [https://github.com/b3log/solo/wiki/standalone_mode] for more details.");
    Latkes.initRuntimeEnv();
    // POM structure in dev env
    String webappDirLocation = "src/main/webapp/";
    final File file = new File(webappDirLocation);
    if (!file.exists()) {
        // production environment
        webappDirLocation = ".";
    }
    final int port = Integer.valueOf(portArg);
    final Server server = new Server(port);
    final WebAppContext root = new WebAppContext();
    // Use parent class loader
    root.setParentLoaderPriority(true);
    root.setContextPath("/");
    root.setDescriptor(webappDirLocation + "/WEB-INF/web.xml");
    root.setResourceBase(webappDirLocation);
    server.setHandler(root);
    try {
        server.start();
    } catch (final Exception e) {
        logger.log(Level.ERROR, "Server start failed", e);
        System.exit(-1);
    }
    serverScheme = Latkes.getServerScheme();
    serverHost = Latkes.getServerHost();
    serverPort = Latkes.getServerPort();
    final String contextPath = Latkes.getContextPath();
    try {
        Desktop.getDesktop().browse(new URI(serverScheme + "://" + serverHost + ":" + serverPort + contextPath));
    } catch (final Throwable e) {
    // Ignored
    }
    server.join();
}
Also used : Options(org.apache.commons.cli.Options) Server(org.eclipse.jetty.server.Server) Logger(org.b3log.latke.logging.Logger) URI(java.net.URI) ParseException(org.apache.commons.cli.ParseException) HelpFormatter(org.apache.commons.cli.HelpFormatter) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 94 with CommandLine

use of org.apache.commons.cli.CommandLine in project core by s4.

the class MainApp method main.

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("corehome").hasArg().withDescription("core home").create("c"));
    options.addOption(OptionBuilder.withArgName("appshome").hasArg().withDescription("applications home").create("a"));
    options.addOption(OptionBuilder.withArgName("s4clock").hasArg().withDescription("s4 clock").create("d"));
    options.addOption(OptionBuilder.withArgName("seedtime").hasArg().withDescription("event clock initialization time").create("s"));
    options.addOption(OptionBuilder.withArgName("extshome").hasArg().withDescription("extensions home").create("e"));
    options.addOption(OptionBuilder.withArgName("instanceid").hasArg().withDescription("instance id").create("i"));
    options.addOption(OptionBuilder.withArgName("configtype").hasArg().withDescription("configuration type").create("t"));
    CommandLineParser parser = new GnuParser();
    CommandLine commandLine = null;
    String clockType = "wall";
    try {
        commandLine = parser.parse(options, args);
    } catch (ParseException pe) {
        System.err.println(pe.getLocalizedMessage());
        System.exit(1);
    }
    int instanceId = -1;
    if (commandLine.hasOption("i")) {
        String instanceIdStr = commandLine.getOptionValue("i");
        try {
            instanceId = Integer.parseInt(instanceIdStr);
        } catch (NumberFormatException nfe) {
            System.err.println("Bad instance id: %s" + instanceIdStr);
            System.exit(1);
        }
    }
    if (commandLine.hasOption("c")) {
        coreHome = commandLine.getOptionValue("c");
    }
    if (commandLine.hasOption("a")) {
        appsHome = commandLine.getOptionValue("a");
    }
    if (commandLine.hasOption("d")) {
        clockType = commandLine.getOptionValue("d");
    }
    if (commandLine.hasOption("e")) {
        extsHome = commandLine.getOptionValue("e");
    }
    String configType = "typical";
    if (commandLine.hasOption("t")) {
        configType = commandLine.getOptionValue("t");
    }
    long seedTime = 0;
    if (commandLine.hasOption("s")) {
        seedTime = Long.parseLong(commandLine.getOptionValue("s"));
    }
    File coreHomeFile = new File(coreHome);
    if (!coreHomeFile.isDirectory()) {
        System.err.println("Bad core home: " + coreHome);
        System.exit(1);
    }
    File appsHomeFile = new File(appsHome);
    if (!appsHomeFile.isDirectory()) {
        System.err.println("Bad applications home: " + appsHome);
        System.exit(1);
    }
    if (instanceId > -1) {
        System.setProperty("instanceId", "" + instanceId);
    } else {
        System.setProperty("instanceId", "" + S4Util.getPID());
    }
    List loArgs = commandLine.getArgList();
    if (loArgs.size() < 1) {
    // System.err.println("No bean configuration file specified");
    // System.exit(1);
    }
    // String s4ConfigXml = (String) loArgs.get(0);
    // System.out.println("s4ConfigXml is " + s4ConfigXml);
    ClassPathResource propResource = new ClassPathResource("s4_core.properties");
    Properties prop = new Properties();
    if (propResource.exists()) {
        prop.load(propResource.getInputStream());
    } else {
        System.err.println("Unable to find s4_core.properties. It must be available in classpath");
        System.exit(1);
    }
    ApplicationContext coreContext = null;
    String configBase = coreHome + File.separatorChar + "conf" + File.separatorChar + configType;
    String configPath = "";
    List<String> coreConfigUrls = new ArrayList<String>();
    File configFile = null;
    // load clock configuration
    configPath = configBase + File.separatorChar + clockType + "_clock.xml";
    coreConfigUrls.add(configPath);
    // load core config xml
    configPath = configBase + File.separatorChar + "s4_core_conf.xml";
    configFile = new File(configPath);
    if (!configFile.exists()) {
        System.err.printf("S4 core config file %s does not exist\n", configPath);
        System.exit(1);
    }
    coreConfigUrls.add(configPath);
    String[] coreConfigFiles = new String[coreConfigUrls.size()];
    coreConfigUrls.toArray(coreConfigFiles);
    String[] coreConfigFileUrls = new String[coreConfigFiles.length];
    for (int i = 0; i < coreConfigFiles.length; i++) {
        coreConfigFileUrls[i] = "file:" + coreConfigFiles[i];
    }
    coreContext = new FileSystemXmlApplicationContext(coreConfigFileUrls, coreContext);
    ApplicationContext context = coreContext;
    Clock s4Clock = (Clock) context.getBean("clock");
    if (s4Clock instanceof EventClock && seedTime > 0) {
        EventClock s4EventClock = (EventClock) s4Clock;
        s4EventClock.updateTime(seedTime);
        System.out.println("Intializing event clock time with seed time " + s4EventClock.getCurrentTime());
    }
    PEContainer peContainer = (PEContainer) context.getBean("peContainer");
    Watcher w = (Watcher) context.getBean("watcher");
    w.setConfigFilename(configPath);
    // load extension modules
    String[] configFileNames = getModuleConfigFiles(extsHome, prop);
    if (configFileNames.length > 0) {
        String[] configFileUrls = new String[configFileNames.length];
        for (int i = 0; i < configFileNames.length; i++) {
            configFileUrls[i] = "file:" + configFileNames[i];
        }
        context = new FileSystemXmlApplicationContext(configFileUrls, context);
    }
    // load application modules
    configFileNames = getModuleConfigFiles(appsHome, prop);
    if (configFileNames.length > 0) {
        String[] configFileUrls = new String[configFileNames.length];
        for (int i = 0; i < configFileNames.length; i++) {
            configFileUrls[i] = "file:" + configFileNames[i];
        }
        context = new FileSystemXmlApplicationContext(configFileUrls, context);
        // attach any beans that implement ProcessingElement to the PE
        // Container
        String[] processingElementBeanNames = context.getBeanNamesForType(ProcessingElement.class);
        for (String processingElementBeanName : processingElementBeanNames) {
            Object bean = context.getBean(processingElementBeanName);
            try {
                Method getS4ClockMethod = bean.getClass().getMethod("getS4Clock");
                if (getS4ClockMethod.getReturnType().equals(Clock.class)) {
                    if (getS4ClockMethod.invoke(bean) == null) {
                        Method setS4ClockMethod = bean.getClass().getMethod("setS4Clock", Clock.class);
                        setS4ClockMethod.invoke(bean, coreContext.getBean("clock"));
                    }
                }
            } catch (NoSuchMethodException mnfe) {
            // acceptable
            }
            System.out.println("Adding processing element with bean name " + processingElementBeanName + ", id " + ((ProcessingElement) bean).getId());
            peContainer.addProcessor((ProcessingElement) bean);
        }
    }
}
Also used : Options(org.apache.commons.cli.Options) GnuParser(org.apache.commons.cli.GnuParser) ArrayList(java.util.ArrayList) Watcher(io.s4.util.Watcher) Properties(java.util.Properties) EventClock(io.s4.util.clock.EventClock) Clock(io.s4.util.clock.Clock) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ArrayList(java.util.ArrayList) List(java.util.List) CommandLineParser(org.apache.commons.cli.CommandLineParser) EventClock(io.s4.util.clock.EventClock) ProcessingElement(io.s4.processor.ProcessingElement) Method(java.lang.reflect.Method) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) CommandLine(org.apache.commons.cli.CommandLine) PEContainer(io.s4.processor.PEContainer) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 95 with CommandLine

use of org.apache.commons.cli.CommandLine in project core by s4.

the class Adapter method main.

public static void main(String[] args) {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("corehome").hasArg().withDescription("core home").create("c"));
    options.addOption(OptionBuilder.withArgName("instanceid").hasArg().withDescription("instance id").create("i"));
    options.addOption(OptionBuilder.withArgName("configtype").hasArg().withDescription("configuration type").create("t"));
    options.addOption(OptionBuilder.withArgName("userconfig").hasArg().withDescription("user-defined legacy data adapter configuration file").create("d"));
    CommandLineParser parser = new GnuParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (ParseException pe) {
        System.err.println(pe.getLocalizedMessage());
        System.exit(1);
    }
    int instanceId = -1;
    if (commandLine.hasOption("i")) {
        String instanceIdStr = commandLine.getOptionValue("i");
        try {
            instanceId = Integer.parseInt(instanceIdStr);
        } catch (NumberFormatException nfe) {
            System.err.println("Bad instance id: %s" + instanceIdStr);
            System.exit(1);
        }
    }
    if (commandLine.hasOption("c")) {
        coreHome = commandLine.getOptionValue("c");
    }
    String configType = "typical";
    if (commandLine.hasOption("t")) {
        configType = commandLine.getOptionValue("t");
    }
    String userConfigFilename = null;
    if (commandLine.hasOption("d")) {
        userConfigFilename = commandLine.getOptionValue("d");
    }
    File userConfigFile = new File(userConfigFilename);
    if (!userConfigFile.isFile()) {
        System.err.println("Bad user configuration file: " + userConfigFilename);
        System.exit(1);
    }
    File coreHomeFile = new File(coreHome);
    if (!coreHomeFile.isDirectory()) {
        System.err.println("Bad core home: " + coreHome);
        System.exit(1);
    }
    if (instanceId > -1) {
        System.setProperty("instanceId", "" + instanceId);
    } else {
        System.setProperty("instanceId", "" + S4Util.getPID());
    }
    String configBase = coreHome + File.separatorChar + "conf" + File.separatorChar + configType;
    String configPath = configBase + File.separatorChar + "adapter_conf.xml";
    File configFile = new File(configPath);
    if (!configFile.exists()) {
        System.err.printf("adapter config file %s does not exist\n", configPath);
        System.exit(1);
    }
    // load adapter config xml
    ApplicationContext coreContext;
    coreContext = new FileSystemXmlApplicationContext("file:" + configPath);
    ApplicationContext context = coreContext;
    Adapter adapter = (Adapter) context.getBean("adapter");
    ApplicationContext appContext = new FileSystemXmlApplicationContext(new String[] { "file:" + userConfigFilename }, context);
    Map listenerBeanMap = appContext.getBeansOfType(EventProducer.class);
    if (listenerBeanMap.size() == 0) {
        System.err.println("No user-defined listener beans");
        System.exit(1);
    }
    EventProducer[] eventListeners = new EventProducer[listenerBeanMap.size()];
    int index = 0;
    for (Iterator it = listenerBeanMap.keySet().iterator(); it.hasNext(); index++) {
        String beanName = (String) it.next();
        System.out.println("Adding producer " + beanName);
        eventListeners[index] = (EventProducer) listenerBeanMap.get(beanName);
    }
    adapter.setEventListeners(eventListeners);
}
Also used : Options(org.apache.commons.cli.Options) GnuParser(org.apache.commons.cli.GnuParser) EventProducer(io.s4.listener.EventProducer) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) CommandLine(org.apache.commons.cli.CommandLine) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) Iterator(java.util.Iterator) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File) Map(java.util.Map)

Aggregations

CommandLine (org.apache.commons.cli.CommandLine)968 Options (org.apache.commons.cli.Options)560 ParseException (org.apache.commons.cli.ParseException)464 CommandLineParser (org.apache.commons.cli.CommandLineParser)459 HelpFormatter (org.apache.commons.cli.HelpFormatter)259 GnuParser (org.apache.commons.cli.GnuParser)230 DefaultParser (org.apache.commons.cli.DefaultParser)213 PosixParser (org.apache.commons.cli.PosixParser)177 File (java.io.File)165 IOException (java.io.IOException)162 Test (org.junit.Test)144 Option (org.apache.commons.cli.Option)131 ArrayList (java.util.ArrayList)63 Path (org.apache.hadoop.fs.Path)60 BasicParser (org.apache.commons.cli.BasicParser)50 Configuration (org.apache.hadoop.conf.Configuration)41 Configuration (org.apache.flink.configuration.Configuration)37 HashMap (java.util.HashMap)34 List (java.util.List)34 Properties (java.util.Properties)33