1- """ARGS
1+ """ARGS.
22
33:Description: Handling of command line arguments.
44
77
88"""
99
10+ import sys
11+ from optparse import OptionParser
1012
11- def parse_options (p_def , short_options , types , help_strings ):
13+
14+ def parse_options (p_def , short_options , types , help_strings , args = None ):
1215 """Parse command line options.
1316
1417 Parameters
@@ -26,45 +29,46 @@ def parse_options(p_def, short_options, types, help_strings):
2629 -------
2730 options: tuple
2831 Command line options
32+
2933 """
34+ if args is None :
35+ args = sys .argv [1 :]
3036
31- usage = "%prog [OPTIONS]"
37+ usage = "%prog [OPTIONS]"
3238 parser = OptionParser (usage = usage )
3339
3440 # Loop over default parameter values
3541 for key in p_def :
36-
3742 # Process if help string exists
3843 if key in help_strings :
39-
4044 # Set short option
4145 if key in short_options :
4246 short = short_options [key ]
4347 else :
4448 # Default is no short option
45- short = ''
49+ short = ""
4650
4751 # Set type
4852 if key in types :
4953 typ = types [key ]
5054 else :
5155 # Default is str
52- typ = ' string'
56+ typ = " string"
5357
5458 # Special case: bool type
55- if typ == ' bool' :
59+ if typ == " bool" :
5660 parser .add_option (
57- f' { short } ' ,
58- f' --{ key } ' ,
61+ f" { short } " ,
62+ f" --{ key } " ,
5963 dest = key ,
6064 default = False ,
61- action = ' store_true' ,
65+ action = " store_true" ,
6266 help = help_strings [key ].format (p_def [key ]),
6367 )
6468 else :
6569 parser .add_option (
6670 short ,
67- f' --{ key } ' ,
71+ f" --{ key } " ,
6872 dest = key ,
6973 type = typ ,
7074 default = p_def [key ],
@@ -73,13 +77,85 @@ def parse_options(p_def, short_options, types, help_strings):
7377
7478 # Add verbose option
7579 parser .add_option (
76- '-v' ,
77- ' --verbose' ,
78- dest = ' verbose' ,
79- action = ' store_true' ,
80- help = f' verbose output'
80+ "-v" ,
81+ " --verbose" ,
82+ dest = " verbose" ,
83+ action = " store_true" ,
84+ help = f" verbose output" ,
8185 )
8286
83- options , args = parser .parse_args ()
87+ options , my_args = parser .parse_args (args )
8488
8589 return options
90+
91+
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 :
139+ # 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 (
150+ 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 :
157+ raise ValueError (
158+ f"String '{ len (res )} ' has length { num } , required is { num } "
159+ )
160+
161+ return res
0 commit comments