Skip to content

Commit

Permalink
Modify Parser to support method chanining
Browse files Browse the repository at this point in the history
(close to builder pattern)
  • Loading branch information
jimschubert committed Dec 11, 2016
1 parent 0be35e2 commit f5e0320
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import us.jimschubert.kopper.*

fun main(args: Array<String>) {
val parser = Parser()
parser.name = "Kopper CLI"
parser.applicationDescription = "Kopper example application"
parser.setName("Kopper CLI")
parser.setApplicationDescription("Kopper example application")

parser.flag("q", listOf("quiet", "silent"), description = "Run silently")
parser.option("f", listOf("file"), description = "File name")
Expand Down
12 changes: 7 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ configurations.all {

allprojects {
apply plugin: 'idea'
repositories {
jcenter()
}
}

configure(subprojects.findAll {it.name != 'kopper.kt'}) {
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'maven'
apply plugin: 'org.jetbrains.dokka'
apply plugin: 'signing'
apply plugin: 'org.jetbrains.dokka'

tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }

compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}

repositories {
jcenter()
}
}

task wrapper(type: Wrapper) {
Expand Down
4 changes: 2 additions & 2 deletions kopper-cli/src/main/kotlin/us/jimschubert/kopper/cli/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import us.jimschubert.kopper.*

fun main(args: Array<String>) {
val parser = Parser()
parser.name = "Kopper CLI"
parser.applicationDescription = "Kopper example application"
parser.setName("Kopper CLI")
parser.setApplicationDescription("Kopper example application")

parser.flag("q", listOf("quiet", "silent"), description = "Run silently")
parser.option("f", listOf("file"), description = "File name")
Expand Down
31 changes: 26 additions & 5 deletions kopper/src/main/kotlin/us/jimschubert/kopper/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,48 @@ class Parser {
private var options: MutableList<Option<*>> = mutableListOf()
private var _args: MutableList<String> = mutableListOf()

var name: String? = null
var applicationDescription: String? = null
private var _name: String? = null
val name: String?
get() = _name

private var _applicationDescription: String? = null
val applicationDescription: String?
get() = _applicationDescription

val remainingArgs: List<String> get() = _args.toList()

fun option(shortOption: String,
longOption: List<String> = listOf(),
description: String? = null,
default: String? = null) {
default: String? = null): Parser {
options.add(StringOption(shortOption, longOption, description, default))

return this
}

fun setName(name: String) : Parser {
_name = name
return this
}

fun setApplicationDescription(description: String) : Parser {
_applicationDescription = description
return this
}

fun flag(shortOption: String,
longOption: List<String> = listOf(),
description: String? = null,
default: Boolean? = true) {
default: Boolean? = true): Parser {
options.add(BooleanOption(shortOption, longOption, description, default))

return this
}

fun <T> custom(option: Option<T>) {
fun <T> custom(option: Option<T>): Parser {
options.add(option)

return this
}

fun parse(args: Array<String>) {
Expand Down
12 changes: 5 additions & 7 deletions kopper/src/test/kotlin/us/jimschubert/kopper/ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ class ParserTest {
@BeforeMethod
fun before(){
parser = Parser()

parser.name = "Kopper"
parser.applicationDescription = "Kopper Tests"

parser.flag("q", listOf("quiet", "silent"), description = "Run silently")
parser.option("f", listOf("file"), description = "File name")
parser.flag("a", listOf("allowEmpty"))
.setName("Kopper")
.setApplicationDescription("Kopper Tests")
.flag("q", listOf("quiet", "silent"), description = "Run silently")
.option("f", listOf("file"), description = "File name")
.flag("a", listOf("allowEmpty"))
}

@Test
Expand Down

0 comments on commit f5e0320

Please sign in to comment.