mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2024-11-22 16:56:16 +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:
parent
069799cbaf
commit
a9444e7813
@ -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}")
|
||||||
|
|
||||||
|
var next_arg: String? = null
|
||||||
|
|
||||||
|
if (current_arg.contains("=")) {
|
||||||
|
val parts = current_arg.split("=", limit=2)
|
||||||
|
current_arg = parts[0]
|
||||||
|
next_arg = parts[1]
|
||||||
|
} else if (list.isNotEmpty() && !list[0].startsWith("--")) {
|
||||||
|
next_arg = list.removeAt(0)
|
||||||
}
|
}
|
||||||
last_key = arg.substring(2)
|
|
||||||
|
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 {
|
} else {
|
||||||
if (last_key==null) {
|
// current_arg has the value next_arg
|
||||||
throw RuntimeException("Unexpected unnamed parameter ${arg}")
|
values.put(current_arg, next_arg)
|
||||||
}
|
}
|
||||||
values.put(last_key!!, arg)
|
|
||||||
last_key = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (last_key!=null) {
|
|
||||||
booleans.add(last_key!!)
|
|
||||||
values.put(last_key!!, "true")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user