1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 08:46:15 +00:00

Rewrote CommandLineOptions: The code is now better readable and accepts parameters like --target=/data and accepts some short parameters like -t for --target.

This commit is contained in:
Fabian Schlenz 2018-04-10 06:36:18 +02:00
parent 069799cbaf
commit a9444e7813

View File

@ -19,26 +19,42 @@ class CommandLineOptions(args: Array<String>) {
val booleans = mutableListOf<String>() val booleans = mutableListOf<String>()
val values = mutableMapOf<String, String>() val values = mutableMapOf<String, String>()
var last_key: String? = null var last_key: String? = null
val substitutions = mapOf("-t" to "--target")
init { init {
for(arg in args) { val list = args.toMutableList()
if (arg.startsWith("--")) {
if (last_key!=null) { while (list.isNotEmpty()) {
booleans.add(last_key!!)
values.put(last_key!!, "true") var current_arg = list.removeAt(0)
} if (!current_arg.startsWith("-")) throw RuntimeException("Unexpected unnamed parameter ${current_arg}")
last_key = arg.substring(2)
} else { var next_arg: String? = null
if (last_key==null) {
throw RuntimeException("Unexpected unnamed parameter ${arg}") if (current_arg.contains("=")) {
} val parts = current_arg.split("=", limit=2)
values.put(last_key!!, arg) current_arg = parts[0]
last_key = null next_arg = parts[1]
} else if (list.isNotEmpty() && !list[0].startsWith("--")) {
next_arg = list.removeAt(0)
}
if (!current_arg.startsWith("--") && current_arg.startsWith("-")) {
val replacement = substitutions.get(current_arg)
if (replacement == null) throw RuntimeException("Unknown short parameter ${current_arg}")
current_arg = replacement
}
current_arg = current_arg.substring(2)
if (next_arg == null) {
// current_arg seems to be a boolean value
booleans.add(current_arg)
values.put(current_arg, "true")
} else {
// current_arg has the value next_arg
values.put(current_arg, next_arg)
} }
}
if (last_key!=null) {
booleans.add(last_key!!)
values.put(last_key!!, "true")
} }
} }
} }