@@ -24,6 +24,9 @@ def parse_options(p_def, short_options, types, help_strings, args=None):
2424 command line options types
2525 help_strings : dict
2626 command line options help strings
27+ args : list, optional
28+ list of arguments, default is None, in which case sys.args[1:]
29+ is used
2730
2831 Returns
2932 -------
@@ -84,78 +87,88 @@ def parse_options(p_def, short_options, types, help_strings, args=None):
8487 help = f"verbose output" ,
8588 )
8689
87- options , my_args = parser .parse_args (args )
90+ options_values , my_args = parser .parse_args (args )
91+
92+ # Transform parameter values to dict
93+ options = {}
94+ for key in vars (options_values ):
95+ options [key ] = getattr (options_values , key )
96+
97+ # Add other default values which do not have argument option
98+ for key in p_def :
99+ if key not in options :
100+ options [key ] = p_def [key ]
88101
89102 return options
90103
91104
92- def my_string_split (string , num = - 1 , verbose = False , stop = False , sep = None ):
93- """My String Split.
94-
95- Split a *string* into a list of strings. Choose as separator
96- the first in the list [space, underscore] that occurs in the string.
97- (Thus, if both occur, use space.)
98-
99- Parameters
100- ----------
101- string : str
102- Input string
103- num : int
104- Required length of output list of strings, -1 if no requirement.
105- verbose : bool
106- Verbose output
107- stop : bool
108- Stop programs with error if True, return None and continues otherwise
109- sep : bool
110- Separator, try ' ', '_', and '.' if None (default)
111-
112- Raises
113- ------
114- ValueError
115- If number of elements in string and num are different, for stop=True
116- If no separator found in string
117-
118- Returns
119- -------
120- list
121- List of string on success, and None if failed
122-
123- """
124- if string is None :
125- return None
126-
127- if sep is None :
128- has_space = string .find (" " )
129- has_underscore = string .find ("_" )
130- has_dot = string .find ("." )
131-
132- if has_space != - 1 :
133- my_sep = " "
134- elif has_underscore != - 1 :
135- my_sep = "_"
136- elif has_dot != - 1 :
137- my_sep = "."
138- else :
105+ def my_string_split (string , num = - 1 , verbose = False , stop = False , sep = None ):
106+ """My String Split.
107+
108+ Split a *string* into a list of strings. Choose as separator
109+ the first in the list [space, underscore] that occurs in the string.
110+ (Thus, if both occur, use space.)
111+
112+ Parameters
113+ ----------
114+ string : str
115+ Input string
116+ num : int
117+ Required length of output list of strings, -1 if no requirement.
118+ verbose : bool
119+ Verbose output
120+ stop : bool
121+ Stop programs with error if True, return None and continues otherwise
122+ sep : bool
123+ Separator, try ' ', '_', and '.' if None (default)
124+
125+ Raises
126+ ------
127+ ValueError
128+ If number of elements in string and num are different, for stop=True
129+ If no separator found in string
130+
131+ Returns
132+ -------
133+ list
134+ List of string on success, and None if failed
135+
136+ """
137+ if string is None :
138+ return None
139+
140+ if sep is None :
141+ has_space = string .find (" " )
142+ has_underscore = string .find ("_" )
143+ has_dot = string .find ("." )
144+
145+ if has_space != - 1 :
146+ my_sep = " "
147+ elif has_underscore != - 1 :
148+ my_sep = "_"
149+ elif has_dot != - 1 :
150+ my_sep = "."
151+ else :
139152 # no separator found, does string consist of only one element?
140- if num == - 1 or num == 1 :
141- my_sep = None
142- else :
143- raise ValueError (
144- "No separator (' ', '_', or '.') found in string"
145- + f" '{ string } ', cannot split"
146- )
147- else :
148- if not string .find (sep ):
149- raise ValueError (
153+ if num == - 1 or num == 1 :
154+ my_sep = None
155+ else :
156+ raise ValueError (
157+ "No separator (' ', '_', or '.') found in string"
158+ + f" '{ string } ', cannot split"
159+ )
160+ else :
161+ if not string .find (sep ):
162+ raise ValueError (
150163 f"No separator '{ sep } ' found in string '{ string } ' cannot split"
151- )
152- my_sep = sep
153-
154- res = string .split (my_sep )
155-
156- if num != - 1 and num != len (res ) and stop :
164+ )
165+ my_sep = sep
166+
167+ res = string .split (my_sep )
168+
169+ if num != - 1 and num != len (res ) and stop :
157170 raise ValueError (
158171 f"String '{ len (res )} ' has length { num } , required is { num } "
159172 )
160-
173+
161174 return res
0 commit comments