Skip to content

Commit 2094c26

Browse files
committed
Fixed issue #18
1 parent d5f3b8c commit 2094c26

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## Unreleased
9+
10+
### Fixed
11+
12+
- [Issue #18](https://github.com/xenomachina/kotlin-argparser/issues/18)
13+
814
## 2.0.2 - 2017-06-12
915

1016
### Fixed

src/main/kotlin/com/xenomachina/argparser/ArgParser.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ class ArgParser(args: Array<out String>,
371371
}
372372

373373
internal abstract fun registerLeaf(root: Delegate<*>)
374+
375+
abstract internal val hasValidators: Boolean
374376
}
375377

376378
/**

src/main/kotlin/com/xenomachina/argparser/Default.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,16 @@ fun <T> ArgParser.DelegateProvider<T>.default(newDefault: T): ArgParser.Delegate
3535
* @param newDefault the default value for the resulting [ArgParser.Delegate]
3636
*/
3737
fun <T> ArgParser.Delegate<T>.default(defaultValue: T): ArgParser.Delegate<T> {
38+
if (hasValidators) {
39+
throw IllegalStateException("Cannot add default after adding validators")
40+
}
3841
val inner = this
3942

4043
return object : ArgParser.Delegate<T>() {
44+
45+
override val hasValidators: Boolean
46+
get() = inner.hasValidators
47+
4148
override fun toHelpFormatterValue(): HelpFormatter.Value = inner.toHelpFormatterValue().copy(isRequired = false)
4249

4350
override fun validate() {
@@ -63,7 +70,7 @@ fun <T> ArgParser.Delegate<T>.default(defaultValue: T): ArgParser.Delegate<T> {
6370
get() = inner.help
6471

6572
override fun addValidator(validator: ArgParser.Delegate<T>.() -> Unit): ArgParser.Delegate<T> =
66-
inner.addValidator() { validator(inner) }
73+
apply { inner.addValidator { validator(this@apply) } }
6774

6875
override fun registerLeaf(root: ArgParser.Delegate<*>) {
6976
inner.registerLeaf(root)

src/main/kotlin/com/xenomachina/argparser/ParsingDelegate.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ internal abstract class ParsingDelegate<T>(
3131
validators.add(validator)
3232
}
3333

34+
override val hasValidators: Boolean
35+
get() = validators.isNotEmpty()
36+
3437
override val value: T
3538
get() {
3639
parser.force()

src/main/kotlin/com/xenomachina/argparser/WrappingDelegate.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ internal class WrappingDelegate<U, W>(
4747
override fun addValidator(validator: ArgParser.Delegate<W>.() -> Unit): ArgParser.Delegate<W> =
4848
apply { inner.addValidator { validator(this@WrappingDelegate) } }
4949

50+
override val hasValidators: Boolean
51+
get() = inner.hasValidators
52+
5053
override fun registerLeaf(root: ArgParser.Delegate<*>) {
5154
inner.registerLeaf(root)
5255
}

src/test/kotlin/com/xenomachina/argparser/ArgParserTest.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.xenomachina.common.orElse
2323
import io.kotlintest.matchers.beOfType
2424
import io.kotlintest.matchers.should
2525
import io.kotlintest.matchers.shouldBe
26+
import io.kotlintest.matchers.shouldEqual
2627
import io.kotlintest.matchers.shouldThrow
2728
import io.kotlintest.specs.FunSpec
2829
import java.io.File
@@ -1467,4 +1468,33 @@ optional arguments:
14671468
14681469
--baz QUUX test help message""".trim()
14691470
}
1470-
})
1471+
})
1472+
1473+
class Issue18Test_ValidatorThenDefault : Test({
1474+
class Args(parser: ArgParser) {
1475+
val x by parser.storing(
1476+
"-x",
1477+
help = TEST_HELP,
1478+
transform = String::toInt
1479+
).addValidator {
1480+
value shouldEqual 0
1481+
}.default(0)
1482+
}
1483+
shouldThrow<IllegalStateException> {
1484+
Args(parserOf())
1485+
}.message shouldEqual "Cannot add default after adding validators"
1486+
})
1487+
1488+
class Issue18Test_DefaultThenValidator : Test({
1489+
class Args(parser: ArgParser) {
1490+
val x by parser.storing(
1491+
"-x",
1492+
help = "",
1493+
transform = String::toInt
1494+
).default(0).addValidator {
1495+
value shouldEqual 0
1496+
}
1497+
}
1498+
val x = Args(parserOf()).x
1499+
x shouldEqual 0
1500+
})

0 commit comments

Comments
 (0)