Example usage for org.apache.commons.cli CommandLine getArgList

List of usage examples for org.apache.commons.cli CommandLine getArgList

Introduction

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

Prototype

public List getArgList() 

Source Link

Document

Retrieve any left-over non-recognized options and arguments

Usage

From source file:org.ff4j.cli.FF4jCliProcessor.java

private void processCommandUpdateProperty(String commandLine) {
    try {/*w  ww  .  ja  v a 2 s .co  m*/
        CommandLine cmd = CMD_PARSER.parse(propertyOptions(), commandLine.split(" "));
        if (cmd.getArgList().size() != 1 || !cmd.hasOption("p") || !cmd.hasOption("v")) {
            logError("Invalid command, expecting update -p <property> -v <value>");
        } else {
            String property = cmd.getOptionValue('p');
            String value = cmd.getOptionValue('v');
            if (!currentFF4J.getPropertiesStore().existProperty(property)) {
                logWarn("Property " + property + " does not exist, nothing to update");
            } else {
                currentFF4J.getPropertiesStore().updateProperty(property, value);
                logInfo("Property " + property + " has been updated with " + value);
            }
        }
    } catch (ParseException e) {
        error(e, "parsing error during update property command");
    } catch (Exception e) {
        error(e, "Cannot update property");
    }
}

From source file:org.ff4j.cli.FF4jCliProcessor.java

private void processCommandAddGroup(String commandLine) {
    try {/*from   www .ja v  a  2  s . co m*/
        CommandLine cmd = CMD_PARSER.parse(addGroupOptions(), commandLine.split(" "));
        if (cmd.getArgList().size() != 1 || !cmd.hasOption("f") || !cmd.hasOption("g")) {
            logError("Invalid command, expecting addToGroup[removeFromGroup] -f <featureName> -g <grouName>");
        } else {
            String feature = cmd.getOptionValue('f');
            String group = cmd.getOptionValue('g');
            if (!currentFF4J.getFeatureStore().exist(feature)) {
                logWarn("Feature does not exist, nothing updated");
            } else {
                if (cmd.getArgList().get(0).equals("addToGroup")) {
                    currentFF4J.getFeatureStore().addToGroup(feature, group);
                    logInfo(FEATURE + feature + " has been added to group " + group);
                } else if (cmd.getArgList().get(0).equals("removeFromGroup")) {
                    String currentGroup = currentFF4J.getFeatureStore().read(feature).getGroup();
                    if (group.equals(currentGroup)) {
                        currentFF4J.getFeatureStore().removeFromGroup(feature, group);
                        logInfo(FEATURE + feature + " has been removed from group: " + group);
                    } else if (currentGroup == null || currentGroup.isEmpty()) {
                        logWarn("The groupName is invalid expected:" + currentGroup + " but was [" + group
                                + "]");
                    } else {
                        logWarn("Cannot remove group: there are no group on this feature");
                    }
                }
            }
        }
    } catch (ParseException e) {
        error(e, "Error during addToGroup/removeFromGroup command");
    }
}

From source file:org.ff4j.cli.FF4jCliProcessor.java

private void processCommandGrant(String commandLine) {
    try {//  w ww  .  ja v a2 s  . c  o  m
        CommandLine cmd = CMD_PARSER.parse(grantOptions(), commandLine.split(" "));
        if (cmd.getArgList().size() != 1 || !cmd.hasOption("f") || !cmd.hasOption("r")) {
            logError("Invalid command, expecting grant[revoke] -r <role> -f <featureName>");
        } else {
            String feature = cmd.getOptionValue('f');
            String role = cmd.getOptionValue('r');
            if (!currentFF4J.getFeatureStore().exist(feature)) {
                logWarn("Feature does not exist, nothing updated");
            } else {
                if (cmd.getArgList().get(0).equals("grant")) {
                    currentFF4J.getFeatureStore().grantRoleOnFeature(feature, role);
                    logInfo("Role " + role + " has been added to feature " + feature);
                } else if (cmd.getArgList().get(0).equals("revoke")) {
                    Set<String> permissions = currentFF4J.getFeatureStore().read(feature).getPermissions();
                    if (permissions == null) {
                        logWarn("The role is invalidn there is no role on the feature " + feature);
                    } else if (permissions.contains(role)) {
                        currentFF4J.getFeatureStore().removeRoleFromFeature(feature, role);
                        logInfo(FEATURE + feature + " has not more role " + role);
                    } else {
                        logWarn("The role is invalid expected one of " + permissions.toString());
                    }
                }
            }
        }
    } catch (ParseException e) {
        error(e, "Error during addToGroup/removeFromGroup command");
    }
}

