diff --git a/endorsedLibraries/kotlinx.cli/src/main/kotlin/kotlinx/cli/ArgumentValues.kt b/endorsedLibraries/kotlinx.cli/src/main/kotlin/kotlinx/cli/ArgumentValues.kt index a8d27c4e17e..eccb5dee861 100644 --- a/endorsedLibraries/kotlinx.cli/src/main/kotlin/kotlinx/cli/ArgumentValues.kt +++ b/endorsedLibraries/kotlinx.cli/src/main/kotlin/kotlinx/cli/ArgumentValues.kt @@ -123,7 +123,8 @@ internal abstract class AbstractArgumentSingleValue(descriptor: Descript internal class ArgumentSingleValue(descriptor: Descriptor): AbstractArgumentSingleValue(descriptor), ArgumentValueDelegate { override var value: T - get() = parsedValue + get() = if (!isEmpty()) parsedValue else error("Value for argument ${descriptor.fullName} isn't set. " + + "ArgParser.parse(...) method should be called before.") set(value) = setDelegatedValue(value) } diff --git a/samples/csvparser/README.md b/samples/csvparser/README.md index 18e7181ace7..668b8b514aa 100644 --- a/samples/csvparser/README.md +++ b/samples/csvparser/README.md @@ -8,7 +8,7 @@ To build use `../gradlew assemble`. To run use `../gradlew runReleaseExecutableCsvParser` or execute the program directly: - ./build/bin/csvParser/main/release/executable/csvparser.kexe ./European_Mammals_Red_List_Nov_2009.csv 4 100 + ./build/bin/csvParser/main/release/executable/csvparser.kexe ./European_Mammals_Red_List_Nov_2009.csv --column 4 --count 100 It will print number of all unique entries in fifth column (Family, zero-based index) in first 100 rows of the CSV file. diff --git a/samples/csvparser/build.gradle.kts b/samples/csvparser/build.gradle.kts index 2b60facd6c8..1e752713257 100644 --- a/samples/csvparser/build.gradle.kts +++ b/samples/csvparser/build.gradle.kts @@ -15,10 +15,11 @@ kotlin { } hostTarget.apply { + compilations["main"].kotlinOptions.freeCompilerArgs = listOf("-l", "kotlinx-cli") binaries { executable { entryPoint = "sample.csvparser.main" - runTask?.args("./European_Mammals_Red_List_Nov_2009.csv", 4, 100) + runTask?.args("--column", 4, "--count", 100, "./European_Mammals_Red_List_Nov_2009.csv") } } } diff --git a/samples/csvparser/src/csvParserMain/kotlin/CsvParser.kt b/samples/csvparser/src/csvParserMain/kotlin/CsvParser.kt index a565d74de60..72ccc2e3e57 100644 --- a/samples/csvparser/src/csvParserMain/kotlin/CsvParser.kt +++ b/samples/csvparser/src/csvParserMain/kotlin/CsvParser.kt @@ -7,6 +7,7 @@ package sample.csvparser import kotlinx.cinterop.* import platform.posix.* +import kotlinx.cli.* fun parseLine(line: String, separator: Char) : List { val result = mutableListOf() @@ -30,13 +31,11 @@ fun parseLine(line: String, separator: Char) : List { } fun main(args: Array) { - if (args.size != 3) { - println("Usage: csvparser.kexe ") - return - } - val fileName = args[0] - val column = args[1].toInt() - val count = args[2].toInt() + val argParser = ArgParser("csvparser") + val fileName by argParser.argument(ArgType.String, description = "CSV file") + val column by argParser.option(ArgType.Int, description = "Column to parse").required() + val count by argParser.option(ArgType.Int, description = "Count of lines to parse").required() + argParser.parse(args) val file = fopen(fileName, "r") if (file == null) { diff --git a/samples/tetris/build.gradle.kts b/samples/tetris/build.gradle.kts index 416c1160fb3..04b60f8b1f7 100644 --- a/samples/tetris/build.gradle.kts +++ b/samples/tetris/build.gradle.kts @@ -69,6 +69,8 @@ kotlin { } } } + + compilations["main"].kotlinOptions.freeCompilerArgs = listOf("-l", "kotlinx-cli") } } diff --git a/samples/tetris/src/tetrisMain/kotlin/main.kt b/samples/tetris/src/tetrisMain/kotlin/main.kt index e81bdb4fa8b..6548dcb6296 100644 --- a/samples/tetris/src/tetrisMain/kotlin/main.kt +++ b/samples/tetris/src/tetrisMain/kotlin/main.kt @@ -5,25 +5,17 @@ package sample.tetris +import kotlinx.cli.* + fun main(args: Array) { - var startLevel = Config.startLevel - var width = Config.width - var height = Config.height - when (args.size) { - 1 -> startLevel = args[0].toInt() - 2 -> { - width = args[0].toInt() - height = args[1].toInt() - } - 3 -> { - width = args[0].toInt() - height = args[1].toInt() - startLevel = args[2].toInt() - } - } + val argParser = ArgParser("tetris", useDefaultHelpShortName = false) + val level by argParser.option(ArgType.Int, shortName = "l", description = "Game level").default(Config.startLevel) + val width by argParser.option(ArgType.Int, shortName = "w", description = "Width of the game field").default(Config.width) + val height by argParser.option(ArgType.Int, shortName = "h", description = "Height of the game field").default(Config.height) + argParser.parse(args) val visualizer = SDL_Visualizer(width, height) val game = Game(width, height, visualizer, visualizer) - game.startNewGame(startLevel) + game.startNewGame(level) return } \ No newline at end of file diff --git a/samples/videoplayer/build.gradle.kts b/samples/videoplayer/build.gradle.kts index 0ec31dd14fe..8f8df6a6995 100644 --- a/samples/videoplayer/build.gradle.kts +++ b/samples/videoplayer/build.gradle.kts @@ -45,6 +45,8 @@ kotlin { } } } + + compilations["main"].kotlinOptions.freeCompilerArgs = listOf("-l", "kotlinx-cli") } // Enable experimental stdlib API used by the sample. diff --git a/samples/videoplayer/src/videoPlayerMain/kotlin/VideoPlayer.kt b/samples/videoplayer/src/videoPlayerMain/kotlin/VideoPlayer.kt index 4ec998a9ff3..18ae5585d05 100644 --- a/samples/videoplayer/src/videoPlayerMain/kotlin/VideoPlayer.kt +++ b/samples/videoplayer/src/videoPlayerMain/kotlin/VideoPlayer.kt @@ -9,6 +9,7 @@ import ffmpeg.* import kotlin.system.* import kotlinx.cinterop.* import platform.posix.* +import kotlinx.cli.* enum class State { PLAYING, @@ -156,16 +157,25 @@ class VideoPlayer(val requestedSize: Dimensions?) : DisposableContainer() { } fun main(args: Array) { - if (args.isEmpty()) { - println("Usage: videoplayer.kexe [ | 'video' | 'audio' | 'both']") - exitProcess(1) - } + val argParser = ArgParser("videoplayer") + val mode by argParser.option( + ArgType.Choice(listOf("video", "audio", "both")), shortName = "m", description = "Play mode") + .default("both") + val size by argParser.option(ArgType.Int, shortName = "s", description = "Required size of videoplayer window") + .delimiter(",") + val fileName by argParser.argument(ArgType.String, description = "File to play") + argParser.parse(args) + av_register_all() - val mode = if (args.size == 2) PlayMode.valueOf(args[1].toUpperCase()) else PlayMode.BOTH - val requestedSize = if (args.size < 3) null else Dimensions(args[1].toInt(), args[2].toInt()) + val requestedSize = if (size.size != 2) { + if (size.isNotEmpty()) + println("Size value should include width and height separated with ','.") + null + } else + Dimensions(size[0], size[1]) val player = VideoPlayer(requestedSize) try { - player.playFile(args[0], mode) + player.playFile(fileName, PlayMode.valueOf(mode.toUpperCase())) } finally { player.dispose() }