diff --git a/build.gradle b/build.gradle index 996fcf4..adade90 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Constants.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Constants.kt index 6b82d55..543511f 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Constants.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Constants.kt @@ -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. diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt index 10df273..2e686af 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt @@ -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) { // 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() } } } diff --git a/website/index.html b/website/index.html index 979f5c8..acab3b3 100644 --- a/website/index.html +++ b/website/index.html @@ -36,12 +36,11 @@

mobibot is making extensive use of various open source libraries, including: