use of org.fagu.fmv.cli.exception.LineParseException in project fmv by f-agu.
the class Help method run.
/**
* @see org.fagu.fmv.cli.Command#run(java.lang.String[])
*/
@Override
public void run(String[] args) {
if (args.length == 0) {
listAllCommands();
return;
}
for (String arg : args) {
CommandFactory commandFactory = commandBuilder.getCommandFactory(arg);
if (commandFactory == null) {
println("Command not found: " + arg);
continue;
}
org.fagu.fmv.cli.Command command = null;
try {
command = commandFactory.create(consoleReader, commandBuilder, project, arg, args);
} catch (LineParseException e) {
continue;
}
command.help();
}
}
use of org.fagu.fmv.cli.exception.LineParseException in project fmv by f-agu.
the class DefaultCommandBuilder method add.
/**
* @see org.fagu.fmv.cli.CommandBuilder#add(java.lang.Class)
*/
@Override
public void add(final Class<? extends Command> cmdClass) {
final org.fagu.fmv.cli.annotation.Command cmd = cmdClass.getAnnotation(org.fagu.fmv.cli.annotation.Command.class);
if (cmd == null) {
throw new RuntimeException("Annotation @Command not found: " + cmdClass.getName());
}
add(new CommandFactory() {
/**
* @see org.fagu.fmv.cli.CommandFactory#getCommandName()
*/
@Override
public String getCommandName() {
return cmd.value();
}
/**
* @see org.fagu.fmv.cli.CommandFactory#create(jline.console.ConsoleReader, org.fagu.fmv.cli.CommandBuilder,
* org.fagu.fmv.core.project.Project, java.lang.String, String[])
*/
@Override
public Command create(ConsoleReader consoleReader, CommandBuilder commandBuilder, Project project, String executable, String[] args) throws LineParseException {
return build(cmdClass);
}
});
// alias
List<Alias> aliases = new ArrayList<>();
Aliases aliasesAnno = cmdClass.getAnnotation(Aliases.class);
if (aliasesAnno != null) {
aliases.addAll(Arrays.asList(aliasesAnno.value()));
}
Alias aliasAnno = cmdClass.getAnnotation(Alias.class);
if (aliasAnno != null) {
aliases.add(aliasAnno);
}
if (!aliases.isEmpty()) {
for (Alias alias : aliases) {
String aliasCommand = alias.command();
if (StringUtils.isBlank(aliasCommand)) {
aliasCommand = cmd.value();
}
for (String aliasName : alias.value()) {
addAlias(aliasName, aliasCommand);
}
}
}
// completion
Completion completion = cmdClass.getAnnotation(Completion.class);
if (completion != null) {
try {
@SuppressWarnings("unchecked") Class<CompleterFactory> completerFactoryClass = (Class<CompleterFactory>) Class.forName(completion.value());
CompleterFactory completerFactory = completerFactoryClass.newInstance();
Completer completer = completerFactory.create(cmd.value());
completerMap.add(cmd.value(), completer);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
use of org.fagu.fmv.cli.exception.LineParseException in project fmv by f-agu.
the class Help method listAllCommands.
// *****************************************
/**
*/
protected void listAllCommands() {
TreeMap<String, HelpTable> cmdFctMap = new TreeMap<>();
for (CommandFactory commandFactory : commandBuilder.getCommandFactories()) {
String commandName = commandFactory.getCommandName();
if (org.fagu.fmv.cli.command.Alias.isAlias(commandFactory)) {
String mainCmd = org.fagu.fmv.cli.command.Alias.getAliasCommand(commandFactory);
cmdFctMap.computeIfAbsent(mainCmd, HelpTable::new).addAlias(commandName);
continue;
} else {
HelpTable helpTable = cmdFctMap.computeIfAbsent(commandName, HelpTable::new);
org.fagu.fmv.cli.Command cmd;
try {
cmd = commandFactory.create(consoleReader, commandBuilder, project, "", null);
} catch (LineParseException e) {
throw new RuntimeException(e);
}
helpTable.setDescription(cmd.getShortDescription());
}
}
int maxLen = cmdFctMap.values().stream().mapToInt(ht -> ht.getNames().length()).max().orElse(0);
for (HelpTable helpTable : cmdFctMap.values()) {
StringBuilder buf = new StringBuilder(40);
buf.append(" ").append(StringUtils.rightPad(helpTable.getNames(), maxLen)).append(" ").append(helpTable.description);
println(buf.toString());
}
}
use of org.fagu.fmv.cli.exception.LineParseException in project fmv by f-agu.
the class DefaultCommandBuilder method createAndExec.
/**
* @see org.fagu.fmv.cli.CommandBuilder#createAndExec(java.lang.String)
*/
@Override
public Command createAndExec(String line) throws LineParseException {
CommandLine parse = new CommandLine("ignore");
parse.addArguments(line, false);
String[] arguments = parse.getArguments();
String executable = arguments[0];
CommandFactory commandFactory = cmdBuilderMap.get(executable.toLowerCase());
if (commandFactory == null) {
// command not found
throw new LineParseException(line);
}
String[] args = new String[arguments.length - 1];
System.arraycopy(arguments, 1, args, 0, arguments.length - 1);
Command command = commandFactory.create(consoleReader, this, project, commandFactory.getCommandName(), args);
Options options = new Options();
options.addOption("h", "help", false, "");
CommandLineParser parser = new GnuParser();
try {
org.apache.commons.cli.CommandLine commandLine = parser.parse(options, args);
if (commandLine.hasOption('h')) {
command.help();
return command;
}
} catch (ParseException e) {
// ignore
}
command.run(args);
return command;
}
Aggregations