Search in sources :

Example 1 with PluginOperationAction

use of org.syncany.operations.plugin.PluginOperationAction in project syncany by syncany.

the class PluginCommand method parseOptions.

@Override
public PluginOperationOptions parseOptions(String[] operationArgs) throws Exception {
    PluginOperationOptions operationOptions = new PluginOperationOptions();
    OptionParser parser = new OptionParser();
    OptionSpec<Void> optionLocal = parser.acceptsAll(asList("L", "local-only"));
    OptionSpec<Void> optionRemote = parser.acceptsAll(asList("R", "remote-only"));
    OptionSpec<Void> optionSnapshots = parser.acceptsAll(asList("s", "snapshot", "snapshots"));
    OptionSpec<Void> optionMinimalOutput = parser.acceptsAll(asList("m", "minimal-output"));
    OptionSpec<String> optionApiEndpoint = parser.acceptsAll(asList("a", "api-endpoint")).withRequiredArg();
    OptionSet options = parser.parse(operationArgs);
    // Files
    List<?> nonOptionArgs = options.nonOptionArguments();
    if (nonOptionArgs.size() == 0) {
        throw new Exception("Invalid syntax, please specify an action (list, install, remove, update).");
    }
    // <action>
    String actionStr = nonOptionArgs.get(0).toString();
    PluginOperationAction action = parsePluginAction(actionStr);
    operationOptions.setAction(action);
    // --minimal-output
    minimalOutput = options.has(optionMinimalOutput);
    // --snapshots
    operationOptions.setSnapshots(options.has(optionSnapshots));
    // --api-endpoint
    if (options.has(optionApiEndpoint)) {
        operationOptions.setApiEndpoint(options.valueOf(optionApiEndpoint));
    }
    // install|remove <plugin-id>
    if (action == PluginOperationAction.INSTALL || action == PluginOperationAction.REMOVE) {
        if (nonOptionArgs.size() != 2) {
            throw new Exception("Invalid syntax, please specify a plugin ID.");
        }
        // <plugin-id>
        String pluginId = nonOptionArgs.get(1).toString();
        operationOptions.setPluginId(pluginId);
    } else // --local-only, --remote-only
    if (action == PluginOperationAction.LIST) {
        if (options.has(optionLocal)) {
            operationOptions.setListMode(PluginListMode.LOCAL);
        } else if (options.has(optionRemote)) {
            operationOptions.setListMode(PluginListMode.REMOTE);
        } else {
            operationOptions.setListMode(PluginListMode.ALL);
        }
        // <plugin-id> (optional in 'list' or 'update')
        if (nonOptionArgs.size() == 2) {
            String pluginId = nonOptionArgs.get(1).toString();
            operationOptions.setPluginId(pluginId);
        }
    } else if (action == PluginOperationAction.UPDATE && nonOptionArgs.size() == 2) {
        String pluginId = nonOptionArgs.get(1).toString();
        operationOptions.setPluginId(pluginId);
    }
    return operationOptions;
}
Also used : PluginOperationAction(org.syncany.operations.plugin.PluginOperationAction) PluginOperationOptions(org.syncany.operations.plugin.PluginOperationOptions) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser)

Aggregations

OptionParser (joptsimple.OptionParser)1 OptionSet (joptsimple.OptionSet)1 PluginOperationAction (org.syncany.operations.plugin.PluginOperationAction)1 PluginOperationOptions (org.syncany.operations.plugin.PluginOperationOptions)1