33Utilities for the CLI functions.
44"""
55from __future__ import print_function , division , unicode_literals , absolute_import
6- import re
76
7+ from builtins import bytes , str
8+
9+ import re
810import click
11+ import json
912
1013from .instance import import_module
1114from ..interfaces .base import InputMultiPath , traits
1619UNKNOWN_OPTIONS = dict (allow_extra_args = True ,
1720 ignore_unknown_options = True )
1821
19-
2022# specification of existing ParamTypes
2123ExistingDirPath = click .Path (exists = True , file_okay = False , resolve_path = True )
2224ExistingFilePath = click .Path (exists = True , dir_okay = False , resolve_path = True )
@@ -63,22 +65,33 @@ 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
74+ print (name , spec .trait_type )
7175 # current support is for simple trait types
7276 if not spec .inner_traits :
73- trait_type = type (spec .trait_type .default_value )
74- if trait_type in (str , int , float ):
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
7582 args ["type" ] = trait_type
7683 elif len (spec .inner_traits ) == 1 :
7784 trait_type = type (spec .inner_traits [0 ].trait_type .default_value )
78- if trait_type in (bool , str , int , float ):
85+ if trait_type == bytes :
86+ trait_type = str
87+ if trait_type in (bytes , bool , str , int , float ):
7988 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
8093
81- if hasattr (spec , "mandatory" ) and spec . mandatory :
94+ if getattr (spec , "mandatory" , False ) :
8295 if spec .is_trait_type (InputMultiPath ):
8396 args ["nargs" ] = "+"
8497 elif spec .is_trait_type (traits .List ):
@@ -87,6 +100,15 @@ def add_args_options(arg_parser, interface):
87100 args ["nargs" ] = spec .trait_type .maxlen
88101 else :
89102 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 )))
90112 arg_parser .add_argument (name , help = desc , ** args )
91113 else :
92114 if spec .is_trait_type (InputMultiPath ):
@@ -97,6 +119,8 @@ def add_args_options(arg_parser, interface):
97119 args ["nargs" ] = spec .trait_type .maxlen
98120 else :
99121 args ["nargs" ] = "*"
100- arg_parser .add_argument ("--%s" % name , dest = name ,
101- help = desc , ** args )
122+ if not has_multiple_inner_traits :
123+ arg_parser .add_argument ("--%s" % name , dest = name ,
124+ help = desc , ** args )
125+
102126 return arg_parser
0 commit comments