Search in sources :

Example 1 with Builder

use of org.apache.commons.cli.Option.Builder in project BiglyBT by BiglySoftware.

the class Main method main.

public static void main(String[] args) {
    if (DEBUG_STARTUPTIME) {
        lastDebugTime = System.currentTimeMillis();
    }
    if (Launcher.checkAndLaunch(Main.class, args))
        return;
    // This *has* to be done first as it sets system properties that are read and cached by Java
    COConfigurationManager.preInitialise();
    if (DEBUG_STARTUPTIME) {
        logTime("args: " + Arrays.toString(args));
    }
    Thread.currentThread().setName(Constants.APP_NAME);
    String mi_str = System.getProperty("MULTI_INSTANCE");
    boolean mi = mi_str != null && mi_str.equalsIgnoreCase("true");
    if (DEBUG_STARTUPTIME) {
        logTime("preInit");
    }
    try {
        // Build a list of UIS
        Options uiOptions = new Options();
        Builder builder = Option.builder("u").longOpt("ui").argName("uis").hasArg();
        uiOptions.addOption(builder.build());
        if (Constants.isWindows) {
            builder = Option.builder("console");
            uiOptions.addOption(builder.build());
        }
        try {
            CommandLine commandLine = new DefaultParser().parse(uiOptions, args, true);
            buildUIList(commandLine);
        } catch (ParseException e) {
        }
        // Add UIS Command Line Options
        Options options = UIConst.buildOptions();
        commands = UIConst.buildCommandLine(options, args);
        if (commands == null) {
            System.exit(0);
        }
        if (DEBUG_STARTUPTIME) {
            logTime("buildCommandLine");
        }
        if (!mi) {
            startServer = new StartServer();
            if (startServer.getServerState() == StartServer.STATE_FAULTY) {
                System.setProperty("transitory.startup", "1");
                // looks like there's already a process listening on 127.0.0.1:<port>
                // attempt to pass args to existing instance
                // First, do some OSX magic because parameters are passed via OpenDocument API and other callbacks
                args = CocoaMagic(args);
                if (!new CoreSingleInstanceClient().sendArgs(args, 5000)) {
                    // arg passing attempt failed, so start core anyway
                    String msg = "There appears to be another process already listening on socket [127.0.0.1:" + Constants.INSTANCE_PORT + "].\n\nLocate and terminate the other program or change the control port - <a href=\"" + Constants.URL_WIKI + "w/Commandline_options#Changing_the_Control_Port\">see the wiki for details</a>.";
                    System.err.println(msg);
                    return;
                } else {
                    // we sent params to other core, don't init the core
                    return;
                }
            }
            if (commands.hasOption("closedown") || commands.hasOption("shutdown") || commands.hasOption("restart")) {
                return;
            }
            if (DEBUG_STARTUPTIME) {
                logTime("StartServer");
            }
        } else {
            System.out.println("MULTI_INSTANCE enabled");
        }
        // Special Exit if user ask for help
        if (commands != null && commands.hasOption('h')) {
            HelpFormatter hf = new HelpFormatter();
            hf.setOptionComparator(null);
            hf.printHelp("[options] [torrent [torrent ...]]", options);
            if (startServer != null) {
                startServer.stopIt();
            }
            System.exit(0);
        }
        boolean isFirst = true;
        for (IUserInterface ui : UIConst.UIS.values()) {
            ui.init(isFirst, (UIConst.UIS.size() > 1));
            isFirst = false;
        }
        neverStarted = true;
        core = CoreFactory.create();
        if (DEBUG_STARTUPTIME) {
            logTime("Core Create");
        }
        for (IUserInterface ui : UIConst.UIS.values()) {
            ui.coreCreated(core);
        }
        if (DEBUG_STARTUPTIME) {
            logTime("UIConst.set" + Constants.AZUREUS_NAME + "Core");
        }
        UIConst.processArgs(commands, options, args);
        if (DEBUG_STARTUPTIME) {
            logTime("UIConst.processArgs");
        }
        if (startServer != null) {
            startServer.setDaemon(true);
            startServer.start();
        }
        neverStarted = !core.isStarted();
        core.addLifecycleListener(new CoreLifecycleAdapter() {

            @Override
            public void started(Core core) {
                Main.neverStarted = false;
            }

            @Override
            public void stopping(Core core) {
                Main.stopping = true;
            }

            @Override
            public void stopped(Core core) {
                if (startServer != null) {
                    startServer.stopIt();
                }
                Main.stopped = true;
            }
        });
        for (IUserInterface ui : UIConst.UIS.values()) {
            ui.takeMainThread();
            if (stopping) {
                break;
            }
        }
        if (neverStarted) {
            if (DEBUG_STARTUPTIME) {
                logTime("takeMainThread");
            }
            core.start();
            if (DEBUG_STARTUPTIME) {
                logTime("coreStart");
            }
        }
        if (!stopping) {
            // no one took the main thread!
            while (!stopped) {
                try {
                    Thread.sleep(200);
                    // (not case "-u console,Swt")
                    if (newUI != null) {
                        IUserInterface threadTaker = newUI;
                        newUI = null;
                        threadTaker.takeMainThread();
                    }
                } catch (InterruptedException e) {
                }
            }
        }
    } catch (CoreException e) {
        System.out.println("Start fails:");
        e.printStackTrace();
    }
    if (DEBUG_STARTUPTIME) {
        logTime("DONE");
    }
}
Also used : CoreSingleInstanceClient(com.biglybt.core.impl.CoreSingleInstanceClient) CoreLifecycleAdapter(com.biglybt.core.CoreLifecycleAdapter) IUserInterface(com.biglybt.ui.common.IUserInterface) Builder(org.apache.commons.cli.Option.Builder) CoreException(com.biglybt.core.CoreException) StartServer(com.biglybt.ui.common.StartServer) Core(com.biglybt.core.Core)