From source file:org.ff4j.cli.FF4jCliProcessor.java

/**
 * Command to connect.//from w  ww . j a  va 2  s .c o m
 *
 * @param commandLine
 *          execute command line
 */
private void processCommandConnect(String commandLine) {
    try {
        CommandLine cmd = CMD_PARSER.parse(connectOptions(), commandLine.split(" "));
        if (cmd.getArgList().size() != 2) {
            logError("Invalid command, expecting connect <envName> [-u user] [-p password]");
        } else if (users.isEmpty()) {
            connectEnv(cmd.getArgList().get(1));
        } else if (!cmd.hasOption("u") || !cmd.hasOption("p")) {
            logWarn("Connection is not setup as opened, expecting credentials");
            logError("Invalid syntax expected connect <envName> -u <user> -p <password>");
        } else {
            String user = cmd.getOptionValue('u');
            String password = cmd.getOptionValue('p');
            if (!users.containsKey(user) || !users.get(user).equals(password)) {
                logError("Invalid credentials, check users");
            } else {
                connectEnv(cmd.getArgList().get(1));
            }
        }
    } catch (ParseException e) {
        error(e, ERROR_DURING_CONNECT_COMMAND);
    }
}

From source file:org.ff4j.cli.FF4jCliProcessor.java

/**
 * Process commandline when an environment is already selected.
 *
 * @param commandLine/*from  w ww  . j a  v  a 2s.  c o  m*/
 *       current command line
 * @param enable
 *       flag to disable or enable features
 */
private void processCommandEnable(String commandLine, boolean enable) {
    try {
        CommandLine cmd = CMD_PARSER.parse(enableFeatureOptions(), commandLine.split(" "));
        if (cmd.getArgList().size() != 1 || !cmd.hasOption("f")) {
            logWarn("Invalid command, expecting enable/disable -f <featureName>");
        } else {
            String featureName = cmd.getOptionValue('f');
            if (!currentFF4J.getFeatureStore().exist(featureName)) {
                logWarn("Feature [" + featureName + "] not found");

            } else if (enable) {
                currentFF4J.getFeatureStore().enable(featureName);
                logInfo(FEATURE + featureName + " is now enabled");

            } else {
                currentFF4J.getFeatureStore().disable(featureName);
                logInfo(FEATURE + featureName + " is now disabled");
            }
        }
    } catch (ParseException moe) {
        error(moe, ERROR_DURING_CONNECT_COMMAND);
    }
}

From source file:org.ff4j.cli.FF4jCliProcessor.java

private void processCommandEnableGroup(String commandLine, boolean enable) {
    try {/*from   w  w  w . j a  v  a  2  s. c om*/
        CommandLine cmd = CMD_PARSER.parse(enableGroupOptions(), commandLine.split(" "));
        if (cmd.getArgList().size() != 1 || !cmd.hasOption("g")) {
            logWarn("Invalid command, expecting enableGroup/disableGroup -f <groupName>");
        } else {
            String groupName = cmd.getOptionValue('g');
            if (!currentFF4J.getFeatureStore().existGroup(groupName)) {
                logWarn("Group [" + groupName + "] not found");
            } else if (enable) {
                currentFF4J.getFeatureStore().enableGroup(groupName);
                logInfo("Group " + groupName + " is now enabled");
            } else {
                currentFF4J.getFeatureStore().disableGroup(groupName);
                logInfo("Group " + groupName + " is now disabled");
            }
        }
    } catch (ParseException e) {
        error(e, ERROR_DURING_CONNECT_COMMAND);
    }
}

From source file:org.ff4j.cli.ParserTest.java

