Skip to content

Commit 6fda53b

Browse files
committed
Add documentation for the argparser utilities
1 parent 59ae690 commit 6fda53b

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

tools/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ def json_file_to_dict(fname):
202202
# Wowza, double closure
203203
def argparse_type(casedness, prefer_hyphen=False) :
204204
def middle(list, type_name):
205+
# validate that an argument passed in (as string) is a member of the list of possible
206+
# arguments. Offer a suggestion if the case of the string, or the hyphens/underscores
207+
# do not match the expected style of the argument.
205208
def parse_type(string):
206209
if prefer_hyphen: newstring = casedness(string).replace("_","-")
207210
else: newstring = casedness(string).replace("-","_")
@@ -214,22 +217,27 @@ def parse_type(string):
214217
return parse_type
215218
return middle
216219

220+
# short cuts for the argparse_type versions
217221
argparse_uppercase_type = argparse_type(str.upper, False)
218222
argparse_lowercase_type = argparse_type(str.lower, False)
219223
argparse_uppercase_hyphen_type = argparse_type(str.upper, True)
220224
argparse_lowercase_hyphen_type = argparse_type(str.lower, True)
221225

226+
# An argument parser combinator that takes in an argument parser and creates a new parser that
227+
# accepts a comma separated list of the same thing.
222228
def argparse_many(fn):
223229
def wrap(string):
224230
return [fn(s) for s in string.split(",")]
225231
return wrap
226232

233+
# An argument parser that verifies that a string passed in corresponds to a file
227234
def argparse_filestring_type(string) :
228235
if exists(string) :
229236
return string
230237
else :
231238
raise argparse.ArgumentTypeError("{0}"" does not exist in the filesystem.".format(string))
232239

240+
# render a list of strings as a in a bunch of columns
233241
def columnate(strings, seperator=", ", chars=80):
234242
col_width = max(len(s) for s in strings)
235243
total_width = col_width + len(seperator)

0 commit comments

Comments
 (0)