@@ -32,12 +32,14 @@ def __init__(
3232 long_arg : str ,
3333 help_string : str ,
3434 default = None ,
35- required = False ):
35+ required = False ,
36+ arg_type = None ):
3637 self .short_arg = short_arg
3738 self .long_arg = long_arg
3839 self .help_string = help_string
3940 self .default = default
4041 self .required = required
42+ self .arg_type = arg_type if arg_type else str
4143
4244
4345def parse_args (
@@ -49,7 +51,7 @@ def parse_args(
4951 for argument in arguments :
5052 parser .add_argument (
5153 argument .short_arg , argument .long_arg , required = argument .required ,
52- help = argument .help_string , default = argument .default ,
54+ help = argument .help_string , default = argument .default , type = argument . arg_type ,
5355 )
5456 parser .add_argument (
5557 "-u" ,
@@ -416,3 +418,43 @@ def show_lun() -> None:
416418 except NetAppRestError as error :
417419 print ("Error:- " % error .http_err_response .http_response .text )
418420 print ("Exception caught :" + str (error ))
421+
422+
423+ class LiveMultilineOutput :
424+ """This is a class for managing multiline text output on the screen.
425+ While inside this class' context, the application can change the output's
426+ buffer by calling change() and the buffer will be redrawn, clearing the
427+ previous output and printing all of the new output. Once the context is
428+ exited, the cursor is moved to the line after the last line in the output
429+ so that normal printing can continue.
430+ """
431+
432+ def __init__ (self , initial_data = None ):
433+ self .buffer = []
434+ if initial_data :
435+ self .buffer = initial_data
436+ self ._lines_drawn = 0
437+ self .draw ()
438+
439+ def change (self , new_list ):
440+ """Update the text buffer with a new list of strings"""
441+
442+ self .buffer = new_list
443+ self .draw ()
444+
445+ def draw (self ):
446+ """Erase our current line and write a new one, then move down"""
447+
448+ for line in self .buffer :
449+ print ("\033 [K%s" % line )
450+ self ._lines_drawn = len (self .buffer )
451+ # now that we're done, move all the way back to the top left
452+ if self ._lines_drawn > 0 :
453+ print ("\033 [%sA\r " % self ._lines_drawn , end = "" )
454+
455+ def __enter__ (self ):
456+ return self
457+
458+ def __exit__ (self , exception_type , exception_value , traceback ):
459+ # move down to the bottom
460+ print ("\033 [%sB" % (self ._lines_drawn - 1 )).
0 commit comments