Search in sources :

Example 6 with ArgumentsParser

use of org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser in project ovirt-engine by oVirt.

the class LoggerServiceImpl method parseArguments.

@Override
public void parseArguments(List<String> args) throws Exception {
    args.remove(0);
    Properties props = new Properties();
    try (InputStream in = LoggerServiceImpl.class.getResourceAsStream("arguments.properties");
        Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
        props.load(reader);
    }
    Map<String, String> substitutions = context.get(ContextKeys.CLI_PARSER_SUBSTITUTIONS);
    ArgumentsParser parser = new ArgumentsParser(props, "module");
    parser.getSubstitutions().putAll(substitutions);
    parser.parse(args);
    argModuleMap = parser.getParsedArgs();
    if ((Boolean) argModuleMap.get("help")) {
        System.out.format("Usage: %s", parser.getUsage());
        throw new ExitException("Help", 0);
    }
    if (!parser.getErrors().isEmpty()) {
        for (Throwable t : parser.getErrors()) {
            log.error(t.getMessage());
        }
        throw new ExitException("Parsing error", 1);
    }
    if (args.size() < 1) {
        log.error("Action not provided");
        throw new ExitException("Action not provided", 1);
    }
    try {
        action = Action.valueOf(args.get(0).toUpperCase().replace("-", "_"));
    } catch (IllegalArgumentException e) {
        log.error("Invalid action '{}'", args.get(0));
        throw new ExitException("Invalid action", 1);
    }
    argMap = action.parse(substitutions, props, args);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Properties(java.util.Properties) ArgumentsParser(org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser) ExitException(org.ovirt.engine.exttool.core.ExitException)

Aggregations

InputStream (java.io.InputStream)6 ArgumentsParser (org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser)6 InputStreamReader (java.io.InputStreamReader)4 Reader (java.io.Reader)4 Properties (java.util.Properties)4 ArrayList (java.util.ArrayList)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ExitException (org.ovirt.engine.exttool.core.ExitException)2 File (java.io.File)1 Path (java.nio.file.Path)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ExtensionsManager (org.ovirt.engine.core.extensions.mgr.ExtensionsManager)1 DBUtils (org.ovirt.engine.ssoreg.db.DBUtils)1