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,104 +241,86 @@ 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() Constants.VERSION_ARG,
) Constants.VERSION_ARG.substring(0, 1),
.addOption( "Print version info"
Constants.VERSION_ARG.substring(0, 1), ).default(false)
Constants.VERSION_ARG,
false,
"print version info"
)
// 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}" +
println(ReleaseInfo.WEBSITE) " (${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 -> { // Redirect stdout and stderr
// Load the properties if (!debug) {
val p = Properties()
try { try {
Files.newInputStream( val stdout = PrintStream(
Paths.get(commandLine.getOptionValue(Constants.PROPS_ARG[0], "./mobibot.properties")) BufferedOutputStream(
).use { fis -> FileOutputStream(
p.load(fis) logsDir + channel.substring(1) + '.' + Utils.today() + ".log", true
} )
} catch (ignore: FileNotFoundException) { ), true
System.err.println("Unable to find properties file.") )
exitProcess(1) System.setOut(stdout)
} catch (ignore: IOException) { } catch (ignore: IOException) {
System.err.println("Unable to open properties file.") System.err.println("Unable to open output (stdout) log file.")
exitProcess(1) exitProcess(1)
} }
val nickname = p.getProperty("nick", Mobibot::class.java.name.lowercase()) try {
val channel = p.getProperty("channel") val stderr = PrintStream(
val logsDir = p.getProperty("logs", ".").appendIfMissing(File.separatorChar) BufferedOutputStream(
FileOutputStream("$logsDir$nickname.err", true)
// Redirect stdout and stderr ), true
if (!commandLine.hasOption(Constants.DEBUG_ARG[0])) { )
try { System.setErr(stderr)
val stdout = PrintStream( } catch (ignore: IOException) {
BufferedOutputStream( System.err.println("Unable to open error (stderr) log file.")
FileOutputStream( exitProcess(1)
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)
}
} }
// 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> <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>
@ -142,4 +141,4 @@
</div> </div>
</body> </body>
</html> </html>