Search in sources :

Example 1 with CommandLineParser

use of act.cli.util.CommandLineParser in project actframework by actframework.

the class CliContext method handle.

/**
 * Run handler and return `false` if it needs to exit the CLI or `true` otherwise
 */
void handle() throws IOException {
    if (null == handler) {
        println("Command not recognized: %s", command());
        return;
    }
    if (handler == Exit.INSTANCE) {
        handler.handle(this);
    }
    CommandLineParser parser = commandLine();
    boolean help = parser.getBoolean("-h", "--help");
    if (help) {
        Help.INSTANCE.showHelp(parser.command(), this);
    } else {
        try {
            session.handler(handler);
            handler.handle(this);
        } catch ($.Break b) {
            throw b;
        } catch (Exception e) {
            console.println("Error: " + e.getMessage());
        }
    }
}
Also used : org.osgl.$(org.osgl.$) CommandLineParser(act.cli.util.CommandLineParser) IOException(java.io.IOException)

Example 2 with CommandLineParser

use of act.cli.util.CommandLineParser in project actframework by actframework.

the class Help method handle.

@Override
public void handle(CliContext context) {
    List<String> args = context.arguments();
    String command = null;
    if (args.size() > 0) {
        command = args.get(0);
        if (showHelp(command, context)) {
            return;
        }
    }
    CommandLineParser parser = context.commandLine();
    boolean sys = parser.getBoolean("-s", "--system");
    boolean app = parser.getBoolean("-a", "--app");
    if (!sys && !app) {
        sys = true;
        app = true;
    }
    CliDispatcher dispatcher = context.app().cliDispatcher();
    List<String> sysCommands = dispatcher.commands(true, false);
    List<String> appCommands = dispatcher.commands(false, true);
    int maxLen;
    if (sys && !app) {
        maxLen = calMaxCmdLen(sysCommands);
    } else if (app && !sys) {
        maxLen = calMaxCmdLen(appCommands);
    } else {
        maxLen = calMaxCmdLen(C.list(sysCommands).append(appCommands));
    }
    String fmt = "%-" + (maxLen + 4) + "s - %s";
    if (sys) {
        list(command, "@|bold System commands|@", fmt, sysCommands, dispatcher, context);
    }
    if (app) {
        if (sys) {
            context.println("");
        }
        list(command, "@|bold Application commands|@", fmt, appCommands, dispatcher, context);
    }
}
Also used : CliDispatcher(act.cli.CliDispatcher) CommandLineParser(act.cli.util.CommandLineParser)

Example 3 with CommandLineParser

use of act.cli.util.CommandLineParser in project actframework by actframework.

the class CliContextParamLoader method preParseOptions.

public void preParseOptions(Method method, CommandMethodMetaInfo methodMetaInfo, CliContext context) {
    List<OptionLoader> optionLoaders = ensureOptionLoaders(method, methodMetaInfo);
    CommandLineParser commandLineParser = context.commandLine();
    boolean argumentAsOption = false;
    if (1 == optionLoaders.size()) {
        OptionLoader loader = optionLoaders.get(0);
        if (loader.required) {
            String theOptionVal = commandLineParser.argumentAsOption();
            if (null != theOptionVal) {
                argumentAsOption = true;
                context.parsingContext().foundRequired(loader.requiredGroup);
                context.param(loader.bindName, theOptionVal);
            }
        }
    }
    if (!argumentAsOption) {
        for (OptionLoader loader : optionLoaders) {
            String bindName = loader.bindName;
            String value = commandLineParser.getString(loader.lead1, loader.lead2);
            if (S.notBlank(value)) {
                if (loader.required) {
                    context.parsingContext().foundRequired(loader.requiredGroup);
                }
                context.param(bindName, value);
            }
        }
    }
    context.parsingContext().raiseExceptionIfThereAreMissingOptions(context);
}
Also used : CommandLineParser(act.cli.util.CommandLineParser)

Example 4 with CommandLineParser

use of act.cli.util.CommandLineParser in project actframework by actframework.

the class CliVarArgumentLoader method load.

@Override
public Object load(Object cached, ActContext<?> context, boolean noDefaultValue) {
    CliContext ctx = (CliContext) context;
    List list = new ArrayList();
    CliContext.ParsingContext parsingContext = ctx.parsingContext();
    CommandLineParser parser = ctx.commandLine();
    while (parsingContext.hasArguments(parser)) {
        int id = ctx.parsingContext().curArgId().getAndIncrement();
        list.add(resolver.resolve(parser.argument(id)));
    }
    return ArrayLoader.listToArray(list, componentType);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) CommandLineParser(act.cli.util.CommandLineParser) CliContext(act.cli.CliContext)

Aggregations

CommandLineParser (act.cli.util.CommandLineParser)4 CliContext (act.cli.CliContext)1 CliDispatcher (act.cli.CliDispatcher)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 org.osgl.$ (org.osgl.$)1