@Test
public void testParse() throws ParseException {

    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp("connect <envName> \nUse to connect to env\n", FF4jCliOptions.connectOptions());
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(FF4jCliOptions.connectOptions(),
            new String[] { "connect", "dev", "-u", "admin", "-p", "password" });

    if (cmd.hasOption("u")) {
        System.out.println(cmd.getOptionValue("u"));
    }/*w ww .java  2 s .co m*/
    if (cmd.hasOption("p")) {
        System.out.println(cmd.getOptionValue("u"));
    }
    System.out.println(cmd.getArgList());
}

From source file:org.genivi.commonapi.core.cli.CommandLineHandler.java

@Override
public int excute(CommandLine parsedArguments) {

    @SuppressWarnings("unchecked")
    List<String> files = parsedArguments.getArgList();

    // Disable log outputs if "quiet" as the very first action.
    // -ll --loglevel quiet or verbose
    if (parsedArguments.hasOption("ll")) {
        cliTool.setLogLevel(parsedArguments.getOptionValue("ll"));
    }/*from  w  w w  . jav a 2 s . c om*/
    ConsoleLogger.printLog("Executing CommonAPI Core Code Generation...\n");

    if (parsedArguments.hasOption("sk")) {
        // Switch on generation of skeletons (if this option has a parameter,
        // the parameter is the postfix for the file names
        cliTool.setCreateSkeletonCode();
        String skArgument = parsedArguments.getOptionValue("sk");
        if (skArgument == null) {
            // no -sk argument was given, use "Default"
            skArgument = "Default";
        }
        // The fidl/fdepl file will be interpreted as sk argument, 
        // if "-sk" is placed just before the fidl/fdepl file in the command line !
        if (skArgument.endsWith(FILE_EXTENSION_FDEPL) || skArgument.endsWith(FILE_EXTENSION_FIDL)) {
            // it is not an -sk argument but it is the file to generate code from !
            files.add(skArgument);
        } else {
            cliTool.setSkeletonPostfix(skArgument);
        }
    }
    // a search path may be specified, collect all fidl/fdepl files
    if (parsedArguments.hasOption("sp")) {
        String searchPath = parsedArguments.getOptionValue("sp");
        files.addAll(cliTool.searchFidlandFdeplFiles(searchPath));
    }

    // we expect at least the fidel/fdepl file as command line argument
    if (files.size() > 0 && files.get(0) != null) {
        String file = files.get(0);
        if (file.endsWith(FILE_EXTENSION_FDEPL) || file.endsWith(FILE_EXTENSION_FIDL)) {
            // handle command line options

            // Switch off generation of common code
            // -nc --no-common do not generate common code
            if (parsedArguments.hasOption("nc")) {
                cliTool.setNoCommonCode();
            }

            // Switch off generation of proxy code
            // -np --no-proxy do not generate proxy code
            if (parsedArguments.hasOption("np")) {
                cliTool.setNoProxyCode();
            }

            // Switch off generation of stub code
            // -ns --no-stub do not generate stub code            
            if (parsedArguments.hasOption("ns")) {
                cliTool.setNoStubCode();
            }

            // destination: -d --dest overwrite default directory
            if (parsedArguments.hasOption("d")) {
                cliTool.setDefaultDirectory(parsedArguments.getOptionValue("d"));
            }

            // destination: -dsub --dest-subdirs use destination/<filename>/ subdirs
            // as dest directory
            if (parsedArguments.hasOption("dsub")) {
                cliTool.setDestinationSubdirs();
            }
            // destination: -dc --dest-common overwrite target directory for common part
            if (parsedArguments.hasOption("dc")) {
                cliTool.setCommonDirectory(parsedArguments.getOptionValue("dc"));
            }

            // destination: -dp --dest-proxy overwrite target directory for proxy code
            if (parsedArguments.hasOption("dp")) {
                cliTool.setProxyDirectory(parsedArguments.getOptionValue("dp"));
            }

            // destination: -ds --dest-stub overwrite target directory for stub code
            if (parsedArguments.hasOption("ds")) {
                cliTool.setStubDirectory(parsedArguments.getOptionValue("ds"));
            }

            // destination: -dsk --dest-skel overwrite target directory for skeleton code
            if (parsedArguments.hasOption("dsk")) {
                cliTool.setSkeletonDirectory(parsedArguments.getOptionValue("dsk"));
            }

            // A file path, that points to a file, that contains the license text.
            // -l --license license text in generated files
            if (parsedArguments.hasOption("l")) {
                cliTool.setLicenseText(parsedArguments.getOptionValue("l"));
            }

            // Add prefixes to enumeration literals
            // -pre --prefix-enum-literal additional prefix to all generated enumeration literals
            if (parsedArguments.hasOption("pre")) {
                cliTool.setEnumPrefix(parsedArguments.getOptionValue("pre"));
            }
            // Switch off validation
            if (parsedArguments.hasOption("nv")) {
                cliTool.disableValidation();
            }
            // Switch off code generation
            if (parsedArguments.hasOption("ng")) {
                cliTool.disableCodeGeneration();
            }
            // Don't generate code for included types and interfaces
            if (parsedArguments.hasOption("wod")) {
                cliTool.noCodeforDependencies();
            }
            // Don't generate synchronous calls
            if (parsedArguments.hasOption("nsc")) {
                cliTool.disableSyncCalls();
            }
            // print out generated files
            if (parsedArguments.hasOption("pf")) {
                cliTool.listGeneratedFiles();
            }
            // finally invoke the generator.
            // the remaining arguments are assumed to be files !
            cliTool.generateCore(files);
        } else {
            System.out.println(
                    "The file extension should be ." + FILE_EXTENSION_FIDL + " or ." + FILE_EXTENSION_FDEPL);
        }
    } else {
        System.out.println("A *.fidl or *.fdepl file was not specified !");
    }
    return 0;
}

