Rewrote some samples using kotlinx.cli (#3351)

This commit is contained in:
LepilkinaElena
2019-09-17 15:28:49 +03:00
committed by GitHub
parent ba37e69e1b
commit 3b81f12020
8 changed files with 40 additions and 33 deletions
@@ -123,7 +123,8 @@ internal abstract class AbstractArgumentSingleValue<T: Any>(descriptor: Descript
internal class ArgumentSingleValue<T: Any>(descriptor: Descriptor<T, T>): AbstractArgumentSingleValue<T>(descriptor),
ArgumentValueDelegate<T> {
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)
}
+1 -1
View File
@@ -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.
+2 -1
View File
@@ -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")
}
}
}
@@ -7,6 +7,7 @@ package sample.csvparser
import kotlinx.cinterop.*
import platform.posix.*
import kotlinx.cli.*
fun parseLine(line: String, separator: Char) : List<String> {
val result = mutableListOf<String>()
@@ -30,13 +31,11 @@ fun parseLine(line: String, separator: Char) : List<String> {
}
fun main(args: Array<String>) {
if (args.size != 3) {
println("Usage: csvparser.kexe <file.csv> <column> <count>")
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) {
+2
View File
@@ -69,6 +69,8 @@ kotlin {
}
}
}
compilations["main"].kotlinOptions.freeCompilerArgs = listOf("-l", "kotlinx-cli")
}
}
+8 -16
View File
@@ -5,25 +5,17 @@
package sample.tetris
import kotlinx.cli.*
fun main(args: Array<String>) {
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
}
+2
View File
@@ -45,6 +45,8 @@ kotlin {
}
}
}
compilations["main"].kotlinOptions.freeCompilerArgs = listOf("-l", "kotlinx-cli")
}
// Enable experimental stdlib API used by the sample.
@@ -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<String>) {
if (args.isEmpty()) {
println("Usage: videoplayer.kexe <file.ext> [<width> <height> | '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()
}