@@ -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,86 @@ def format_data(cls, data):
159167 if isinstance (data , cls ._schema .DataType ):
160168 return data
161169
170+ if isinstance (data , int ) and cls ._schema .DataType == float :
171+ return data
172+
173+ raise AAZInvalidValueError (f"{ cls ._schema .DataType } type value expected, got '{ data } '({ type (data )} )" )
174+
175+
176+ class AAZAnyTypeArgAction (AAZArgAction ):
177+
178+ @classmethod
179+ def setup_operations (cls , dest_ops , values , prefix_keys = None ):
180+ if prefix_keys is None :
181+ prefix_keys = []
182+ if values is None :
183+ data = AAZBlankArgValue # use blank data when values string is None
184+ else :
185+ if isinstance (values , list ):
186+ assert prefix_keys # the values will be input as an list when parse singular option of a list argument
187+ if len (values ) != 1 :
188+ raise AAZInvalidValueError (f"only support 1 value, got { len (values )} : { values } " )
189+ values = values [0 ]
190+
191+ if isinstance (values , str ) and len (values ) > 0 :
192+ try :
193+ data = cls .decode_str (values )
194+ except AAZShowHelp as aaz_help :
195+ aaz_help .schema = cls ._schema
196+ raise aaz_help
197+ else :
198+ data = values
199+ data = cls .format_data (data )
200+ dest_ops .add (data , * prefix_keys )
201+
202+ @classmethod
203+ def decode_str (cls , value ):
204+ from azure .cli .core .util import get_file_json , shell_safe_json_parse , get_file_yaml
205+
206+ # check if the value is a partial value
207+ key , _ , v = cls ._str_parser .split_partial_value (value )
208+ if key is not None :
209+ raise AAZInvalidValueError (
210+ "AnyType args only support full value shorthand syntax, "
211+ "please don't use partial value shorthand syntax. "
212+ "If it's a simple string, please wrap it with single quotes." )
213+
214+ # read from file
215+ path = os .path .expanduser (value )
216+ if os .path .exists (path ):
217+ if path .endswith ('.yml' ) or path .endswith ('.yaml' ):
218+ # read from yaml file
219+ v = get_file_yaml (path )
220+ else :
221+ # read from json file
222+ v = get_file_json (path , preserve_order = True )
223+ else :
224+ try :
225+ v = cls ._str_parser (value )
226+ except AAZInvalidShorthandSyntaxError as shorthand_ex :
227+ try :
228+ v = shell_safe_json_parse (value , True )
229+ except Exception as ex :
230+ logger .debug (ex ) # log parse json failed expression
231+ raise shorthand_ex # raise shorthand syntax exception
232+ return v
233+
234+ @classmethod
235+ def format_data (cls , data ):
236+ if data == AAZBlankArgValue :
237+ if cls ._schema ._blank == AAZUndefined :
238+ raise AAZInvalidValueError ("argument value cannot be blank" )
239+ if isinstance (cls ._schema ._blank , AAZPromptInput ):
240+ # Postpone the prompt input when apply the operation.
241+ # In order not to break the logic of displaying help or validating other parameters.
242+ return AAZPromptInputOperation (prompt = cls ._schema ._blank , action_cls = cls )
243+ data = copy .deepcopy (cls ._schema ._blank )
244+
162245 if data is None :
163246 if cls ._schema ._nullable :
164247 return data
165248 raise AAZInvalidValueError ("field is not nullable" )
166-
167- raise AAZInvalidValueError (f"{ cls ._schema .DataType } type value expected, got '{ data } '({ type (data )} )" )
249+ return data
168250
169251
170252class AAZCompoundTypeArgAction (AAZArgAction ): # pylint: disable=abstract-method
@@ -245,7 +327,7 @@ def _decode_value(cls, schema, value): # pylint: disable=unused-argument
245327 # simple type
246328 v = cls ._str_parser (value , is_simple = True )
247329 else :
248- # compound type
330+ # compound type or any type
249331 # read from file
250332 path = os .path .expanduser (value )
251333 if os .path .exists (path ):
@@ -264,7 +346,6 @@ def _decode_value(cls, schema, value): # pylint: disable=unused-argument
264346 except Exception as ex :
265347 logger .debug (ex ) # log parse json failed expression
266348 raise shorthand_ex # raise shorthand syntax exception
267-
268349 return v
269350
270351
@@ -324,52 +405,6 @@ def format_data(cls, data):
324405 raise AAZInvalidValueError (f"dict type value expected, got '{ data } '({ type (data )} )" )
325406
326407
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-
373408class AAZListArgAction (AAZCompoundTypeArgAction ):
374409
375410 def __call__ (self , parser , namespace , values , option_string = None ):
0 commit comments