Skip to content

Commit 775a0fb

Browse files
authored
Allow boolean params to implicitly default to false (#6764)
1 parent 63aae89 commit 775a0fb

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

docs/workflow.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ params {
4949
input: Path
5050
5151
// Whether to save intermediate files.
52-
save_intermeds: Boolean = false
52+
save_intermeds: Boolean
5353
}
5454
```
5555

@@ -69,7 +69,7 @@ As a best practice, parameters should only be referenced in the entry workflow o
6969

7070
The default value can be overridden by the command line, params file, or config file. Parameters from multiple sources are resolved in the order described in {ref}`cli-params`. Parameters specified on the command line are converted to the appropriate type based on the corresponding type annotation.
7171

72-
A parameter that doesn't specify a default value is a *required* parameter. If a required parameter is not given a value at runtime, the run will fail.
72+
A parameter that doesn't specify a default value is a *required* parameter. If a required parameter is not given a value at runtime, the run will fail. Boolean parameters that don't specify a default value default to `false`.
7373

7474
:::{versionadded} 26.04.0
7575
:::

modules/nextflow/src/main/groovy/nextflow/script/ParamsDsl.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class ParamsDsl {
4646
private Map<String,Param> declarations = [:]
4747

4848
void declare(String name, Class type, boolean optional, Object defaultValue = null) {
49+
if( defaultValue == null && type == Boolean )
50+
defaultValue = false
4951
declarations[name] = new Param(name, type, optional, defaultValue)
5052
}
5153

modules/nextflow/src/test/groovy/nextflow/script/ParamsDslTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ParamsDslTest extends Specification {
2626
def dsl = new ParamsDsl()
2727
dsl.declare('input', Path, false)
2828
dsl.declare('chunk_size', Integer, false, 1)
29-
dsl.declare('save_intermeds', Boolean, false, false)
29+
dsl.declare('save_intermeds', Boolean, false)
3030
dsl.apply(session)
3131
then:
3232
session.binding.getParams() == [input: FileHelper.asPath('./data'), chunk_size: 3, save_intermeds: false, outdir: 'results']
@@ -55,7 +55,7 @@ class ParamsDslTest extends Specification {
5555
when:
5656
def dsl = new ParamsDsl()
5757
dsl.declare('input', Path, false)
58-
dsl.declare('save_intermeds', Boolean, false, false)
58+
dsl.declare('save_intermeds', Boolean, false)
5959
dsl.apply(session)
6060
then:
6161
def e = thrown(ScriptRuntimeException)
@@ -72,7 +72,7 @@ class ParamsDslTest extends Specification {
7272
when:
7373
def dsl = new ParamsDsl()
7474
dsl.declare('input', Path, false)
75-
dsl.declare('save_intermeds', Boolean, false, false)
75+
dsl.declare('save_intermeds', Boolean, false)
7676
dsl.apply(session)
7777
then:
7878
def e = thrown(ScriptRuntimeException)
@@ -89,7 +89,7 @@ class ParamsDslTest extends Specification {
8989
when:
9090
def dsl = new ParamsDsl()
9191
dsl.declare('input', Path, false)
92-
dsl.declare('save_intermeds', Boolean, false, false)
92+
dsl.declare('save_intermeds', Boolean, false)
9393
dsl.apply(session)
9494
then:
9595
def e = thrown(ScriptRuntimeException)

tests/checks/params-dsl.nf/.checks

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ $NXF_RUN --input ./data > stdout
55

66
< stdout grep -F 'params.input = [./data]'
77
< stdout grep -F 'params.save_intermeds = false'
8+
< stdout grep -F 'params.method = auto'
89

910
echo
1011
echo "Test missing required param"
@@ -17,13 +18,23 @@ set -e
1718

1819
< stdout grep -F 'Parameter `input` is required'
1920

21+
echo
22+
echo "Test overwrite script param from command line"
23+
echo
24+
$NXF_RUN -c ../../params-dsl.config --input 'alpha,beta' --save_intermeds --method special > stdout
25+
26+
< stdout grep -F 'params.input = [alpha, beta]'
27+
< stdout grep -F 'params.save_intermeds = true'
28+
< stdout grep -F 'params.method = special'
29+
2030
echo
2131
echo "Test overwrite script param from config profile"
2232
echo
2333
$NXF_RUN -c ../../params-dsl.config -profile test > stdout
2434

2535
< stdout grep -F 'params.input = [alpha, beta, delta]'
2636
< stdout grep -F 'params.save_intermeds = true'
37+
< stdout grep -F 'params.method = special'
2738

2839
echo
2940
echo "Test invalid param"

tests/params-dsl.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ profiles {
55
test {
66
params.input = 'alpha,beta,delta'
77
params.save_intermeds = true
8+
params.method = 'special'
89
}
910
}

tests/params-dsl.nf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616
*/
1717

1818
params {
19-
// List of IDs.
19+
// Comma-separated list of IDs.
2020
input: String
2121

2222
// Whether to save intermediate outputs.
23-
save_intermeds: Boolean = false
23+
save_intermeds: Boolean
24+
25+
// Method to use for analyzing samples.
26+
method: String = 'auto'
2427
}
2528

2629
workflow {
2730
main:
2831
println "params.input = ${params.input.tokenize(',')}"
2932
println "params.save_intermeds = ${params.save_intermeds}"
33+
println "params.method = ${params.method}"
3034
}

0 commit comments

Comments
 (0)