From 41cb26bcdb2a2764a09274bd358f0694c91fc9de Mon Sep 17 00:00:00 2001 From: rom Date: Wed, 19 May 2021 21:27:00 +0200 Subject: Plain mode --- curr | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/curr b/curr index 4b036d1..15ed195 100755 --- a/curr +++ b/curr @@ -49,23 +49,29 @@ fun E_INVALID_CODE(code: String): Int = fun getApi(key: String): CurrencyApi? = URL(path(key)).openStream().use { Klaxon().parse(it) } fun main(): Int { - if (args.size == 1 && args[0] == "--help") { + val args = args.toMutableList() + if (args.size == 1 && (args[0] == "--help" || args[0] == "-h")) { System.err.println( """ - Usage: curr + Usage: curr [-p/--plain] Usage: curr --codes Usage: curr --help + --plain -p Plain output mode. + --codes -c Display currency code list. + --help -h Display this help message. + Set your https://currencyapi.net/ api key in ${'$'}CURRENCY_API_KEY """.trimIndent() ) return 0 } - if (args.size == 1 && args[0] == "--codes") { + if (args.size == 1 && (args[0] == "--codes" || args[0] == "-c")) { val api = getApi(apiKey ?: return E_NO_API_KEY()) ?: return E_APIERR() println((api.rates.keys + api.base).joinToString("\n")) return 0 } + var isPlain = args.removeAll { it == "-p" || it == "--plain" } if (args.size == 3) { val amount: Double = try { args[2].toDouble() @@ -75,7 +81,12 @@ fun main(): Int { val api = getApi(apiKey ?: return E_NO_API_KEY()) ?: return E_APIERR() if (api.rate(args[0]) == null) return E_INVALID_CODE(args[0]) if (api.rate(args[1]) == null) return E_INVALID_CODE(args[1]) - println("$amount ${args[0]} to ${args[1]}: ${api.convert(args[0], amount, args[1])}") + val converted = api.convert(args[0], amount, args[1]) + if (isPlain) { + println(converted) + } else { + println("$amount ${args[0]} to ${args[1]}: $converted") + } return 0 } return E_UNKNOWN_USAGE() -- cgit