@@ -55,7 +55,10 @@ def visit_AnnAssign(self, node):
5555 try :
5656 if (isinstance (node .annotation , ast .Name ) and node .annotation .id in self .input_type_mapper ) or \
5757 (isinstance (node .annotation , ast .Str ) and node .annotation .s in self .input_type_mapper ):
58- mapper = self .input_type_mapper [node .annotation .id ]
58+ if hasattr (node .annotation , 'id' ):
59+ mapper = self .input_type_mapper [node .annotation .id ]
60+ else :
61+ mapper = self .input_type_mapper [node .annotation .s ]
5962 self .extracted_nodes .append (
6063 (node , mapper [0 ], mapper [1 ], True , True , False )
6164 )
@@ -152,6 +155,7 @@ def from_jupyter_notebook_node(cls, node: NotebookNode) -> 'AnnotatedIPython2CWL
152155
153156 @classmethod
154157 def _wrap_script_to_method (cls , tree , variables ) -> str :
158+ add_args = cls .__get_add_arguments__ ([v for v in variables if v .is_input ])
155159 main_template_code = os .linesep .join ([
156160 f"def main({ ',' .join ([v .name for v in variables if v .is_input ])} ):" ,
157161 "\t pass" ,
@@ -160,19 +164,31 @@ def _wrap_script_to_method(cls, tree, variables) -> str:
160164 "import argparse" ,
161165 'import pathlib' ,
162166 "parser = argparse.ArgumentParser()" ,
163- * [f'parser.add_argument("--{ variable .name } ", '
164- f'type={ variable .argparse_typeof } , '
165- f'required={ variable .required } )'
166- for variable in variables ],
167+ * add_args ,
167168 "args = parser.parse_args()" ,
168- f"main({ ',' .join ([f'{ v .name } =args.{ v .name } ' for v in variables if v .is_input ])} )"
169+ f"main({ ',' .join ([f'{ v .name } =args.{ v .name } ' for v in variables if v .is_input ])} )"
169170 ]],
170171 ])
171172 main_function = ast .parse (main_template_code )
172173 [node for node in main_function .body if isinstance (node , ast .FunctionDef ) and node .name == 'main' ][0 ] \
173174 .body = tree .body
174175 return astor .to_source (main_function )
175176
177+ @classmethod
178+ def __get_add_arguments__ (cls , variables ):
179+ args = []
180+ for variable in variables :
181+ is_array = variable .cwl_typeof .endswith ('[]' )
182+ arg : str = f'parser.add_argument("--{ variable .name } ", '
183+ arg += f'type={ variable .argparse_typeof } , '
184+ arg += f'required={ variable .required } , '
185+ if is_array :
186+ arg += f'nargs="+", '
187+ arg = arg .strip ()
188+ arg += ')'
189+ args .append (arg )
190+ return args
191+
176192 def cwl_command_line_tool (self , docker_image_id : str = 'jn2cwl:latest' ) -> Dict :
177193 """
178194 Creates the description of the CWL Command Line Tool.
0 commit comments