Home > Software design >  Command-line arguments for Kotlin program
Command-line arguments for Kotlin program

Time:02-05

I stuck with a project. It's an encryption-decryption program. But to complicate the things it uses command-line arguments instead of user input. I solved the user input part. But this part is really hard for me. Here is the objective:

Objectives The program must parse three arguments: -mode, -key, and -data. The first argument should determine the program's mode (enc for encryption, dec for decryption). The second argument is an integer key to modify the message, and the third is a text or ciphertext to encrypt/decrypt.

Arguments are guaranteed to be passed to the program. If, for some reason, they turn out to be wrong:

If there is no -mode, the program should work in the enc mode; If there is no -key, the program should consider that it is 0; If there is no -data, the program should assume that data is an empty string. Keep in mind that the order of the arguments might be different. For example, -mode enc maybe at the end, at the beginning, or in the middle of the array.

Examples Example 1: encryption; the arguments are: -mode enc -key 5 -data "Welcome to hyperskill!"

\jqhtrj%yt%m~ujwxpnqq&

java -jar programName.jar -mode enc -key 5 -data "Welcome to hyperskill!" This way the program inputs \jqhtrj%yt%m~ujwxpnqq& But how to achieve the same input if the program starts with for example java -jar programName.jar -data "Welcome to hyperskill!" -mode enc -key 5 ?? The encoding-decoding part is done in the previous stage. Thanks in advance!

CodePudding user response:

Assuming that the command line arguments will always come in the format of -nameOfArgument and then immediately followed by the argument itself, here is a simple way. For more complicated command line options, I would recommend using a parser library like kotlinx-cli.

Because of the assumption, you will always find an even number of elements in the arguments array passed to main. You can make this array chunked into lists of 2 elements each, and then associate the two elements into a Map.

fun main(args: Array<String>) {
    val argsMap = args.toList().chunked(2).associate { it[0] to it[1] }
}

After that, argsMap["-mode"] gets you the mode, argsMap["-key"] gets you the key. These could also be null, which means that that argument is not passed.

CodePudding user response:

Fisrtly thanks to Sweeper for the assistance! Maybe not the finest solution, but here is my code:

fun main(args: Array<String>) {
    val argsMap = args.toList().chunked(2).associate { it[0] to it[1] }

    if (argsMap["-mode"] == "enc" || argsMap["-mode"] == "") {
            for (char in argsMap["-data"] ?: "") {
                    print(char   (argsMap["-key"]?.toInt() ?: "".toInt()))
            }
    } else if (argsMap["-mode"] == "dec") {
            for (char in argsMap["-data"] ?: "") {
                    print(char - (argsMap["-key"]?.toInt() ?: "".toInt()))
            }
    } else if (argsMap["-key"] == "") {
            print(argsMap["-data"])
    } else if (argsMap["-data"] == "") {
            print("")
    }

}

  • Related