@@ -150,6 +150,14 @@ def format_data(cls, data):
150150 return AAZPromptInputOperation (prompt = cls ._schema ._blank , action_cls = cls )
151151 data = copy .deepcopy (cls ._schema ._blank )
152152
153+ if data is None :
154+ if cls ._schema ._nullable :
155+ return data
156+ raise AAZInvalidValueError ("field is not nullable" )
157+
158+ if cls ._schema .DataType == str and isinstance (data , (int , bool , float )):
159+ data = str (data ).lower () # convert to json raw values
160+
153161 if isinstance (data , str ):
154162 # transfer string into correct data
155163 if cls ._schema .enum :
@@ -159,12 +167,80 @@ def format_data(cls, data):
159167 if isinstance (data , cls ._schema .DataType ):
160168 return data
161169
170+ raise AAZInvalidValueError (f"{ cls ._schema .DataType } type value expected, got '{ data } '({ type (data )} )" )
171+
172+
173+ class AAZAnyTypeArgAction (AAZArgAction ):
174+
175+ @classmethod
176+ def setup_operations (cls , dest_ops , values , prefix_keys = None ):
177+ if prefix_keys is None :
178+ prefix_keys = []
179+ if values is None :
180+ data = AAZBlankArgValue # use blank data when values string is None
181+ else :
182+ if isinstance (values , list ):
183+ assert prefix_keys # the values will be input as an list when parse singular option of a list argument
184+ if len (values ) != 1 :
185+ raise AAZInvalidValueError (f"only support 1 value, got { len (values )} : { values } " )
186+ values = values [0 ]
187+
188+ if isinstance (values , str ) and len (values ) > 0 :
189+ try :
190+ data = cls .decode_str (values )
191+ except AAZShowHelp as aaz_help :
192+ aaz_help .schema = cls ._schema
193+ raise aaz_help
194+ else :
195+ data = values
196+ data = cls .format_data (data )
197+ dest_ops .add (data , * prefix_keys )
198+
199+ @classmethod
200+ def decode_str (cls , value ):
201+ from azure .cli .core .util import get_file_json , shell_safe_json_parse , get_file_yaml
202+
203+ # check if the value is a partial value
204+ key , _ , v = cls ._str_parser .split_partial_value (value )
205+ if key is not None :
206+ raise AAZInvalidValueError (f"AnyType args only support full value shorthand syntax, please don't use partial value shorthand syntax. If it's a simple string, please wrap it with single quotes." )
207+
208+ # read from file
209+ path = os .path .expanduser (value )
210+ if os .path .exists (path ):
211+ if path .endswith ('.yml' ) or path .endswith ('.yaml' ):
212+ # read from yaml file
213+ v = get_file_yaml (path )
214+ else :
215+ # read from json file
216+ v = get_file_json (path , preserve_order = True )
217+ else :
218+ try :
219+ v = cls ._str_parser (value )
220+ except AAZInvalidShorthandSyntaxError as shorthand_ex :
221+ try :
222+ v = shell_safe_json_parse (value , True )
223+ except Exception as ex :
224+ logger .debug (ex ) # log parse json failed expression
225+ raise shorthand_ex # raise shorthand syntax exception
226+ return v
227+
228+ @classmethod
229+ def format_data (cls , data ):
230+ if data == AAZBlankArgValue :
231+ if cls ._schema ._blank == AAZUndefined :
232+ raise AAZInvalidValueError ("argument value cannot be blank" )
233+ if isinstance (cls ._schema ._blank , AAZPromptInput ):
234+ # Postpone the prompt input when apply the operation.
235+ # In order not to break the logic of displaying help or validating other parameters.
236+ return AAZPromptInputOperation (prompt = cls ._schema ._blank , action_cls = cls )
237+ data = copy .deepcopy (cls ._schema ._blank )
238+
162239 if data is None :
163240 if cls ._schema ._nullable :
164241 return data
165242 raise AAZInvalidValueError ("field is not nullable" )
166-
167- raise AAZInvalidValueError (f"{ cls ._schema .DataType } type value expected, got '{ data } '({ type (data )} )" )
243+ return data
168244
169245
170246class AAZCompoundTypeArgAction (AAZArgAction ): # pylint: disable=abstract-method
@@ -245,7 +321,7 @@ def _decode_value(cls, schema, value): # pylint: disable=unused-argument
245321 # simple type
246322 v = cls ._str_parser (value , is_simple = True )
247323 else :
248- # compound type
324+ # compound type or any type
249325 # read from file
250326 path = os .path .expanduser (value )
251327 if os .path .exists (path ):
@@ -264,7 +340,6 @@ def _decode_value(cls, schema, value): # pylint: disable=unused-argument
264340 except Exception as ex :
265341 logger .debug (ex ) # log parse json failed expression
266342 raise shorthand_ex # raise shorthand syntax exception
267-
268343 return v
269344
270345
@@ -324,52 +399,6 @@ def format_data(cls, data):
324399 raise AAZInvalidValueError (f"dict type value expected, got '{ data } '({ type (data )} )" )
325400
326401
327- class AAZFreeFormDictArgAction (AAZSimpleTypeArgAction ):
328-
329- @classmethod
330- def decode_str (cls , value ):
331- from azure .cli .core .util import get_file_json , shell_safe_json_parse , get_file_yaml
332-
333- if len (value ) == 0 :
334- # the express "a=" will return the blank value of schema 'a'
335- return AAZBlankArgValue
336-
337- path = os .path .expanduser (value )
338- if os .path .exists (path ):
339- if path .endswith ('.yml' ) or path .endswith ('.yaml' ):
340- # read from yaml file
341- v = get_file_yaml (path )
342- else :
343- # read from json file
344- v = get_file_json (path , preserve_order = True )
345- else :
346- try :
347- v = shell_safe_json_parse (value , True )
348- except Exception as ex :
349- logger .debug (ex ) # log parse json failed expression
350- raise
351- return v
352-
353- @classmethod
354- def format_data (cls , data ):
355- if data == AAZBlankArgValue :
356- if cls ._schema ._blank == AAZUndefined :
357- raise AAZInvalidValueError ("argument value cannot be blank" )
358- assert not isinstance (cls ._schema ._blank , AAZPromptInput ), "Prompt input is not supported in " \
359- "FreeFormDict args."
360- data = copy .deepcopy (cls ._schema ._blank )
361-
362- if isinstance (data , dict ):
363- return data
364-
365- if data is None :
366- if cls ._schema ._nullable :
367- return data
368- raise AAZInvalidValueError ("field is not nullable" )
369-
370- raise AAZInvalidValueError (f"dict type value expected, got '{ data } '({ type (data )} )" )
371-
372-
373402class AAZListArgAction (AAZCompoundTypeArgAction ):
374403
375404 def __call__ (self , parser , namespace , values , option_string = None ):
0 commit comments