1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-12-24 22:15:33 +00:00

CommandLineOptions is now static to make usage easier.

This commit is contained in:
Fabian Schlenz 2016-07-11 18:11:12 +02:00
parent 71150a36d2
commit 6499aed3c1
4 changed files with 50 additions and 53 deletions

View File

@ -36,45 +36,45 @@ public class CommandLineController {
public TelegramApp app;
public UserManager user = null;
public CommandLineController(CommandLineOptions options) {
if (options.cmd_version) {
public CommandLineController() {
if (CommandLineOptions.cmd_version) {
System.out.println("Telegram_Backup version " + Config.APP_APPVER + ", Copyright (C) 2016 Fabian Schlenz");
System.out.println("Telegram_Backup comes with ABSOLUTELY NO WARRANTY. This is free software, and you are");
System.out.println("welcome to redistribute it under certain conditions; run it with '--license' for details.");
System.exit(0);
} else if (options.cmd_help) {
} else if (CommandLineOptions.cmd_help) {
this.show_help();
System.exit(0);
} else if (options.cmd_license) {
} else if (CommandLineOptions.cmd_license) {
this.show_license();
System.exit(0);
}
if (options.target != null) {
Config.FILE_BASE = options.target;
if (CommandLineOptions.val_target != null) {
Config.FILE_BASE = CommandLineOptions.val_target;
}
System.out.println("Base directory for files: " + Config.FILE_BASE);
if (options.cmd_list_accounts) this.list_accounts();
if (CommandLineOptions.cmd_list_accounts) this.list_accounts();
app = new TelegramApp(Config.APP_ID, Config.APP_HASH, Config.APP_MODEL, Config.APP_SYSVER, Config.APP_APPVER, Config.APP_LANG);
if (options.cmd_debug) Kotlogram.setDebugLogEnabled(true);
if (CommandLineOptions.cmd_debug) Kotlogram.setDebugLogEnabled(true);
String account = null;
Vector<String> accounts = Utils.getAccounts();
if (options.cmd_login) {
if (CommandLineOptions.cmd_login) {
// do nothing
} else if (options.account!=null) {
} else if (CommandLineOptions.val_account!=null) {
boolean found = false;
for (String acc : accounts) if (acc.equals(options.account)) found=true;
for (String acc : accounts) if (acc.equals(CommandLineOptions.val_account)) found=true;
if (!found) {
show_error("Couldn't find account '" + options.account + "'. Maybe you want to use '--login' first?");
show_error("Couldn't find account '" + CommandLineOptions.val_account + "'. Maybe you want to use '--login' first?");
}
account = options.account;
account = CommandLineOptions.val_account;
} else if (accounts.size()==0) {
System.out.println("No accounts found. Starting login process...");
options.cmd_login = true;
CommandLineOptions.cmd_login = true;
} else if (accounts.size()==1) {
account = accounts.firstElement();
System.out.println("Using only available account: " + account);
@ -92,11 +92,11 @@ public class CommandLineController {
try {
user = new UserManager(client);
if (options.export != null) {
if (options.export.toLowerCase().equals("html")) {
if (CommandLineOptions.val_export != null) {
if (CommandLineOptions.val_export.toLowerCase().equals("html")) {
(new HTMLExporter()).export(user);
System.exit(0);
} else if (options.export.toLowerCase().equals("stats")) {
} else if (CommandLineOptions.val_export.toLowerCase().equals("stats")) {
(new StatsExporter()).export(user);
System.exit(0);
} else {
@ -105,7 +105,7 @@ public class CommandLineController {
}
if (options.cmd_login) {
if (CommandLineOptions.cmd_login) {
cmd_login();
System.exit(0);
}
@ -113,14 +113,14 @@ public class CommandLineController {
System.out.println("You are logged in as " + user.getUserString());
DownloadManager d = new DownloadManager(user, client, new CommandLineDownloadProgress());
d.downloadMessages(options.limit_messages);
d.downloadMessages(CommandLineOptions.val_limit_messages);
d.downloadMedia();
} catch (RpcErrorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (options.cmd_daemon) {
if (CommandLineOptions.cmd_daemon) {
handler.setUser(user, client);
System.out.println("DAEMON mode requested - keeping running.");
} else {

View File

@ -17,36 +17,36 @@
package de.fabianonline.telegram_backup;
class CommandLineOptions {
public boolean cmd_console = false;
public String account = null;
public boolean cmd_help = false;
public boolean cmd_login = false;
public boolean cmd_debug = false;
public boolean cmd_list_accounts = false;
public Integer limit_messages = null;
public String target = null;
public boolean cmd_version = false;
public String export = null;
public boolean cmd_license = false;
public boolean cmd_daemon = false;
public static boolean cmd_console = false;
public static String val_account = null;
public static boolean cmd_help = false;
public static boolean cmd_login = false;
public static boolean cmd_debug = false;
public static boolean cmd_list_accounts = false;
public static Integer val_limit_messages = null;
public static String val_target = null;
public static boolean cmd_version = false;
public static String val_export = null;
public static boolean cmd_license = false;
public static boolean cmd_daemon = false;
public CommandLineOptions(String[] args) {
public static void parseOptions(String[] args) {
String last_cmd = null;
for (String arg : args) {
if (last_cmd != null) {
switch (last_cmd) {
case "--account":
this.account = arg;
val_account = arg;
break;
case "--limit-messages":
this.limit_messages = Integer.parseInt(arg);
val_limit_messages = Integer.parseInt(arg);
break;
case "--target":
this.target = arg;
val_target = arg;
break;
case "--export":
this.export = arg;
val_export = arg;
break;
}
last_cmd = null;
@ -58,37 +58,37 @@ class CommandLineOptions {
last_cmd = "--account"; continue;
case "-h": case "--help":
this.cmd_help = true; break;
cmd_help = true; break;
case "-l": case "--login":
this.cmd_login = true; break;
cmd_login = true; break;
case "--debug":
this.cmd_debug = true; break;
cmd_debug = true; break;
case "-A": case "--list-accounts":
this.cmd_list_accounts = true; break;
cmd_list_accounts = true; break;
case "--limit-messages":
last_cmd = arg; continue;
case "--console":
this.cmd_console = true; break;
cmd_console = true; break;
case "-t": case "--target":
last_cmd = "--target"; continue;
case "-V": case "--version":
this.cmd_version = true; break;
cmd_version = true; break;
case "-e": case "--export":
last_cmd = "--export"; continue;
case "--license":
this.cmd_license = true; break;
cmd_license = true; break;
case "-d": case "--daemon":
this.cmd_daemon = true; break;
cmd_daemon = true; break;
default:
throw new RuntimeException("Unknown command " + arg);

View File

@ -20,12 +20,12 @@ import de.fabianonline.telegram_backup.CommandLineController;
public class CommandLineRunner {
public static void main(String[] args) {
CommandLineOptions options = new CommandLineOptions(args);
if (true || options.cmd_console) {
CommandLineOptions.parseOptions(args);
if (true || CommandLineOptions.cmd_console) {
// Always use the console for now.
new CommandLineController(options);
new CommandLineController();
} else {
new GUIController(options);
new GUIController();
}
}
}

View File

@ -25,10 +25,7 @@ import java.awt.event.ActionListener;
import java.util.Vector;
public class GUIController {
private CommandLineOptions options;
public GUIController(CommandLineOptions options) {
this.options = options;
public GUIController() {
showAccountChooserDialog();
}