3
3
Utilities for the CLI functions.
4
4
"""
5
5
from __future__ import print_function , division , unicode_literals , absolute_import
6
- import re
7
6
7
+ from builtins import bytes , str
8
+
9
+ import re
8
10
import click
11
+ import json
9
12
10
13
from .instance import import_module
11
14
from ..interfaces .base import InputMultiPath , traits
16
19
UNKNOWN_OPTIONS = dict (allow_extra_args = True ,
17
20
ignore_unknown_options = True )
18
21
19
-
20
22
# specification of existing ParamTypes
21
23
ExistingDirPath = click .Path (exists = True , file_okay = False , resolve_path = True )
22
24
ExistingFilePath = click .Path (exists = True , dir_okay = False , resolve_path = True )
@@ -63,22 +65,33 @@ def add_args_options(arg_parser, interface):
63
65
# Escape any % signs with a %
64
66
desc = desc .replace ('%' , '%%' )
65
67
args = {}
68
+ has_multiple_inner_traits = False
66
69
67
70
if spec .is_trait_type (traits .Bool ):
68
71
args ["default" ] = getattr (inputs , name )
69
72
args ["action" ] = 'store_true'
70
73
74
+ print (name , spec .trait_type )
71
75
# current support is for simple trait types
72
76
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
75
82
args ["type" ] = trait_type
76
83
elif len (spec .inner_traits ) == 1 :
77
84
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 ):
79
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
80
93
81
- if hasattr (spec , "mandatory" ) and spec . mandatory :
94
+ if getattr (spec , "mandatory" , False ) :
82
95
if spec .is_trait_type (InputMultiPath ):
83
96
args ["nargs" ] = "+"
84
97
elif spec .is_trait_type (traits .List ):
@@ -87,6 +100,15 @@ def add_args_options(arg_parser, interface):
87
100
args ["nargs" ] = spec .trait_type .maxlen
88
101
else :
89
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 )))
90
112
arg_parser .add_argument (name , help = desc , ** args )
91
113
else :
92
114
if spec .is_trait_type (InputMultiPath ):
@@ -97,6 +119,8 @@ def add_args_options(arg_parser, interface):
97
119
args ["nargs" ] = spec .trait_type .maxlen
98
120
else :
99
121
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
+
102
126
return arg_parser
0 commit comments