From source file:org.genivi.commonapi.dbus.cli.CommandLineHandlerDBus.java

@Override
public int excute(CommandLine parsedArguments) {
    @SuppressWarnings("unchecked")
    List<String> files = parsedArguments.getArgList();
    // a search path may be specified, collect all fidl/fdepl files
    if (parsedArguments.hasOption("sp")) {
        files.addAll(cliTool.searchFidlandFdeplFiles(parsedArguments.getOptionValue("sp")));
    }/*w w  w .  j  a  va2 s .c  o m*/
    // We expect at least one fidl/fdepl file as command line argument
    if (files.size() > 0 && files.get(0) != null) {
        String file = files.get(0);
        // handle command line options

        // -ll --loglevel quiet or verbose
        if (parsedArguments.hasOption("ll")) {
            cliTool.setLogLevel(parsedArguments.getOptionValue("ll"));
        }
        ConsoleLogger.printLog("Executing CommonAPI DBus Code Generation...\n");

        // Switch off generation of common code
        // -nc --no-common do not generate proxy code
        if (parsedArguments.hasOption("nc")) {
            cliTool.setNoCommonCode();
        }

        // Switch off generation of proxy code
        // -np --no-proxy do not generate proxy code
        if (parsedArguments.hasOption("np")) {
            cliTool.setNoProxyCode();
        }

        // Switch off generation of stub code
        // -ns --no-stub do not generate stub code
        if (parsedArguments.hasOption("ns")) {
            cliTool.setNoStubCode();
        }

        // destination: -d --dest overwrite default directory
        if (parsedArguments.hasOption("d")) {
            cliTool.setDefaultDirectory(parsedArguments.getOptionValue("d"));
        }

        // destination: -dsub --dest-subdirs use destination/<filename>/ subdirs
        // as dest directory
        if (parsedArguments.hasOption("dsub")) {
            cliTool.setDestinationSubdirs();
        }

        // destination: -dc --dest-common overwrite target directory for
        // common part
        if (parsedArguments.hasOption("dc")) {
            cliTool.setCommonDirectory(parsedArguments.getOptionValue("dc"));
        }

        // destination: -dp --dest-proxy overwrite target directory for
        // proxy code
        if (parsedArguments.hasOption("dp")) {
            cliTool.setProxyDirectory(parsedArguments.getOptionValue("dp"));
        }

        // destination: -ds --dest-stub overwrite target directory for stub
        // code
        if (parsedArguments.hasOption("ds")) {
            cliTool.setStubDirectory(parsedArguments.getOptionValue("ds"));
        }

        // A file path, that points to a file, that contains the license
        // text.
        // -l --license license text in generated files
        if (parsedArguments.hasOption("l")) {
            cliTool.setLicenseText(parsedArguments.getOptionValue("l"));
        }

        // Switch off validation
        if (parsedArguments.hasOption("nv")) {
            cliTool.disableValidation();
        }
        // Switch off code generation at all
        if (parsedArguments.hasOption("ng")) {
            cliTool.disableCodeGeneration();
        }
        // Don't generate code for included types and interfaces
        if (parsedArguments.hasOption("wod")) {
            cliTool.noCodeforDependencies();
        }
        // Don't generate synchronous calls
        if (parsedArguments.hasOption("nsc")) {
            cliTool.disableSyncCalls();
        }
        // print out generated files
        if (parsedArguments.hasOption("pf")) {
            cliTool.listGeneratedFiles();
        }
        // finally invoke the generator.
        cliTool.generateDBus(files);
    } else {
        System.out.println("A *.fidl or *.fdepl file was not specified !");
    }
    return 0;
}

