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

AbsractCommand uses property delegate for arguments instead of function now

This commit is contained in:
LordMZTE 2020-08-19 15:28:53 +02:00
parent dcc36e7ebf
commit 3fbd93a519
6 changed files with 11 additions and 20 deletions

View file

@ -6,7 +6,7 @@ import net.sourceforge.argparse4j.inf.ArgumentParser
/**
* an implementation of [ICommand] meant to reduce boilerplate.
* this automatically creates a base [ArgumentParser]
* with the [helpMessage] as description and then applies [addArgs] to it
* with the [helpMessage] as description which can be modified by using [argParser]
* and uses [displayName] as the name for the command in the help message.
*
* the [name] of the command will be a converted version of the [displayName] by default
@ -21,18 +21,12 @@ constructor(
val displayName: String,
override val name: String = displayName.toLowerCase().replace(' ', '_')
) : ICommand {
override val parser: ArgumentParser by lazy {
ArgumentParsers.newFor(displayName)
protected fun argParser(block: ArgumentParser.() -> Unit) = lazy {
ArgumentParsers.newFor(this.displayName)
.build()
.description(helpMessage)
.apply {addArgs()}
.description(this.helpMessage)
.apply(block)
}
/**
* This will be called to add arguments to the [ArgumentParser] of this command.
* override this to add arguments.
*
* @receiver the [ArgumentParser] to add the args to
*/
protected open fun ArgumentParser.addArgs() {}
override val parser: ArgumentParser by argParser {}
}

View file

@ -16,7 +16,6 @@ import ley.anvil.modpacktools.util.downloadFiles
import ley.anvil.modpacktools.util.fPrintln
import ley.anvil.modpacktools.util.toZip
import net.sourceforge.argparse4j.impl.Arguments.storeTrue
import net.sourceforge.argparse4j.inf.ArgumentParser
import net.sourceforge.argparse4j.inf.Namespace
import org.apache.commons.io.FileUtils
import java.io.File
@ -32,7 +31,7 @@ object BuildTwitch : AbstractCommand("BuildTwitch") {
private val tmp: File by lazy {File(tempDir, "twitch")}
private val downloadDir by lazy {File(tempDir, "download")}
override fun ArgumentParser.addArgs() {
override val parser by argParser {
arg("-a", "--all") {
help("Downloads all relations instead of just required ones")
action(storeTrue())

View file

@ -14,7 +14,6 @@ import ley.anvil.modpacktools.util.fPrintln
import net.sourceforge.argparse4j.impl.Arguments.storeTrue
import net.sourceforge.argparse4j.impl.type.CaseInsensitiveEnumNameArgumentType
import net.sourceforge.argparse4j.impl.type.FileArgumentType
import net.sourceforge.argparse4j.inf.ArgumentParser
import net.sourceforge.argparse4j.inf.Namespace
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVPrinter
@ -28,7 +27,7 @@ import java.util.Comparator.comparing
object CreateModlist : AbstractCommand("CreateModlist") {
override val helpMessage: String = "This creates a modlist either as html or csv file."
override fun ArgumentParser.addArgs() {
override val parser by argParser {
arg("type") {
type(CaseInsensitiveEnumNameArgumentType(Format::class.java))
help("What format the mod list should be made in")

View file

@ -12,7 +12,6 @@ import ley.anvil.modpacktools.util.downloadFiles
import ley.anvil.modpacktools.util.fPrintln
import net.sourceforge.argparse4j.impl.Arguments.storeTrue
import net.sourceforge.argparse4j.impl.type.FileArgumentType
import net.sourceforge.argparse4j.inf.ArgumentParser
import net.sourceforge.argparse4j.inf.Namespace
import java.io.File
@ -20,7 +19,7 @@ import java.io.File
object DownloadMods : AbstractCommand("DownloadMods") {
override val helpMessage: String = "Downloads all mods."
override fun ArgumentParser.addArgs() {
override val parser by argParser {
arg("dir") {
type(FileArgumentType().verifyCanCreate())
help("the directory to download the mods to")

View file

@ -20,7 +20,7 @@ import java.io.FileWriter
object Import : AbstractCommand("Import") {
override val helpMessage: String = "Converts a given manifest file to a modpackjson file"
override fun ArgumentParser.addArgs() {
override val parser: ArgumentParser by argParser {
arg("manifest") {
help("the manifest file to import")
type(FileArgumentType().verifyIsFile())

View file

@ -15,7 +15,7 @@ import net.sourceforge.argparse4j.inf.Namespace
object ListRelations : AbstractCommand("ListRelations") {
override val helpMessage: String = "Lists the relations of this mod pack"
override fun ArgumentParser.addArgs() {
override val parser: ArgumentParser by argParser {
arg("-c", "--csv") {
help("Doesn't format as a table but instead as csv (separated by ;)")
action(storeTrue())