Skip to content

Commit d82a18f

Browse files
authored
Merge pull request #1908 from satra/fix/clirun
MRG: allow more support for cli
2 parents f66a5a6 + 901cf05 commit d82a18f

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

nipype/scripts/utils.py

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
Utilities for the CLI functions.
44
"""
55
from __future__ import print_function, division, unicode_literals, absolute_import
6-
import re
76

7+
from builtins import bytes, str
8+
9+
import re
810
import click
11+
import json
912

1013
from .instance import import_module
1114
from ..interfaces.base import InputMultiPath, traits
@@ -16,7 +19,6 @@
1619
UNKNOWN_OPTIONS = dict(allow_extra_args=True,
1720
ignore_unknown_options=True)
1821

19-
2022
# specification of existing ParamTypes
2123
ExistingDirPath = click.Path(exists=True, file_okay=False, resolve_path=True)
2224
ExistingFilePath = click.Path(exists=True, dir_okay=False, resolve_path=True)
@@ -63,18 +65,62 @@ def add_args_options(arg_parser, interface):
6365
# Escape any % signs with a %
6466
desc = desc.replace('%', '%%')
6567
args = {}
68+
has_multiple_inner_traits = False
6669

6770
if spec.is_trait_type(traits.Bool):
6871
args["default"] = getattr(inputs, name)
6972
args["action"] = 'store_true'
7073

71-
if hasattr(spec, "mandatory") and spec.mandatory:
74+
print(name, spec.trait_type)
75+
# current support is for simple trait types
76+
if not spec.inner_traits:
77+
if not spec.is_trait_type(traits.TraitCompound):
78+
trait_type = type(spec.trait_type.default_value)
79+
if trait_type in (bytes, str, int, float):
80+
if trait_type == bytes:
81+
trait_type = str
82+
args["type"] = trait_type
83+
elif len(spec.inner_traits) == 1:
84+
trait_type = type(spec.inner_traits[0].trait_type.default_value)
85+
if trait_type == bytes:
86+
trait_type = str
87+
if trait_type in (bytes, bool, str, int, float):
88+
args["type"] = trait_type
89+
else:
90+
if len(spec.inner_traits) > 1:
91+
if not spec.is_trait_type(traits.Dict):
92+
has_multiple_inner_traits = True
93+
94+
if getattr(spec, "mandatory", False):
7295
if spec.is_trait_type(InputMultiPath):
7396
args["nargs"] = "+"
97+
elif spec.is_trait_type(traits.List):
98+
if (spec.trait_type.minlen == spec.trait_type.maxlen) and \
99+
spec.trait_type.maxlen:
100+
args["nargs"] = spec.trait_type.maxlen
101+
else:
102+
args["nargs"] = "+"
103+
elif spec.is_trait_type(traits.Dict):
104+
args["type"] = json.loads
105+
106+
if has_multiple_inner_traits:
107+
raise NotImplementedError(
108+
('This interface cannot be used. via the'
109+
' command line as multiple inner traits'
110+
' are currently not supported for mandatory'
111+
' argument: {}.'.format(name)))
74112
arg_parser.add_argument(name, help=desc, **args)
75113
else:
76114
if spec.is_trait_type(InputMultiPath):
77115
args["nargs"] = "*"
78-
arg_parser.add_argument("--%s" % name, dest=name,
79-
help=desc, **args)
116+
elif spec.is_trait_type(traits.List):
117+
if (spec.trait_type.minlen == spec.trait_type.maxlen) and \
118+
spec.trait_type.maxlen:
119+
args["nargs"] = spec.trait_type.maxlen
120+
else:
121+
args["nargs"] = "*"
122+
if not has_multiple_inner_traits:
123+
arg_parser.add_argument("--%s" % name, dest=name,
124+
help=desc, **args)
125+
80126
return arg_parser

nipype/utils/nipype_cmd.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,8 @@ def run_instance(interface, options):
5050
print("setting function inputs")
5151

5252
for input_name, _ in list(interface.inputs.items()):
53-
if getattr(options, input_name) != None:
53+
if getattr(options, input_name) is not None:
5454
value = getattr(options, input_name)
55-
if not isinstance(value, bool):
56-
# traits cannot cast from string to float or int
57-
try:
58-
value = float(value)
59-
except:
60-
pass
61-
# try to cast string input to boolean
62-
try:
63-
value = str2bool(value)
64-
except:
65-
pass
6655
try:
6756
setattr(interface.inputs, input_name,
6857
value)

0 commit comments

Comments
 (0)