From source file:org.genivi.commonapi.someip.cli.CommandLineHandlerSomeIp.java

@Override
public int excute(CommandLine parsedArguments) {
    @SuppressWarnings("unchecked")
    List<String> files = parsedArguments.getArgList();

    // a search path may be specified, collect all fdepl files
    if (parsedArguments.hasOption("sp")) {
        files.addAll(cliTool.searchFdeplFiles(parsedArguments.getOptionValue("sp")));
    }//w w  w .  j  a va  2 s  .com
    // We expect at least one fdepl file
    if (files.size() > 0 && files.get(0) != null) {
        String file = files.get(0);
        if (file.endsWith(FILE_EXTENSION_FDEPL)) {
            // handle command line options

            // -ll --loglevel quiet or verbose
            if (parsedArguments.hasOption("ll")) {
                cliTool.setLogLevel(parsedArguments.getOptionValue("ll"));
            }
            ConsoleLogger.printLog("Executing CommonAPI SomeIP Code Generation...\n");

            // Switch off generation of proxy code
            // -np --no-proxy do not generate proxy code
            if (parsedArguments.hasOption("np")) {
                cliTool.setNoProxyCode();
            }

            // Switch off generation of stub code
            // -ns --no-stub do not generate stub code            
            if (parsedArguments.hasOption("ns")) {
                cliTool.setNoStubCode();
            }

            // destination: -d --dest overwrite default directory
            if (parsedArguments.hasOption("d")) {
                cliTool.setDefaultDirectory(parsedArguments.getOptionValue("d"));
            }

            // destination: -dsub --dest-subdirs use destination/<filename>/ subdirs
            // as dest directory
            if (parsedArguments.hasOption("dsub")) {
                cliTool.setDestinationSubdirs();
            }

            // destination: -dc --dest-common overwrite target directory for common part
            if (parsedArguments.hasOption("dc")) {
                cliTool.setCommonDirectory(parsedArguments.getOptionValue("dc"));
            }

            // destination: -dp --dest-proxy overwrite target directory for proxy code
            if (parsedArguments.hasOption("dp")) {
                cliTool.setProxyDirectory(parsedArguments.getOptionValue("dp"));
            }

            // destination: -ds --dest-stub overwrite target directory for stub code
            if (parsedArguments.hasOption("ds")) {
                cliTool.setStubDirectory(parsedArguments.getOptionValue("ds"));
            }

            // A file path, that points to a file, that contains the license text.
            // -l --license license text in generated files
            if (parsedArguments.hasOption("l")) {
                cliTool.setLicenseText(parsedArguments.getOptionValue("l"));
            }
            // print out generated files
            if (parsedArguments.hasOption("pf")) {
                cliTool.listGeneratedFiles();
            }
            // Switch off validation
            if (parsedArguments.hasOption("nv")) {
                cliTool.disableValidation();
            }
            // Don't generate code for included types and interfaces
            if (parsedArguments.hasOption("wod")) {
                cliTool.noCodeforDependencies();
            }
            // Don't generate synchronous calls
            if (parsedArguments.hasOption("nsc")) {
                cliTool.disableSyncCalls();
            }
            // Switch off code generation at all 
            if (parsedArguments.hasOption("ng")) {
                cliTool.disableCodeGeneration();
            }

            // finally invoke the generator.
            cliTool.generateSomeIp(files);
        } else {
            System.out.println("The file extension should be ." + FILE_EXTENSION_FDEPL);
        }
    } else {
        System.out.println("A *.fdepl file was not specified !");
    }
    return 0;
}