Skip to content

Commit b62d41d

Browse files
authored
improve message for invalid uint / ulong (#624)
If a negative integer value is passed for a uint / ulong option, the current error message is not very helpful.
1 parent 2ef5229 commit b62d41d

File tree

6 files changed

+9
-4
lines changed

6 files changed

+9
-4
lines changed

clikt/api/clikt.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ public abstract interface class com/github/ajalt/clikt/output/Localization {
708708
public abstract fun requiredMutexOption (Ljava/lang/String;)Ljava/lang/String;
709709
public abstract fun stringMetavar ()Ljava/lang/String;
710710
public abstract fun switchOptionEnvvar ()Ljava/lang/String;
711+
public abstract fun uintConversionError (Ljava/lang/String;)Ljava/lang/String;
711712
public abstract fun unclosedQuote ()Ljava/lang/String;
712713
public abstract fun usageError ()Ljava/lang/String;
713714
public abstract fun usageTitle ()Ljava/lang/String;
@@ -768,6 +769,7 @@ public final class com/github/ajalt/clikt/output/Localization$DefaultImpls {
768769
public static fun requiredMutexOption (Lcom/github/ajalt/clikt/output/Localization;Ljava/lang/String;)Ljava/lang/String;
769770
public static fun stringMetavar (Lcom/github/ajalt/clikt/output/Localization;)Ljava/lang/String;
770771
public static fun switchOptionEnvvar (Lcom/github/ajalt/clikt/output/Localization;)Ljava/lang/String;
772+
public static fun uintConversionError (Lcom/github/ajalt/clikt/output/Localization;Ljava/lang/String;)Ljava/lang/String;
771773
public static fun unclosedQuote (Lcom/github/ajalt/clikt/output/Localization;)Ljava/lang/String;
772774
public static fun usageError (Lcom/github/ajalt/clikt/output/Localization;)Ljava/lang/String;
773775
public static fun usageTitle (Lcom/github/ajalt/clikt/output/Localization;)Ljava/lang/String;

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/output/Localization.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ interface Localization {
137137
/** Invalid value for a parameter of type [Int] or [Long] */
138138
fun intConversionError(value: String) = "$value is not a valid integer"
139139

140+
/** Invalid value for a parameter of type [UInt] or [ULong] */
141+
fun uintConversionError(value: String) = "$value is not a valid unsigned integer"
142+
140143
/** Invalid value for a parameter of type [Boolean] */
141144
fun boolConversionError(value: String) = "$value is not a valid boolean"
142145

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parameters/types/uint.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.github.ajalt.clikt.parameters.transform.TransformContext
1010

1111

1212
private val conversion: TransformContext.(String) -> UInt =
13-
{ it.toUIntOrNull() ?: fail(context.localization.intConversionError(it)) }
13+
{ it.toUIntOrNull() ?: fail(context.localization.uintConversionError(it)) }
1414

1515
/** Convert the argument values to an `UInt` */
1616
fun RawArgument.uint(): ProcessedArgument<UInt, UInt> = convert(conversion = conversion)

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parameters/types/ulong.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.github.ajalt.clikt.parameters.transform.TransformContext
1010

1111

1212
private val conversion: TransformContext.(String) -> ULong =
13-
{ it.toULongOrNull() ?: fail(context.localization.intConversionError(it)) }
13+
{ it.toULongOrNull() ?: fail(context.localization.uintConversionError(it)) }
1414

1515
/** Convert the argument values to a `ULong` */
1616
fun RawArgument.ulong(): ProcessedArgument<ULong, ULong> = convert(conversion = conversion)

test/src/commonTest/kotlin/com/github/ajalt/clikt/parameters/types/UIntTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class UIntTest {
5555
}
5656

5757
shouldThrow<BadParameterValue> { C().parse("--foo bar") }
58-
.formattedMessage shouldBe "invalid value for --foo: bar is not a valid integer"
58+
.formattedMessage shouldBe "invalid value for --foo: bar is not a valid unsigned integer"
5959

6060
shouldThrow<NoSuchOption> { C().parse("-2") }
6161
shouldThrow<BadParameterValue> { C().parse("--foo=-1") }

test/src/commonTest/kotlin/com/github/ajalt/clikt/parameters/types/ULongTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ULongTest {
4040
}
4141

4242
shouldThrow<BadParameterValue> { C().parse("--foo bar") }
43-
.formattedMessage shouldBe "invalid value for --foo: bar is not a valid integer"
43+
.formattedMessage shouldBe "invalid value for --foo: bar is not a valid unsigned integer"
4444

4545
shouldThrow<NoSuchOption> { C().parse("-2") }
4646
shouldThrow<BadParameterValue> { C().parse("--foo=-1") }

0 commit comments

Comments
 (0)