diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt index 143e6c5..53b968e 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt @@ -9,6 +9,7 @@ import ley.anvil.modpacktools.command.CommandReturn.Companion.fail import ley.anvil.modpacktools.command.CommandReturn.Companion.success import ley.anvil.modpacktools.command.LoadCommand import ley.anvil.modpacktools.util.addonscript.installFile +import ley.anvil.modpacktools.util.arg import ley.anvil.modpacktools.util.downloadFiles import ley.anvil.modpacktools.util.fPrintln import ley.anvil.modpacktools.util.manifest.convertAStoManifest @@ -33,9 +34,10 @@ object BuildTwitch : AbstractCommand("BuildTwitch") { private val downloadDir by lazy {File(tempDir, "download")} override fun ArgumentParser.addArgs() { - addArgument("-a", "--all") - .help("Downloads all relations instead of just required ones") - .action(storeTrue()) + arg("-a", "--all") { + help("Downloads all relations instead of just required ones") + action(storeTrue()) + } } override fun execute(args: Namespace): CommandReturn { diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt index caee447..c9c3667 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt @@ -22,6 +22,7 @@ import ley.anvil.modpacktools.command.AbstractCommand import ley.anvil.modpacktools.command.CommandReturn import ley.anvil.modpacktools.command.CommandReturn.Companion.success import ley.anvil.modpacktools.command.LoadCommand +import ley.anvil.modpacktools.util.arg import ley.anvil.modpacktools.util.fPrintln import net.sourceforge.argparse4j.impl.Arguments.storeTrue import net.sourceforge.argparse4j.impl.type.CaseInsensitiveEnumNameArgumentType @@ -41,22 +42,26 @@ object CreateModlist : AbstractCommand("CreateModlist") { override val helpMessage: String = "This creates a modlist either as html or csv file." override fun ArgumentParser.addArgs() { - addArgument("-s", "--sorting") - .type(CaseInsensitiveEnumNameArgumentType(Sorting::class.java)) - .setDefault(Sorting.NAME) - .help("Determines How mods should be sorted") + arg("-s", "--sorting") { + default = Sorting.NAME + type(CaseInsensitiveEnumNameArgumentType(Sorting::class.java)) + help("Determines How mods should be sorted") + } - addArgument("-a", "--all") - .action(storeTrue()) - .help("If this is set, all relations and not only be mods will be in the list") + arg("-a", "--all") { + action(storeTrue()) + help("If this is set, all relations and not only be mods will be in the list") + } - addArgument("type") - .type(CaseInsensitiveEnumNameArgumentType(Format::class.java)) - .help("What format the mod list should be made in") + arg("type") { + type(CaseInsensitiveEnumNameArgumentType(Format::class.java)) + help("What format the mod list should be made in") + } - addArgument("file") - .type(FileArgumentType().verifyNotExists()) - .help("What file the mod list should be written to") + arg("file") { + type(FileArgumentType().verifyNotExists()) + help("What file the mod list should be written to") + } } override fun execute(args: Namespace): CommandReturn { diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/DownloadMods.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/DownloadMods.kt index d860f43..e66cc07 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/DownloadMods.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/DownloadMods.kt @@ -7,6 +7,7 @@ import ley.anvil.modpacktools.command.CommandReturn import ley.anvil.modpacktools.command.CommandReturn.Companion.success import ley.anvil.modpacktools.command.LoadCommand import ley.anvil.modpacktools.util.DownloadFileTask +import ley.anvil.modpacktools.util.arg import ley.anvil.modpacktools.util.downloadFiles import ley.anvil.modpacktools.util.fPrintln import net.sourceforge.argparse4j.impl.Arguments.storeTrue @@ -22,28 +23,30 @@ object DownloadMods : AbstractCommand("DownloadMods") { override val helpMessage: String = "Downloads all mods." override fun ArgumentParser.addArgs() { - addArgument("dir") - .type(FileArgumentType().verifyCanCreate()) - .help("the directory to download the mods to") + arg("dir") { + type(FileArgumentType().verifyCanCreate()) + help("the directory to download the mods to") + } - addArgument("-f", "--force") - .action(storeTrue()) - .help("if true, mods that are already in the download folder will be downloaded again") + arg("-f", "--force") { + action(storeTrue()) + help("if true, mods that are already in the download folder will be downloaded again") + } - addArgument("-a", "--all") - .action(storeTrue()) - .help("Downloads not only mods but everything with a dir installer") + arg("-a", "--all") { + action(storeTrue()) + help("Downloads not only mods but everything with a dir installer") + } } override fun execute(args: Namespace): CommandReturn { val json = MPJH.asWrapper val fileList = mutableListOf() for( - rel in json!!.defaultVersion!!.getRelations( - arrayOf("client"), - if(args.getBoolean("all")) null else arrayOf("mod") - )!! - ) //TODO only client? what if someone wants a server? + rel in json!!.defaultVersion!!.getRelations { + "client" in it.options && (args.getBoolean("all") || it.type == "mod") + } + ) if(rel.hasFile()) fileList.add(rel.file.get()) diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/Import.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/Import.kt index a8dbf43..384e7f2 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/Import.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/Import.kt @@ -8,6 +8,7 @@ import ley.anvil.modpacktools.command.CommandReturn import ley.anvil.modpacktools.command.CommandReturn.Companion.fail import ley.anvil.modpacktools.command.CommandReturn.Companion.success import ley.anvil.modpacktools.command.LoadCommand +import ley.anvil.modpacktools.util.arg import ley.anvil.modpacktools.util.readAsJson import net.sourceforge.argparse4j.impl.type.FileArgumentType import net.sourceforge.argparse4j.inf.ArgumentParser @@ -20,9 +21,10 @@ object Import : AbstractCommand("Import") { override val helpMessage: String = "Converts a given manifest file to a modpackjson file" override fun ArgumentParser.addArgs() { - addArgument("manifest") - .help("the manifest file to import") - .type(FileArgumentType().verifyIsFile()) + arg("manifest") { + help("the manifest file to import") + type(FileArgumentType().verifyIsFile()) + } } override val needsModpackjson: Boolean = false diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/ListRelations.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/ListRelations.kt index 49dff76..edc0452 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/ListRelations.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/ListRelations.kt @@ -6,6 +6,7 @@ import ley.anvil.modpacktools.command.AbstractCommand import ley.anvil.modpacktools.command.CommandReturn import ley.anvil.modpacktools.command.CommandReturn.Companion.success import ley.anvil.modpacktools.command.LoadCommand +import ley.anvil.modpacktools.util.arg import net.sourceforge.argparse4j.impl.Arguments.storeTrue import net.sourceforge.argparse4j.inf.ArgumentParser import net.sourceforge.argparse4j.inf.Namespace @@ -15,17 +16,20 @@ object ListRelations : AbstractCommand("ListRelations") { override val helpMessage: String = "Lists the relations of this mod pack" override fun ArgumentParser.addArgs() { - addArgument("-c", "--csv") - .help("Doesn't format as a table but instead as csv (separated by ;)") - .action(storeTrue()) + arg("-c", "--csv") { + help("Doesn't format as a table but instead as csv (separated by ;)") + action(storeTrue()) + } - addArgument("-n", "--nolimit") - .help("does not limit the size of the authors list") - .action(storeTrue()) + arg("-n", "--nolimit") { + help("does not limit the size of the authors list") + action(storeTrue()) + } - addArgument("-d", "--description") - .help("adds the description of relations to the list") - .action(storeTrue()) + arg("-d", "--description") { + help("adds the description of relations to the list") + action(storeTrue()) + } } override fun execute(args: Namespace): CommandReturn { diff --git a/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt b/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt index e28b938..41d0771 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt @@ -5,6 +5,8 @@ package ley.anvil.modpacktools.util import com.google.gson.JsonObject import com.google.gson.JsonParser import ley.anvil.modpacktools.HTTP_CLIENT +import net.sourceforge.argparse4j.inf.Argument +import net.sourceforge.argparse4j.inf.ArgumentParser import okhttp3.MediaType import okhttp3.MediaType.Companion.toMediaType import okhttp3.Request @@ -164,3 +166,14 @@ fun File.unzip(outputDir: File) { } stream.close() } + +/** + * this makes arguments for ArgumentParsers look cleaner + * + * @receiver the parser to add the argument to + * @param names the names the argument will have + * @param block this will be called on the argument. it is for settings like help message + */ +fun ArgumentParser.arg(vararg names: String, block: Argument.() -> Unit) { + addArgument(*names).block() +}