Skip to content

Commit d759124

Browse files
committed
rf: use defaults + boolean optional action, add test
1 parent 2263b64 commit d759124

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

fmriprep/cli/parser.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""Parser."""
2424

2525
import sys
26+
from pathlib import Path
2627

2728
from .. import config
2829

@@ -32,9 +33,13 @@ def _build_parser(**kwargs):
3233
3334
``kwargs`` are passed to ``argparse.ArgumentParser`` (mainly useful for debugging).
3435
"""
35-
from argparse import Action, ArgumentDefaultsHelpFormatter, ArgumentParser
36+
from argparse import (
37+
Action,
38+
ArgumentDefaultsHelpFormatter,
39+
ArgumentParser,
40+
BooleanOptionalAction,
41+
)
3642
from functools import partial
37-
from pathlib import Path
3843

3944
from niworkflows.utils.spaces import OutputReferencesAction, Reference
4045
from packaging.version import Version
@@ -521,10 +526,12 @@ def _fallback_trt(value, parser):
521526
'(default is 91k, which equates to 2mm resolution)',
522527
)
523528
g_outputs.add_argument(
524-
'--no-msm',
525-
action='store_false',
529+
'--msm',
530+
action=BooleanOptionalAction,
531+
default=True,
526532
dest='run_msmsulc',
527-
help='Disable Multimodal Surface Matching surface registration.',
533+
help='Enable or disable Multimodal Surface Matching surface registration. '
534+
'To disable, use `--no-msm`.',
528535
)
529536

530537
g_confounds = parser.add_argument_group('Options relating to confounds')
@@ -634,10 +641,12 @@ def _fallback_trt(value, parser):
634641
'(default: OUTPUT_DIR/freesurfer)',
635642
)
636643
g_fs.add_argument(
637-
'--no-submm-recon',
638-
action='store_false',
644+
'--submm-recon',
645+
action=BooleanOptionalAction,
646+
default=True,
639647
dest='hires',
640-
help='Disable sub-millimeter (hires) reconstruction',
648+
help='Enable or disable sub-millimeter (hi-res) reconstruction. '
649+
'To disable, use `--no-submm-recon`.',
641650
)
642651
g_fs.add_argument(
643652
'--fs-no-reconall',

fmriprep/cli/tests/test_parser.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,19 @@ def test_derivatives(tmp_path):
270270
parser.parse_args(temp_args)
271271

272272
_reset_config()
273+
274+
275+
@pytest.mark.parametrize(
276+
('supp_args', 'opt', 'expected'),
277+
[
278+
(['--no-submm-recon'], 'hires', False),
279+
(['--submm-recon'], 'hires', True),
280+
([], 'hires', True),
281+
(['--no-msm'], 'run_msmsulc', False),
282+
],
283+
)
284+
def test_optional_booleans(tmp_path, supp_args, opt, expected):
285+
out_path = str(tmp_path / 'out')
286+
args = [str(tmp_path), out_path, 'participant'] + supp_args
287+
pargs = _build_parser().parse_args(args)
288+
assert getattr(pargs, opt) == expected

0 commit comments

Comments
 (0)