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 'com.github.pircbotx:pircbotx:-SNAPSHOT'
implementation fileTree(dir: 'lib', include: '*.jar') implementation fileTree(dir: 'lib', include: '*.jar')
// Commons // 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' implementation 'commons-net:commons-net:3.8.0'
// Google // Google
@ -62,12 +57,13 @@ dependencies {
implementation platform('org.jetbrains.kotlin:kotlin-bom') implementation platform('org.jetbrains.kotlin:kotlin-bom')
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
implementation'org.jetbrains.kotlinx:kotlinx-cli:0.3.5'
// Logging // Logging
implementation 'org.slf4j:slf4j-api:2.0.0' implementation 'org.slf4j:slf4j-api:2.0.0'
implementation "org.apache.logging.log4j:log4j-api:$versions.log4j" implementation "org.apache.logging.log4j:log4j-api:$versions.log4j"
implementation "org.apache.logging.log4j:log4j-core:$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.rometools:rome:1.18.0'
implementation 'com.squareup.okhttp3:okhttp:4.10.0' implementation 'com.squareup.okhttp3:okhttp:4.10.0'

View file

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

View file

@ -32,6 +32,9 @@
package net.thauvin.erik.mobibot 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.appendIfMissing
import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.bot
import net.thauvin.erik.mobibot.Utils.capitalise 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.Weather2
import net.thauvin.erik.mobibot.modules.WorldTime import net.thauvin.erik.mobibot.modules.WorldTime
import net.thauvin.erik.semver.Version 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.Configuration
import org.pircbotx.PircBotX import org.pircbotx.PircBotX
import org.pircbotx.hooks.ListenerAdapter import org.pircbotx.hooks.ListenerAdapter
@ -245,59 +241,42 @@ class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Pro
@Throws(Exception::class) @Throws(Exception::class)
fun main(args: Array<String>) { fun main(args: Array<String>) {
// Set up the command line options // Set up the command line options
val options = Options() val parser = ArgParser(Constants.CLI_CMD)
.addOption( val debug by parser.option(
Constants.HELP_ARG.substring(0, 1), ArgType.Boolean,
Constants.HELP_ARG, Constants.DEBUG_ARG,
false, Constants.DEBUG_ARG.substring(0, 1),
"print this help message" "Print debug & logging data directly to the console"
) ).default(false)
.addOption( val property by parser.option(
Constants.DEBUG_ARG.substring(0, 1), Constants.DEBUG_ARG, false, ArgType.String,
"print debug & logging data directly to the console" Constants.PROPS_ARG,
) Constants.PROPS_ARG.substring(0, 1),
.addOption( "Use alternate properties file"
Option.builder(Constants.PROPS_ARG.substring(0, 1)).hasArg() ).default("./mobibot.properties")
.argName("file") val version by parser.option(
.desc("use alternate properties file") ArgType.Boolean,
.longOpt(Constants.PROPS_ARG).build()
)
.addOption(
Constants.VERSION_ARG.substring(0, 1),
Constants.VERSION_ARG, Constants.VERSION_ARG,
false, Constants.VERSION_ARG.substring(0, 1),
"print version info" "Print version info"
) ).default(false)
// Parse the command line // Parse the command line
val parser: CommandLineParser = DefaultParser() parser.parse(args)
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)
}
commandLine.hasOption(Constants.VERSION_ARG[0]) -> { if (version) {
// Output the version // Output the version
println("${ReleaseInfo.PROJECT.capitalise()} ${ReleaseInfo.VERSION}" + println(
" (${ReleaseInfo.BUILDDATE.toIsoLocalDate()})") "${ReleaseInfo.PROJECT.capitalise()} ${ReleaseInfo.VERSION}" +
" (${ReleaseInfo.BUILDDATE.toIsoLocalDate()})"
)
println(ReleaseInfo.WEBSITE) println(ReleaseInfo.WEBSITE)
} } else {
else -> {
// Load the properties // Load the properties
val p = Properties() val p = Properties()
try { try {
Files.newInputStream( Files.newInputStream(
Paths.get(commandLine.getOptionValue(Constants.PROPS_ARG[0], "./mobibot.properties")) Paths.get(property)
).use { fis -> ).use { fis ->
p.load(fis) p.load(fis)
} }
@ -313,7 +292,7 @@ class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Pro
val logsDir = p.getProperty("logs", ".").appendIfMissing(File.separatorChar) val logsDir = p.getProperty("logs", ".").appendIfMissing(File.separatorChar)
// Redirect stdout and stderr // Redirect stdout and stderr
if (!commandLine.hasOption(Constants.DEBUG_ARG[0])) { if (!debug) {
try { try {
val stdout = PrintStream( val stdout = PrintStream(
BufferedOutputStream( BufferedOutputStream(
@ -345,7 +324,6 @@ class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Pro
} }
} }
} }
}
/** /**
* Initialize the bot. * Initialize the bot.

View file

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