@@ -19,11 +19,10 @@ def option_should_be_skipped(value: t.Any) -> bool:
1919class ArgumentUnparser :
2020 """For performing reverse operation to what argparse.ArgumentParser does."""
2121
22- # pylint: disable = too-many-arguments
22+ # pylint: disable = too-many-arguments, too-many-positional-arguments
2323 def __init__ (
2424 self , short_opt : str = '-' , long_opt : str = '--' , opt_value : str = '=' ,
2525 begin_delim : str = '"' , end_delim : str = '"' ) -> None :
26-
2726 assert isinstance (short_opt , str )
2827 assert isinstance (long_opt , str )
2928 assert isinstance (opt_value , str )
@@ -46,78 +45,66 @@ def unparse_arg(self, arg: t.Any) -> str: # pylint: disable = no-self-use
4645 arg = '""'
4746 return arg
4847
49- def unparse_args (self , arguments : t .Sequence [t .Any ],
50- * , to_list : bool = False ) -> t .Union [str , t .List [str ]]:
51- """Convert list to string of command-line args."""
48+ def unparse_args_to_list (self , arguments : t .Sequence [t .Any ]) -> list [str ]:
49+ """Convert list of objects to a list of command-line args."""
5250 unparsed_list = []
5351 for arg in arguments :
5452 unparsed_list .append (self .unparse_arg (arg ))
5553 _LOG .debug ('%s: unparsed args to %s' , self , unparsed_list )
56- if to_list :
57- return unparsed_list
58- unparsed = ' ' .join (unparsed_list )
54+ return unparsed_list
55+
56+ def unparse_args (self , arguments : t .Sequence [t .Any ]) -> str :
57+ """Convert list to string of command-line args."""
58+ unparsed = ' ' .join (self .unparse_args_to_list (arguments ))
5959 _LOG .debug ('%s: converted unparsed args to string "%s"' , self , unparsed )
6060 return unparsed
6161
62- def unparse_option (self , key : str , value : t .Any ,
63- * , to_list : bool = False ) -> t .Union [str , t .List [str ]]:
64- """Convert a key-value pair into a string that can be used as a command-line option."""
62+ def unparse_option_to_list (self , key : str , value : t .Any ) -> list [str ]:
63+ """Convert a key-value pair into a short list to be used as a command-line option."""
6564 if option_should_be_skipped (value ):
66- return [] if to_list else ''
65+ return []
6766 unparsed_key = f'{ self ._long_opt if len (key ) > 1 else self ._short_opt } { key } '
68- if not treat_as_option_with_no_value (value ):
69- unparsed_value = self .unparse_arg (value )
70- if to_list and (self ._opt_value == ' ' or treat_as_option_with_no_value (value )):
71- if treat_as_option_with_no_value (value ):
72- return [unparsed_key ]
73- return [unparsed_key , unparsed_value ]
74- if not treat_as_option_with_no_value (value ):
75- unparsed_option = f'{ unparsed_key } { self ._opt_value } { unparsed_value } '
76- if to_list :
77- return [unparsed_option ]
7867 if treat_as_option_with_no_value (value ):
79- return unparsed_key
80- return unparsed_option
68+ return [unparsed_key ]
69+ unparsed_value = self .unparse_arg (value )
70+ if self ._opt_value == ' ' :
71+ return [unparsed_key , unparsed_value ]
72+ return [f'{ unparsed_key } { self ._opt_value } { unparsed_value } ' ]
8173
82- def unparse_options (self , options : t .Mapping [str , t .Any ],
83- * , to_list : bool = False ) -> t .Union [str , t .List [str ]]:
84- """Convert dictionary to string of command-line args."""
74+ def unparse_option (self , key : str , value : t .Any ) -> str :
75+ """Convert a key-value pair into a string that can be used as a command-line option."""
76+ if option_should_be_skipped (value ):
77+ return ''
78+ unparsed = ' ' .join (self .unparse_option_to_list (key , value ))
79+ return unparsed
80+
81+ def unparse_options_to_list (self , options : t .Mapping [str , t .Any ]) -> list [str ]:
82+ """Convert dictionary to a list of command-line args."""
8583 unparsed_list : t .List [str ] = []
8684 for key , value in options .items ():
8785 if option_should_be_skipped (value ):
8886 continue
89- unparsed_option = self .unparse_option (key , value , to_list = to_list )
90- if to_list :
91- unparsed_list += unparsed_option
92- else :
93- assert isinstance (unparsed_option , str ), type (unparsed_option )
94- unparsed_list .append (unparsed_option )
87+ unparsed_list += self .unparse_option_to_list (key , value )
9588 _LOG .debug ('%s: unparsed options to %s' , self , unparsed_list )
96- if to_list :
97- return unparsed_list
98- unparsed = ' ' .join (unparsed_list )
89+ return unparsed_list
90+
91+ def unparse_options (self , options : t .Mapping [str , t .Any ]) -> str :
92+ """Convert dictionary to string of command-line args."""
93+ unparsed = ' ' .join (self .unparse_options_to_list (options ))
9994 _LOG .debug ('%s: converted unparsed options to string "%s"' , self , unparsed )
10095 return unparsed
10196
102- def unparse_options_and_args (self , options : t .Mapping [str , t .Any ], arguments : t .Sequence [t .Any ],
103- * , to_list : bool = False ) -> t .Union [str , t .List [str ]]:
97+ def unparse_options_and_args_to_list (
98+ self , options : t .Mapping [str , t .Any ], arguments : t .Sequence [t .Any ]) -> list [str ]:
99+ """Convert dictionary and list to a list of command-line args."""
100+ unparsed_options = [] if options is None else self .unparse_options_to_list (options )
101+ unparsed_args = [] if arguments is None else self .unparse_args_to_list (arguments )
102+ return unparsed_options + unparsed_args
103+
104+ def unparse_options_and_args (
105+ self , options : t .Mapping [str , t .Any ], arguments : t .Sequence [t .Any ]) -> str :
104106 """Convert dictionary and list to string of command-line args."""
105- if options is None :
106- unparsed_options = [] if to_list else ''
107- else :
108- unparsed_options = self .unparse_options (options , to_list = to_list )
109- if arguments is None :
110- unparsed_args = [] if to_list else ''
111- else :
112- unparsed_args = self .unparse_args (arguments , to_list = to_list )
113- if to_list :
114- return unparsed_options + unparsed_args
115- unparsed = []
116- if unparsed_options :
117- unparsed .append (unparsed_options )
118- if unparsed_args :
119- unparsed .append (unparsed_args )
120- return ' ' .join (unparsed )
107+ return ' ' .join (self .unparse_options_and_args_to_list (options , arguments ))
121108
122109 def unparse_to_list (self , * args , ** kwargs ) -> list :
123110 """Unparse given args as command-line arguments and kwargs as command-line options.
@@ -126,7 +113,7 @@ def unparse_to_list(self, *args, **kwargs) -> list:
126113
127114 This process is a reverse of what built-in argparse module does with parse_args() method.
128115 """
129- return self .unparse_options_and_args (kwargs , args , to_list = True )
116+ return self .unparse_options_and_args_to_list (kwargs , args )
130117
131118 def unparse (self , * args , ** kwargs ) -> str :
132119 """Unparse given args as command-line arguments and kwargs as command-line options.
0 commit comments