Example 2 with Builder

use of org.apache.commons.cli.Option.Builder in project BiglyBT by BiglySoftware.

the class UIConst method getDefaultOptions.

private static Options getDefaultOptions() {
    Options options = new Options();
    Builder builder;
    options.addOption("h", "help", false, "Show this help.");
    builder = Option.builder("u").longOpt("ui").argName("uis").hasArg().desc("Run <uis>. ',' separated list of user interfaces to run (swt, console, telnet). The first one given will respond to requests without determinable source UI (e.g. further torrents added via command line).");
    options.addOption(builder.build());
    builder = Option.builder().longOpt("closedown").desc("shutdown an existing instance of BiglyBT");
    options.addOption(builder.build());
    builder = Option.builder().longOpt("shutdown").desc("shutdown an existing instance of BiglyBT");
    options.addOption(builder.build());
    builder = Option.builder().longOpt("open").desc("show the BiglyBT interface");
    options.addOption(builder.build());
    builder = Option.builder().longOpt("share").desc("share a resource");
    options.addOption(builder.build());
    if (Constants.isWindows) {
        builder = Option.builder("console").desc("(Windows) keeps a console window open while " + Constants.APP_NAME + " is running");
        options.addOption(builder.build());
    }
    return options;
}
Also used : Builder(org.apache.commons.cli.Option.Builder)

Example 3 with Builder

use of org.apache.commons.cli.Option.Builder in project commons by terran4j.

the class CommandLineService method execute.

private void execute(String groupName, String commandName, CommandDefine command, String[] args, PrintStream out) throws BusinessException {
    Options options = new Options();
    List<CommandOptionDefine> optionConfigs = command.getOptions();
    if (optionConfigs != null && optionConfigs.size() > 0) {
        for (CommandOptionDefine optionConfig : optionConfigs) {
            String key = optionConfig.getKey();
            String name = optionConfig.getName();
            String desc = optionConfig.getDesc();
            OptionType type = optionConfig.getType();
            if (type == OptionType.BOOLEAN) {
                options.addOption(key, name, false, desc);
            } else if (type == OptionType.PROPERTIES) {
                Option option = // 
                Option.builder(key).argName("property=value").numberOfArgs(5).valueSeparator('=').desc(// 
                desc).build();
                options.addOption(option);
            } else {
                Builder builder = Option.builder(key).hasArg();
                if (!StringUtils.isEmpty(name)) {
                    builder = builder.longOpt(name);
                }
                Option option = builder.desc(desc).build();
                options.addOption(option);
            }
        }
    }
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (UnrecognizedOptionException e) {
        String optionKey = e.getOption();
        out.println("无效的命令行选项: " + optionKey);
        out.println(getHelp(groupName, commandName));
        return;
    } catch (ParseException e) {
        out.println("解析命令出错:" + e.getMessage());
        out.println(getHelp(groupName, commandName));
        return;
    }
    CommandExecutor executor = command.getExecutor();
    CommandInterpreter ci = new CommandInterpreterImpl(out, commandLine, command);
    if (optionConfigs != null && optionConfigs.size() > 0) {
        for (CommandOptionDefine optionConfig : optionConfigs) {
            if (optionConfig.isRequired() && optionConfig.getType() != OptionType.BOOLEAN) {
                String key = optionConfig.getKey();
                String name = optionConfig.getName();
                String value = ci.getOption(key);
                if (value == null) {
                    throw // 
                    new CommandException(CommandErrorCode.OPTION_KEY_EMPTY.getName()).put("group", // 
                    groupName).put("commandName", // 
                    commandName).put("optionKey", // 
                    key).put("optionName", // 
                    name).setMessage("命令 [${group} ${commandName}] 需要 ${optionName} 选项," + "请在命令后面附上 -${optionKey} [${optionName}]");
                }
            }
        }
    }
    executor.execute(ci);
}
Also used : Options(org.apache.commons.cli.Options) Builder(org.apache.commons.cli.Option.Builder) CommandExecutor(com.terran4j.commons.jfinger.CommandExecutor) CommandException(com.terran4j.commons.jfinger.CommandException) CommandOptionDefine(com.terran4j.commons.jfinger.CommandOptionDefine) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) ParseException(org.apache.commons.cli.ParseException) OptionType(com.terran4j.commons.jfinger.OptionType) CommandInterpreter(com.terran4j.commons.jfinger.CommandInterpreter)

Aggregations

Builder (org.apache.commons.cli.Option.Builder)3 Core (com.biglybt.core.Core)1 CoreException (com.biglybt.core.CoreException)1 CoreLifecycleAdapter (com.biglybt.core.CoreLifecycleAdapter)1 CoreSingleInstanceClient (com.biglybt.core.impl.CoreSingleInstanceClient)1 IUserInterface (com.biglybt.ui.common.IUserInterface)1 StartServer (com.biglybt.ui.common.StartServer)1 CommandException (com.terran4j.commons.jfinger.CommandException)1 CommandExecutor (com.terran4j.commons.jfinger.CommandExecutor)1 CommandInterpreter (com.terran4j.commons.jfinger.CommandInterpreter)1 CommandOptionDefine (com.terran4j.commons.jfinger.CommandOptionDefine)1 OptionType (com.terran4j.commons.jfinger.OptionType)1 CommandLine (org.apache.commons.cli.CommandLine)1 Option (org.apache.commons.cli.Option)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 UnrecognizedOptionException (org.apache.commons.cli.UnrecognizedOptionException)1