Skip to content

MRG: allow more support for cli #1908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions nipype/scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
Utilities for the CLI functions.
"""
from __future__ import print_function, division, unicode_literals, absolute_import
import re

from builtins import bytes, str

import re
import click
import json

from .instance import import_module
from ..interfaces.base import InputMultiPath, traits
Expand All @@ -16,7 +19,6 @@
UNKNOWN_OPTIONS = dict(allow_extra_args=True,
ignore_unknown_options=True)


# specification of existing ParamTypes
ExistingDirPath = click.Path(exists=True, file_okay=False, resolve_path=True)
ExistingFilePath = click.Path(exists=True, dir_okay=False, resolve_path=True)
Expand Down Expand Up @@ -63,18 +65,62 @@ def add_args_options(arg_parser, interface):
# Escape any % signs with a %
desc = desc.replace('%', '%%')
args = {}
has_multiple_inner_traits = False

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

if hasattr(spec, "mandatory") and spec.mandatory:
print(name, spec.trait_type)
# current support is for simple trait types
if not spec.inner_traits:
if not spec.is_trait_type(traits.TraitCompound):
trait_type = type(spec.trait_type.default_value)
if trait_type in (bytes, str, int, float):
if trait_type == bytes:
trait_type = str
args["type"] = trait_type
elif len(spec.inner_traits) == 1:
trait_type = type(spec.inner_traits[0].trait_type.default_value)
if trait_type == bytes:
trait_type = str
if trait_type in (bytes, bool, str, int, float):
args["type"] = trait_type
else:
if len(spec.inner_traits) > 1:
if not spec.is_trait_type(traits.Dict):
has_multiple_inner_traits = True

if getattr(spec, "mandatory", False):
if spec.is_trait_type(InputMultiPath):
args["nargs"] = "+"
elif spec.is_trait_type(traits.List):
if (spec.trait_type.minlen == spec.trait_type.maxlen) and \
spec.trait_type.maxlen:
args["nargs"] = spec.trait_type.maxlen
else:
args["nargs"] = "+"
elif spec.is_trait_type(traits.Dict):
args["type"] = json.loads

if has_multiple_inner_traits:
raise NotImplementedError(
('This interface cannot be used. via the'
' command line as multiple inner traits'
' are currently not supported for mandatory'
' argument: {}.'.format(name)))
arg_parser.add_argument(name, help=desc, **args)
else:
if spec.is_trait_type(InputMultiPath):
args["nargs"] = "*"
arg_parser.add_argument("--%s" % name, dest=name,
help=desc, **args)
elif spec.is_trait_type(traits.List):
if (spec.trait_type.minlen == spec.trait_type.maxlen) and \
spec.trait_type.maxlen:
args["nargs"] = spec.trait_type.maxlen
else:
args["nargs"] = "*"
if not has_multiple_inner_traits:
arg_parser.add_argument("--%s" % name, dest=name,
help=desc, **args)

return arg_parser
13 changes: 1 addition & 12 deletions nipype/utils/nipype_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,8 @@ def run_instance(interface, options):
print("setting function inputs")

for input_name, _ in list(interface.inputs.items()):
if getattr(options, input_name) != None:
if getattr(options, input_name) is not None:
value = getattr(options, input_name)
if not isinstance(value, bool):
# traits cannot cast from string to float or int
try:
value = float(value)
except:
pass
# try to cast string input to boolean
try:
value = str2bool(value)
except:
pass
try:
setattr(interface.inputs, input_name,
value)
Expand Down