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;

View file

@ -20,19 +20,13 @@ import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
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.commands.Command;
import org.asamk.signal.commands.Commands;
import org.asamk.signal.manager.LibSignalLogger;
import org.asamk.signal.util.SecurityProvider;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Security;
import java.util.Map;
public class Main {
@ -40,24 +34,11 @@ public class Main {
installSecurityProviderWorkaround();
// Configuring the logger needs to happen before any logger is initialized
if (isVerbose(args)) {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug");
System.setProperty("org.slf4j.simpleLogger.showThreadName", "true");
System.setProperty("org.slf4j.simpleLogger.showShortLogName", "false");
System.setProperty("org.slf4j.simpleLogger.showDateTime", "true");
System.setProperty("org.slf4j.simpleLogger.dateTimeFormat", "yyyy-MM-dd'T'HH:mm:ss.SSSXX");
LibSignalLogger.initLogger();
} else {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info");
System.setProperty("org.slf4j.simpleLogger.showThreadName", "false");
System.setProperty("org.slf4j.simpleLogger.showShortLogName", "true");
System.setProperty("org.slf4j.simpleLogger.showDateTime", "false");
}
configureLogging(isVerbose(args));
Namespace ns = parseArgs(args);
if (ns == null) {
System.exit(2);
}
ArgumentParser parser = Cli.buildArgumentParser();
Namespace ns = parser.parseArgsOrFail(args);
int res = new Cli(ns).init();
System.exit(res);
@ -70,7 +51,8 @@ public class Main {
}
private static boolean isVerbose(String[] args) {
ArgumentParser parser = buildBaseArgumentParser();
ArgumentParser parser = ArgumentParsers.newFor("signal-cli").build().defaultHelp(false);
parser.addArgument("--verbose").action(Arguments.storeTrue());
Namespace ns;
try {
@ -82,63 +64,19 @@ public class Main {
return ns.getBoolean("verbose");
}
private static Namespace parseArgs(String[] args) {
ArgumentParser parser = buildArgumentParser();
Namespace ns;
try {
ns = parser.parseArgs(args);
} catch (ArgumentParserException e) {
parser.handleError(e);
return null;
}
return ns;
}
private static ArgumentParser buildArgumentParser() {
ArgumentParser parser = buildBaseArgumentParser();
Subparsers subparsers = parser.addSubparsers()
.title("subcommands")
.dest("command")
.description("valid subcommands")
.help("additional help");
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;
}
private static ArgumentParser buildBaseArgumentParser() {
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");
return parser;
private static void configureLogging(final boolean verbose) {
if (verbose) {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug");
System.setProperty("org.slf4j.simpleLogger.showThreadName", "true");
System.setProperty("org.slf4j.simpleLogger.showShortLogName", "false");
System.setProperty("org.slf4j.simpleLogger.showDateTime", "true");
System.setProperty("org.slf4j.simpleLogger.dateTimeFormat", "yyyy-MM-dd'T'HH:mm:ss.SSSXX");
LibSignalLogger.initLogger();
} else {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info");
System.setProperty("org.slf4j.simpleLogger.showThreadName", "false");
System.setProperty("org.slf4j.simpleLogger.showShortLogName", "true");
System.setProperty("org.slf4j.simpleLogger.showDateTime", "false");
}
}
}