Switched to kotlinx-cli from Apache Commons CLI

This commit is contained in:
Erik C. Thauvin 2022-09-14 01:43:43 -07:00
parent aa0c3dd81c
commit af839918a1
4 changed files with 78 additions and 110 deletions

View file

@ -46,12 +46,7 @@ dependencies {
//implementation 'com.github.pircbotx:pircbotx:-SNAPSHOT'
implementation fileTree(dir: 'lib', include: '*.jar')
// Commons
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'org.apache.commons:commons-text:1.9'
implementation 'commons-cli:commons-cli:1.5.0'
implementation 'commons-codec:commons-codec:1.15'
implementation 'commons-net:commons-net:3.8.0'
// Google
@ -62,12 +57,13 @@ dependencies {
implementation platform('org.jetbrains.kotlin:kotlin-bom')
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
implementation'org.jetbrains.kotlinx:kotlinx-cli:0.3.5'
// Logging
implementation 'org.slf4j:slf4j-api:2.0.0'
implementation "org.apache.logging.log4j:log4j-api:$versions.log4j"
implementation "org.apache.logging.log4j:log4j-core:$versions.log4j"
implementation "org.apache.logging.log4j:log4j-slf4j-impl:$versions.log4j"
implementation "org.apache.logging.log4j:log4j-slf4j18-impl:$versions.log4j"
implementation 'com.rometools:rome:1.18.0'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'

View file

@ -58,12 +58,7 @@ object Constants {
/**
* CLI command for usage.
*/
const val CLI_CMD = "java -jar ${ReleaseInfo.PROJECT}.jar [OPTIONS]"
/**
* Help command line argument.
*/
const val HELP_ARG = "help"
const val CLI_CMD = "java -jar ${ReleaseInfo.PROJECT}.jar"
/**
* The help command.

View file

@ -32,6 +32,9 @@
package net.thauvin.erik.mobibot
import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.default
import net.thauvin.erik.mobibot.Utils.appendIfMissing
import net.thauvin.erik.mobibot.Utils.bot
import net.thauvin.erik.mobibot.Utils.capitalise
@ -77,13 +80,6 @@ import net.thauvin.erik.mobibot.modules.War
import net.thauvin.erik.mobibot.modules.Weather2
import net.thauvin.erik.mobibot.modules.WorldTime
import net.thauvin.erik.semver.Version
import org.apache.commons.cli.CommandLine
import org.apache.commons.cli.CommandLineParser
import org.apache.commons.cli.DefaultParser
import org.apache.commons.cli.HelpFormatter
import org.apache.commons.cli.Option
import org.apache.commons.cli.Options
import org.apache.commons.cli.ParseException
import org.pircbotx.Configuration
import org.pircbotx.PircBotX
import org.pircbotx.hooks.ListenerAdapter
@ -245,104 +241,86 @@ class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Pro
@Throws(Exception::class)
fun main(args: Array<String>) {
// Set up the command line options
val options = Options()
.addOption(
Constants.HELP_ARG.substring(0, 1),
Constants.HELP_ARG,
false,
"print this help message"
)
.addOption(
Constants.DEBUG_ARG.substring(0, 1), Constants.DEBUG_ARG, false,
"print debug & logging data directly to the console"
)
.addOption(
Option.builder(Constants.PROPS_ARG.substring(0, 1)).hasArg()
.argName("file")
.desc("use alternate properties file")
.longOpt(Constants.PROPS_ARG).build()
)
.addOption(
Constants.VERSION_ARG.substring(0, 1),
Constants.VERSION_ARG,
false,
"print version info"
)
val parser = ArgParser(Constants.CLI_CMD)
val debug by parser.option(
ArgType.Boolean,
Constants.DEBUG_ARG,
Constants.DEBUG_ARG.substring(0, 1),
"Print debug & logging data directly to the console"
).default(false)
val property by parser.option(
ArgType.String,
Constants.PROPS_ARG,
Constants.PROPS_ARG.substring(0, 1),
"Use alternate properties file"
).default("./mobibot.properties")
val version by parser.option(
ArgType.Boolean,
Constants.VERSION_ARG,
Constants.VERSION_ARG.substring(0, 1),
"Print version info"
).default(false)
// Parse the command line
val parser: CommandLineParser = DefaultParser()
val commandLine: CommandLine
try {
commandLine = parser.parse(options, args)
} catch (e: ParseException) {
System.err.println(e.message)
HelpFormatter().printHelp(Constants.CLI_CMD, options)
exitProcess(1)
}
when {
commandLine.hasOption(Constants.HELP_ARG[0]) -> {
// Output the usage
HelpFormatter().printHelp(Constants.CLI_CMD, options)
}
parser.parse(args)
commandLine.hasOption(Constants.VERSION_ARG[0]) -> {
// Output the version
println("${ReleaseInfo.PROJECT.capitalise()} ${ReleaseInfo.VERSION}" +
" (${ReleaseInfo.BUILDDATE.toIsoLocalDate()})")
println(ReleaseInfo.WEBSITE)
if (version) {
// Output the version
println(
"${ReleaseInfo.PROJECT.capitalise()} ${ReleaseInfo.VERSION}" +
" (${ReleaseInfo.BUILDDATE.toIsoLocalDate()})"
)
println(ReleaseInfo.WEBSITE)
} else {
// Load the properties
val p = Properties()
try {
Files.newInputStream(
Paths.get(property)
).use { fis ->
p.load(fis)
}
} catch (ignore: FileNotFoundException) {
System.err.println("Unable to find properties file.")
exitProcess(1)
} catch (ignore: IOException) {
System.err.println("Unable to open properties file.")
exitProcess(1)
}
val nickname = p.getProperty("nick", Mobibot::class.java.name.lowercase())
val channel = p.getProperty("channel")
val logsDir = p.getProperty("logs", ".").appendIfMissing(File.separatorChar)
else -> {
// Load the properties
val p = Properties()
// Redirect stdout and stderr
if (!debug) {
try {
Files.newInputStream(
Paths.get(commandLine.getOptionValue(Constants.PROPS_ARG[0], "./mobibot.properties"))
).use { fis ->
p.load(fis)
}
} catch (ignore: FileNotFoundException) {
System.err.println("Unable to find properties file.")
exitProcess(1)
val stdout = PrintStream(
BufferedOutputStream(
FileOutputStream(
logsDir + channel.substring(1) + '.' + Utils.today() + ".log", true
)
), true
)
System.setOut(stdout)
} catch (ignore: IOException) {
System.err.println("Unable to open properties file.")
System.err.println("Unable to open output (stdout) log file.")
exitProcess(1)
}
val nickname = p.getProperty("nick", Mobibot::class.java.name.lowercase())
val channel = p.getProperty("channel")
val logsDir = p.getProperty("logs", ".").appendIfMissing(File.separatorChar)
// Redirect stdout and stderr
if (!commandLine.hasOption(Constants.DEBUG_ARG[0])) {
try {
val stdout = PrintStream(
BufferedOutputStream(
FileOutputStream(
logsDir + channel.substring(1) + '.' + Utils.today() + ".log", true
)
), true
)
System.setOut(stdout)
} catch (ignore: IOException) {
System.err.println("Unable to open output (stdout) log file.")
exitProcess(1)
}
try {
val stderr = PrintStream(
BufferedOutputStream(
FileOutputStream("$logsDir$nickname.err", true)
), true
)
System.setErr(stderr)
} catch (ignore: IOException) {
System.err.println("Unable to open error (stderr) log file.")
exitProcess(1)
}
try {
val stderr = PrintStream(
BufferedOutputStream(
FileOutputStream("$logsDir$nickname.err", true)
), true
)
System.setErr(stderr)
} catch (ignore: IOException) {
System.err.println("Unable to open error (stderr) log file.")
exitProcess(1)
}
// Start the bot
Mobibot(nickname, channel, logsDir, p).connect()
}
// Start the bot
Mobibot(nickname, channel, logsDir, p).connect()
}
}
}

View file

@ -36,12 +36,11 @@
<p>mobibot is making extensive use of various <strong>open source libraries</strong>, including:</p>
<ul>
<li>Apache Commons <a href="https://commons.apache.org/proper/commons-cli/">CLI</a> and <a
href="https://commons.apache.org/proper/commons-net/">Net</a></li>
<li><a href="https://commons.apache.org/proper/commons-net/">Apache Commons Net</a></li>
<li><a href="https://github.com/ethauvin/cryptoprice">CryptoPrice</a></li>
<li><a href="https://www.objecthunter.net/exp4j/">exp4j</a></li>
<li><a href="https://jsoup.org/">jsoup</a></li>
<li><a href="https://ostermiller.org/utils/">OstermillerUtils</a></li>
<li><a href="https://github.com/Kotlin/kotlinx-cli">kotlinx-cli</a></li>
<li><a href="https://square.github.io/okhttp/">OkHttp</a></li>
<li><a href="https://bitbucket.org/aksinghnet/owm-japis">OWM JAPIs</a></li>
<li><a href="https://github.com/ethauvin/pinboard-poster">Pinboard Poster</a></li>
@ -142,4 +141,4 @@
</div>
</body>
</html>
</html>