All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.picocli.runtime.DefaultPicocliCommandLineFactory Maven / Gradle / Ivy

package io.quarkus.picocli.runtime;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.literal.NamedLiteral;

import io.quarkus.picocli.runtime.annotations.TopCommand;
import picocli.CommandLine;

@ApplicationScoped
public class DefaultPicocliCommandLineFactory implements PicocliCommandLineFactory {

    private final Instance topCommand;
    private final PicocliConfiguration picocliConfiguration;
    private final CommandLine.IFactory picocliFactory;

    public DefaultPicocliCommandLineFactory(@TopCommand Instance topCommand,
            PicocliConfiguration picocliConfiguration,
            CommandLine.IFactory picocliFactory) {
        this.topCommand = topCommand;
        this.picocliConfiguration = picocliConfiguration;
        this.picocliFactory = picocliFactory;
    }

    private Class classForName(String name) {
        try {
            return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
        } catch (ClassNotFoundException ex) {
            throw new IllegalArgumentException(ex);
        }
    }

    @Override
    public CommandLine create() {
        String topCommandName = picocliConfiguration.topCommand.orElse(null);
        if (topCommandName != null) {
            Instance namedTopCommand = topCommand.select(NamedLiteral.of(topCommandName));
            if (namedTopCommand.isResolvable()) {
                return new CommandLine(namedTopCommand.get(), picocliFactory);
            }
            return new CommandLine(classForName(topCommandName), picocliFactory);
        }
        return new CommandLine(topCommand.get(), picocliFactory);
    }
}