Move buildArgumentParser to Cli class

This commit is contained in:
AsamK 2021-01-16 17:45:45 +01:00
parent fe25ae275b
commit 14c8f07b8d
2 changed files with 68 additions and 89 deletions

View file

@ -1,6 +1,12 @@
package org.asamk.signal;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import net.sourceforge.argparse4j.inf.Subparsers;
import org.asamk.Signal;
import org.asamk.signal.commands.Command;
@ -27,6 +33,7 @@ import org.whispersystems.signalservice.internal.configuration.SignalServiceConf
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@ -36,12 +43,49 @@ public class Cli {
private final Namespace ns;
static ArgumentParser buildArgumentParser() {
ArgumentParser parser = ArgumentParsers.newFor("signal-cli")
.build()
.defaultHelp(true)
.description("Commandline interface for Signal.")
.version(BaseConfig.PROJECT_NAME + " " + BaseConfig.PROJECT_VERSION);
parser.addArgument("-v", "--version").help("Show package version.").action(Arguments.version());
parser.addArgument("--verbose")
.help("Raise log level and include lib signal logs.")
.action(Arguments.storeTrue());
parser.addArgument("--config")
.help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
parser.addArgument("-u", "--username").help("Specify your phone number, that will be used for verification.");
MutuallyExclusiveGroup mut = parser.addMutuallyExclusiveGroup();
mut.addArgument("--dbus").help("Make request via user dbus.").action(Arguments.storeTrue());
mut.addArgument("--dbus-system").help("Make request via system dbus.").action(Arguments.storeTrue());
parser.addArgument("-o", "--output")
.help("Choose to output in plain text or JSON")
.choices("plain-text", "json")
.setDefault("plain-text");
Subparsers subparsers = parser.addSubparsers().title("subcommands").dest("command");
final Map<String, Command> commands = Commands.getCommands();
for (Map.Entry<String, Command> entry : commands.entrySet()) {
Subparser subparser = subparsers.addParser(entry.getKey());
entry.getValue().attachToSubparser(subparser);
}
return parser;
}
public Cli(final Namespace ns) {
this.ns = ns;
}
public int init() {
Command command = getCommand();
String commandKey = ns.getString("command");
Command command = Commands.getCommand(commandKey);
if (command == null) {
logger.error("Command not implemented!");
return 1;
@ -49,9 +93,11 @@ public class Cli {
String username = ns.getString("username");
if (ns.getBoolean("dbus") || ns.getBoolean("dbus_system")) {
final boolean useDbus = ns.getBoolean("dbus");
final boolean useDbusSystem = ns.getBoolean("dbus_system");
if (useDbus || useDbusSystem) {
// If username is null, it will connect to the default object path
return initDbusClient(command, username, ns.getBoolean("dbus_system"));
return initDbusClient(command, username, useDbusSystem);
}
final File dataPath;
@ -208,11 +254,6 @@ public class Cli {
return manager;
}
private Command getCommand() {
String commandKey = ns.getString("command");
return Commands.getCommand(commandKey);
}
private int initDbusClient(final Command command, final String username, final boolean systemBus) {
try {
DBusConnection.DBusBusType busType;