1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-23 01:06:17 +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 TelegramApp app;
public UserManager user = null; public UserManager user = null;
public CommandLineController(CommandLineOptions options) { public CommandLineController() {
if (options.cmd_version) { if (CommandLineOptions.cmd_version) {
System.out.println("Telegram_Backup version " + Config.APP_APPVER + ", Copyright (C) 2016 Fabian Schlenz"); 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("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.out.println("welcome to redistribute it under certain conditions; run it with '--license' for details.");
System.exit(0); System.exit(0);
} else if (options.cmd_help) { } else if (CommandLineOptions.cmd_help) {
this.show_help(); this.show_help();
System.exit(0); System.exit(0);
} else if (options.cmd_license) { } else if (CommandLineOptions.cmd_license) {
this.show_license(); this.show_license();
System.exit(0); System.exit(0);
} }
if (options.target != null) { if (CommandLineOptions.val_target != null) {
Config.FILE_BASE = options.target; Config.FILE_BASE = CommandLineOptions.val_target;
} }
System.out.println("Base directory for files: " + Config.FILE_BASE); 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); 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; String account = null;
Vector<String> accounts = Utils.getAccounts(); Vector<String> accounts = Utils.getAccounts();
if (options.cmd_login) { if (CommandLineOptions.cmd_login) {
// do nothing // do nothing
} else if (options.account!=null) { } else if (CommandLineOptions.val_account!=null) {
boolean found = false; 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) { 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) { } else if (accounts.size()==0) {
System.out.println("No accounts found. Starting login process..."); System.out.println("No accounts found. Starting login process...");
options.cmd_login = true; CommandLineOptions.cmd_login = true;
} else if (accounts.size()==1) { } else if (accounts.size()==1) {
account = accounts.firstElement(); account = accounts.firstElement();
System.out.println("Using only available account: " + account); System.out.println("Using only available account: " + account);
@ -92,11 +92,11 @@ public class CommandLineController {
try { try {
user = new UserManager(client); user = new UserManager(client);
if (options.export != null) { if (CommandLineOptions.val_export != null) {
if (options.export.toLowerCase().equals("html")) { if (CommandLineOptions.val_export.toLowerCase().equals("html")) {
(new HTMLExporter()).export(user); (new HTMLExporter()).export(user);
System.exit(0); System.exit(0);
} else if (options.export.toLowerCase().equals("stats")) { } else if (CommandLineOptions.val_export.toLowerCase().equals("stats")) {
(new StatsExporter()).export(user); (new StatsExporter()).export(user);
System.exit(0); System.exit(0);
} else { } else {
@ -105,7 +105,7 @@ public class CommandLineController {
} }
if (options.cmd_login) { if (CommandLineOptions.cmd_login) {
cmd_login(); cmd_login();
System.exit(0); System.exit(0);
} }
@ -113,14 +113,14 @@ public class CommandLineController {
System.out.println("You are logged in as " + user.getUserString()); System.out.println("You are logged in as " + user.getUserString());
DownloadManager d = new DownloadManager(user, client, new CommandLineDownloadProgress()); DownloadManager d = new DownloadManager(user, client, new CommandLineDownloadProgress());
d.downloadMessages(options.limit_messages); d.downloadMessages(CommandLineOptions.val_limit_messages);
d.downloadMedia(); d.downloadMedia();
} catch (RpcErrorException e) { } catch (RpcErrorException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (options.cmd_daemon) { if (CommandLineOptions.cmd_daemon) {
handler.setUser(user, client); handler.setUser(user, client);
System.out.println("DAEMON mode requested - keeping running."); System.out.println("DAEMON mode requested - keeping running.");
} else { } else {

View File

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

View File

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

View File

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