4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-05-19 11:54:20 +02:00

add and implement arg function to make arguments for commands cleaner and more kotliny

This commit is contained in:
LordMZTE 2020-08-14 19:47:19 +02:00
parent 38d9f13547
commit 77b146cb6c
6 changed files with 71 additions and 42 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -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<FileOrLink>()
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())

View file

@ -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

View file

@ -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 {

View file

@ -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()
}