Skip to content

Commit cb1f81b

Browse files
committed
finished docopt proxy impl
1 parent f98f4a5 commit cb1f81b

File tree

5 files changed

+49
-30
lines changed

5 files changed

+49
-30
lines changed

src/main/kotlin/kscript/DocOpt.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@ import java.io.File
1212
/** Simple Kotlin facade for org.docopt.Docopt.Docopt(java.lang.String) .*/
1313
class DocOpt(args: Array<String>, val usage: String) {
1414

15-
val myDO by lazy {
16-
Docopt(usage).parse(args.toList()).map {
17-
it.key.removePrefix("--").replace("[<>]".toRegex(), "") to it.value?.toString()
15+
val docopt = Docopt(usage)
16+
17+
private val myDO by lazy {
18+
docopt.parse(args.toList()).map {
19+
it.key.removePrefix("--").replace("[<>]".toRegex(), "") to it.value
1820
}.toMap()
1921
}
2022

21-
fun getString(key: String) = myDO[key]!!
22-
fun getStrings(key: String) = myDO[key]!!
23+
fun getString(key: String) = myDO[key]!!.toString()
24+
fun getStrings(key: String) = (myDO[key]!! as List<*>).map { it as String }
25+
26+
fun getFile(key: String) = File(getString(key))
27+
fun getFiles(key: String) = getStrings(key).map { File(it) }
28+
2329

24-
fun getInt(key: String) = myDO[key]!!.toInt()
30+
fun getInt(key: String) = myDO[key]!!.toString().toInt()
2531

26-
fun getNumber(key: String) = myDO[key]!!.toFloat()
32+
fun getNumber(key: String) = myDO[key]!!.toString().toFloat()
2733

28-
fun getBoolean(key: String) = myDO[key]!!.toBoolean()
34+
fun getBoolean(key: String) = myDO[key]!!.toString().toBoolean()
2935
}

src/main/kotlin/kscript/StreamUtil.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package kscript
33
import java.io.BufferedReader
44
import java.io.File
55
import java.io.FileReader
6-
import kotlin.system.exitProcess
76

87

98
// for top-level vs member extensions see https://kotlinlang.org/docs/reference/extensions.html#scope-of-extensions
@@ -20,18 +19,16 @@ fun argLines(arg: String, stdinNames: List<String> = listOf("-", "stdin")): Sequ
2019

2120
stopIfNot(inputFile.canRead()) { "Can not read from '${arg}'" }
2221

23-
// not we don't close the buffer with this approach
22+
// todo we don't close the buffer with this approach
2423
// BufferedReader(FileReader(inputFile )).use { return it }
2524
return BufferedReader(FileReader(inputFile)).lineSequence()
2625
}
2726

27+
fun mapLines(arg: String, stdinNames: List<String> = listOf("-", "stdin"), trafo: (String) -> String) =
28+
argLines(arg, stdinNames).map { trafo(it) }.print()
2829

29-
30-
fun Sequence<String>.print() = forEach { println(it) }
30+
fun filterLines(arg: String, stdinNames: List<String> = listOf("-", "stdin"), trafo: (String) -> Boolean) =
31+
argLines(arg, stdinNames).filter { trafo(it) }.print()
3132

3233

33-
fun processStdin(trafo: (String) -> String) {
34-
generateSequence() { readLine() }.map {
35-
println(trafo(it))
36-
}
37-
}
34+
fun Sequence<String>.print() = forEach { println(it) }

src/main/kotlin/kscript/Support.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import kotlin.system.exitProcess
66
* @author Holger Brandl
77
*/
88

9-
10-
fun foo() = "1"
11-
12-
public inline fun stopIfNot(value: Boolean, lazyMessage: () -> Any) {
9+
/** Similar to require but without a stacktrace. */
10+
inline fun stopIfNot(value: Boolean, lazyMessage: () -> Any) {
1311
if (!value) {
1412
System.err.println("[ERROR] " + lazyMessage().toString())
1513
exitProcess(1)

src/main/kotlin/kscript/experimental/Incubator.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ fun <T> File.mapLines(trafo: (String) -> T) {
1818
fun String.processLines(trafo: (String) -> String) {
1919
split("\n").map { println(trafo(it)) }
2020
}
21+
22+
23+
fun processStdin(trafo: (String) -> String) {
24+
generateSequence() { readLine() }.map {
25+
println(trafo(it))
26+
}
27+
}

src/test/kotlin/kscript/test/DocOptTest.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kscript.test
33
import io.kotlintest.matchers.shouldBe
44
import io.kotlintest.specs.StringSpec
55
import kscript.DocOpt
6+
import java.io.File
67

78
/**
89
* @author Holger Brandl
@@ -11,27 +12,37 @@ import kscript.DocOpt
1112

1213
class DocOptTest : StringSpec() { init {
1314

14-
val args = "-n 7 --pc-only --gtf my.gtf a.fastq b.fastq".split(" ").toTypedArray()
1515

1616
val usage = """
1717
Use star to align fastq files against a genome
1818
Usage: star_align.kts [options] <igenome> <fastq_files>...
1919
2020
Options:
21-
--gtf <gtfFile> Custom gtf file instead of igenome bundled copy
22-
--pc-only Use protein coding genes only for mapping and quantification
23-
-n --num-fragments Fragment count used for processing [default: 5]
21+
--gtf <gtfFile> Custom gtf file instead of igenome bundled copy
22+
--pc-only Use protein coding genes only for mapping and quantification
23+
-n --num-fragments <num_frags> Fragment count used for processing [default: 5]
2424
"""
2525

26+
val args = "-n 7 --pc-only --gtf my.gtf genome.fasta a.fastq b.fastq".split(" ").toTypedArray()
27+
28+
2629
val docopt = DocOpt(args, usage)
2730

31+
32+
// optional arg
2833
docopt.getString("gtf") shouldBe "my.gtf"
29-
docopt.getStrings("fastq_files") shouldBe arrayOf("a.fastq", "b.fastq")
34+
docopt.getFile("gtf") shouldBe File("my.gtf")
35+
36+
// mandatory arg
37+
docopt.getString("igenome") shouldBe "genome.fasta"
38+
docopt.getFile("igenome") shouldBe File("genome.fasta")
39+
40+
docopt.getStrings("fastq_files") shouldBe listOf("a.fastq", "b.fastq")
41+
docopt.getFiles("fastq_files") shouldBe listOf(File("a.fastq"), File("b.fastq"))
42+
3043

31-
docopt.getInt("fastq_files")
32-
docopt.getNumber("num-fragments") shouldBe 7
33-
docopt.getNumber("num-fragments") shouldBe 7
34-
docopt.getNumber("-n")
44+
docopt.getInt("num-fragments") shouldBe 7
45+
docopt.getNumber("num-fragments").toString() shouldBe "7.0"
3546
docopt.getBoolean("pc-only") shouldBe true
3647
}
3748
}

0 commit comments

Comments
 (0)