2

I need to parse info from command line, so I choose args4j library. Format of command line: ls [-l] [-h] [-r] [-o output.file] directory_or_file.

I've tried to create options and then parse. When I get only "-ls", output is correct, but when I get "-ls -l"(more than 1 argument) the program does not stop and I can enter on the command line until I forcibly stop the program. I will be grateful for any help!

public class Settings {
    @Option(name = "-ls", usage = "Init command")
    var command  = false
    @Option(name = "-l", usage = "Long flag")
    var longFlag  = false
    @Option(name = "-h")
    var humanReadable = false
    @Option(name = "-r")
    var reversed = false

}
fun main() {
    val settings = Settings()
    val parser = CmdLineParser(settings)
    var args = mutableListOf<String>()
    val input = readLine()
    parser.parseArgument(input)
    print("${settings.command} ${settings.longFlag}")
Marlock
  • 275
  • 2
  • 9

1 Answers1

0

This might not exactly answer your question but if your platform allows, you may try adding parameters as environment variables. It is usually more convenient to define them in the Makefile or Dockerfile if you're working with microservices. Or you can add them in front of the app command to push them into the app as environment variables. The syntax is:

FOO=bar appcommand

With kotlin, you can use the following example to read environment variables:

System.getenv("FOO")

will return "bar".

matrik
  • 104
  • 3