Skip to content

Commit 1149c82

Browse files
committed
started kotlin docopt facade
1 parent ed9e472 commit 1149c82

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ repositories {
2626
dependencies {
2727
// compileOnly "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2828
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
29+
compile "com.offbytwo:docopt:0.6.0.20150202"
2930

3031
testCompile 'junit:junit:4.11'
3132
testCompile "io.kotlintest:kotlintest:2.0.1"

src/main/kotlin/kscript/ArgUtil.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/kotlin/kscript/DocOpt.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package kscript
2+
3+
import org.docopt.Docopt
4+
import java.io.File
5+
6+
/**
7+
* Helpers to build CLIs with kscript and docopt
8+
*
9+
* @author Holger Brandl
10+
*/
11+
12+
/** Simple Kotlin facade for org.docopt.Docopt.Docopt(java.lang.String) .*/
13+
class DocOpt(args: Array<String>, val usage: String) {
14+
15+
val myDO by lazy {
16+
Docopt(usage).parse(args.toList()).map {
17+
it.key.removePrefix("--").replace("[<>]".toRegex(), "") to it.value?.toString()
18+
}.toMap()
19+
}
20+
21+
fun getString(key: String) = myDO[key]!!
22+
fun getStrings(key: String) = myDO[key]!!
23+
24+
fun getInt(key: String) = myDO[key]!!.toInt()
25+
26+
fun getNumber(key: String) = myDO[key]!!.toFloat()
27+
28+
fun getBoolean(key: String) = myDO[key]!!.toBoolean()
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package kscript.test
2+
3+
import io.kotlintest.matchers.shouldBe
4+
import io.kotlintest.specs.StringSpec
5+
import kscript.DocOpt
6+
7+
/**
8+
* @author Holger Brandl
9+
*/
10+
11+
12+
class DocOptTest : StringSpec() { init {
13+
14+
val args = "-n 7 --pc-only --gtf my.gtf a.fastq b.fastq".split(" ").toTypedArray()
15+
16+
val usage = """
17+
Use star to align fastq files against a genome
18+
Usage: star_align.kts [options] <igenome> <fastq_files>...
19+
20+
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]
24+
"""
25+
26+
val docopt = DocOpt(args, usage)
27+
28+
docopt.getString("gtf") shouldBe "my.gtf"
29+
docopt.getStrings("fastq_files") shouldBe arrayOf("a.fastq", "b.fastq")
30+
31+
docopt.getInt("fastq_files")
32+
docopt.getNumber("num-fragments") shouldBe 7
33+
docopt.getNumber("num-fragments") shouldBe 7
34+
docopt.getNumber("-n")
35+
docopt.getBoolean("pc-only") shouldBe true
36+
}
37+
}

0 commit comments

Comments
 (0)