Skip to content

Commit a33b643

Browse files
committed
Merge pull request nipy#1213 from chrisfilo/fix/cmd_bool
added support for boolean flags in nipype_cmd
2 parents fcd493e + 13edf84 commit a33b643

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

nipype/utils/nipype_cmd.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import argparse
33
import inspect
44
import sys
5-
from nipype.interfaces.base import Interface, InputMultiPath
5+
from nipype.interfaces.base import Interface, InputMultiPath, traits
66
from nipype.utils.misc import str2bool
77

88
def listClasses(module=None):
@@ -24,6 +24,9 @@ def add_options(parser=None, module=None, function=None):
2424
for name, spec in sorted(interface.inputs.traits(transient=None).items()):
2525
desc = "\n".join(interface._get_trait_desc(inputs, name, spec))[len(name)+2:]
2626
args = {}
27+
28+
if spec.is_trait_type(traits.Bool):
29+
args["action"] = 'store_true'
2730

2831
if hasattr(spec, "mandatory") and spec.mandatory:
2932
if spec.is_trait_type(InputMultiPath):
@@ -43,16 +46,17 @@ def run_instance(interface, options):
4346
for input_name, _ in interface.inputs.items():
4447
if getattr(options, input_name) != None:
4548
value = getattr(options, input_name)
46-
#traits cannot cast from string to float or int
47-
try:
48-
value = float(value)
49-
except:
50-
pass
51-
#try to cast string input to boolean
52-
try:
53-
value = str2bool(value)
54-
except:
55-
pass
49+
if not isinstance(value, bool):
50+
#traits cannot cast from string to float or int
51+
try:
52+
value = float(value)
53+
except:
54+
pass
55+
#try to cast string input to boolean
56+
try:
57+
value = str2bool(value)
58+
except:
59+
pass
5660
try:
5761
setattr(interface.inputs, input_name,
5862
value)

nipype/utils/tests/test_cmd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_run_4d_realign_without_arguments(self):
8888
self.assertEqual(stderr.getvalue(),
8989
"""usage: nipype_cmd nipype.interfaces.nipy FmriRealign4d [-h]
9090
[--between_loops [BETWEEN_LOOPS [BETWEEN_LOOPS ...]]]
91-
[--ignore_exception IGNORE_EXCEPTION]
91+
[--ignore_exception]
9292
[--loops [LOOPS [LOOPS ...]]]
9393
[--slice_order SLICE_ORDER]
9494
[--speedup [SPEEDUP [SPEEDUP ...]]]
@@ -113,4 +113,4 @@ def test_run_4d_realign_help(self):
113113
self.assertTrue("Run FmriRealign4d" in stdout.getvalue())
114114

115115
if __name__ == '__main__':
116-
unittest.main()
116+
unittest.main()

0 commit comments

Comments
 (0)