Skip to content

Commit 4cf2a1b

Browse files
authored
Allow command options to have nullable types and default values (#1444)
1 parent 3ef065b commit 4cf2a1b

File tree

4 files changed

+2
-32
lines changed

4 files changed

+2
-32
lines changed

docs/modules/release-notes/pages/0.31.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ To learn more about this feature, consult https://github.com/apple/pkl-evolution
9999
[[cli-framework]]
100100
=== CLI Framework
101101

102-
Pkl 0.31 introduces a new framework for implementing CLI tools in Pkl (pr:https://github.com/apple/pkl/pull/1367[], pr:https://github.com/apple/pkl/pull/1431[], pr:https://github.com/apple/pkl/pull/1432[], pr:https://github.com/apple/pkl/pull/1436[], pr:https://github.com/apple/pkl/pull/1440[]).
102+
Pkl 0.31 introduces a new framework for implementing CLI tools in Pkl (pr:https://github.com/apple/pkl/pull/1367[], pr:https://github.com/apple/pkl/pull/1431[], pr:https://github.com/apple/pkl/pull/1432[], pr:https://github.com/apple/pkl/pull/1436[], pr:https://github.com/apple/pkl/pull/1440[], pr:https://github.com/apple/pkl/pull/1444[]).
103103

104104
The framework provides a way to build command line tools with user experience idioms that will be immediately familiar to users.
105105
CLI tools implemented in Pkl have largely the same capabilities as normal Pkl evaluation (i.e. writing to standard output and files), but this may be extended using xref:language-reference:index.adoc#external-readers[external readers].
@@ -339,6 +339,7 @@ The following bugs have been fixed.
339339
* A possible race condition involving symlinks could bypass `--root-dir` during module and resource reading (pr:https://github.com/apple/pkl/pull/1426[]).
340340
* `pkl format` produces internal stack traces when lexing fails (pr:https://github.com/apple/pkl/pull/1430[]).
341341
* `super` access expressions are parsed incorrectly inside the spread operator (pr:https://github.com/apple/pkl/pull/1364[]).
342+
* Modules and resources with `jar:file:` URIs were not properly sandboxed by `--root-dir` (pr:https://github.com/apple/pkl/pull/1442[]).
342343

343344
== Contributors [small]#🙏#
344345

pkl-core/src/main/java/org/pkl/core/runtime/CommandSpecParser.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,6 @@ public OptionBehavior resolve(ClassProperty prop, boolean requireExplicitDefault
513513
var typeNode = resolved.getFirst();
514514
isNullable = resolved.getSecond();
515515
defaultValue = CommandSpecParser.this.getDefaultValue(prop, requireExplicitDefault);
516-
if (isNullable && defaultValue != null) {
517-
throw exceptionBuilder()
518-
.evalError("commandOptionTypeNullableWithDefaultValue", prop.getName())
519-
.withSourceSection(prop.getHeaderSection())
520-
.build();
521-
}
522516

523517
resolve(prop, typeNode);
524518
return this;

pkl-core/src/main/resources/org/pkl/core/errorMessages.properties

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,11 +1098,6 @@ Found both `@Flag` and `@Argument` annotations for options property `{0}`.\n\
10981098
\n\
10991099
Only one option type may be specified.
11001100

1101-
commandOptionTypeNullableWithDefaultValue=\
1102-
Unexpected option property `{0}` with nullable type and default value.\n\
1103-
\n\
1104-
Options with default values must not be nullable.
1105-
11061101
commandOptionUnsupportedType=\
11071102
Command option property `{0}` has unsupported {1}type `{2}`.
11081103

pkl-core/src/test/kotlin/org/pkl/core/runtime/CommandSpecParserTest.kt

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -292,26 +292,6 @@ class CommandSpecParserTest {
292292
assertThat(exc.message).contains("No type annotation found for `foo` property.")
293293
}
294294

295-
@Test
296-
fun `nullable option with default not allowed`() {
297-
val moduleUri =
298-
writePklFile(
299-
"cmd.pkl",
300-
renderOptions +
301-
"""
302-
class Options {
303-
foo: String? = "bar"
304-
}
305-
"""
306-
.trimIndent(),
307-
)
308-
309-
val exc = assertThrows<PklException> { parse(moduleUri) }
310-
assertThat(exc.message).contains("foo: String? = \"bar\"")
311-
assertThat(exc.message)
312-
.contains("Unexpected option property `foo` with nullable type and default value")
313-
}
314-
315295
@Test
316296
fun `option with union type containing non-string-literals`() {
317297
val moduleUri =

0 commit comments

Comments
 (0)