[+] Argparser extensions
This commit is contained in:
+5
-2
@@ -3,6 +3,7 @@ plugins {
|
||||
id 'application'
|
||||
id "org.jetbrains.kotlin.jvm" version "1.7.21"
|
||||
id "org.openjfx.javafxplugin" version "0.0.13"
|
||||
id "com.github.johnrengelman.shadow" version "7.1.2"
|
||||
}
|
||||
|
||||
repositories {
|
||||
@@ -11,8 +12,10 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// This dependency is used by the application.
|
||||
implementation 'com.google.guava:guava:31.0.1-jre'
|
||||
implementation 'com.google.guava:guava:31.1-jre'
|
||||
|
||||
// https://mvnrepository.com/artifact/net.sourceforge.argparse4j/argparse4j
|
||||
implementation 'net.sourceforge.argparse4j:argparse4j:0.9.0'
|
||||
}
|
||||
|
||||
javafx {
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package mars
|
||||
|
||||
import net.sourceforge.argparse4j.impl.Arguments
|
||||
import net.sourceforge.argparse4j.inf.Argument
|
||||
import net.sourceforge.argparse4j.inf.ArgumentParser
|
||||
import net.sourceforge.argparse4j.inf.Namespace
|
||||
import java.util.*
|
||||
import kotlin.collections.HashMap
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
|
||||
val ARG_PARSER_MAP = HashMap<ArgumentParser, Namespace>()
|
||||
|
||||
fun ArgumentParser.parseToVars(args: Array<String>) = parseArgs(args).also { ARG_PARSER_MAP[this] = it }
|
||||
|
||||
class ArgParserDelegate<T>(val a: ArgumentParser, val name: String)
|
||||
{
|
||||
operator fun getValue(thisRef: Any?, property: KProperty<*>): T?
|
||||
{
|
||||
return ARG_PARSER_MAP[a]?.get(name)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> ArgumentParser.arg(vararg names: String, help: String? = null, apply: Argument.() -> Unit = {}): ArgParserDelegate<T>
|
||||
{
|
||||
val varname = names.last().trimStart('-').replace('-', '_')
|
||||
val arg = addArgument(*names).dest(varname)
|
||||
if (help != null) arg.help(help)
|
||||
apply(arg)
|
||||
return ArgParserDelegate(this, varname)
|
||||
}
|
||||
|
||||
fun ArgumentParser.string(vararg names: String, help: String? = null, apply: Argument.() -> Unit = {}) =
|
||||
arg<String>(*names, help=help, apply=apply)
|
||||
|
||||
fun ArgumentParser.flag(vararg names: String, help: String? = null, apply: Argument.() -> Unit = {}) =
|
||||
arg<Boolean>(*names, help=help) { action(Arguments.storeTrue()); apply() }
|
||||
|
||||
Reference in New Issue
Block a user