Skip to content

Commit 0c765d3

Browse files
committed
#35 switched to optparse because Python 2.6 does not have argparse in its standard library and we are not currently shipping with dependencies declared
1 parent d8ad457 commit 0c765d3

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

about.py

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from __future__ import print_function
2525
from __future__ import with_statement
2626

27-
import argparse
27+
import optparse
2828
import codecs
2929
import csv
3030
import errno
@@ -1064,11 +1064,11 @@ def isvalid_about_file(file_name):
10641064
2 - Print error and warning messages"""
10651065

10661066

1067-
def main(parser, args):
1068-
overwrite = args.overwrite
1069-
verbosity = args.verbosity
1070-
input_path = args.input_path
1071-
output_path = args.output_path
1067+
def main(parser, options, args):
1068+
overwrite = options.overwrite
1069+
verbosity = options.verbosity
1070+
input_path = args[0]
1071+
output_path = args[1]
10721072

10731073
# TODO: need more path normalization (normpath, expanduser)
10741074
# input_path = abspath(input_path)
@@ -1104,22 +1104,59 @@ def main(parser, args):
11041104

11051105

11061106
def get_parser():
1107-
parser = argparse.ArgumentParser(
1108-
description=SYNTAX, formatter_class=argparse.RawTextHelpFormatter)
1109-
parser.add_argument('--overwrite', action='store_true',
1110-
help='Overwrites the output file if it exists')
1111-
parser.add_argument('-v', '--version', action='version', version=VERSION,
1112-
help='Display current version, license notice, and '
1113-
'copyright notice')
1114-
parser.add_argument('--verbosity', type=int, choices=[0, 1, 2],
1115-
help=VERBOSITY)
1116-
parser.add_argument('input_path', help='The input path')
1117-
parser.add_argument('output_path', help='The output path')
1107+
class MyFormatter(optparse.IndentedHelpFormatter):
1108+
def _format_text(self, text):
1109+
"""
1110+
Overridden to allow description to be printed without
1111+
modification
1112+
"""
1113+
return text
1114+
1115+
def format_option(self, option):
1116+
"""
1117+
Overridden to allow options help text to be printed without
1118+
modification
1119+
"""
1120+
result = []
1121+
opts = self.option_strings[option]
1122+
opt_width = self.help_position - self.current_indent - 2
1123+
if len(opts) > opt_width:
1124+
opts = "%*s%s\n" % (self.current_indent, "", opts)
1125+
indent_first = self.help_position
1126+
else: # start help on same line as opts
1127+
opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
1128+
indent_first = 0
1129+
result.append(opts)
1130+
if option.help:
1131+
help_text = self.expand_default(option)
1132+
help_lines = help_text.split('\n')
1133+
#help_lines = textwrap.wrap(help_text, self.help_width)
1134+
result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
1135+
result.extend(["%*s%s\n" % (self.help_position, "", line)
1136+
for line in help_lines[1:]])
1137+
elif opts[-1] != "\n":
1138+
result.append("\n")
1139+
return "".join(result)
1140+
1141+
parser = optparse.OptionParser(
1142+
usage='%prog [options] input_path output_path',
1143+
description=SYNTAX,
1144+
add_help_option=False,
1145+
formatter=MyFormatter(),
1146+
)
1147+
parser.add_option("-h", "--help", action="help", help="Display help")
1148+
parser.add_option(
1149+
"--version",
1150+
action="version",
1151+
help='Display current version, license notice, and copyright notice')
1152+
parser.add_option('--overwrite', action='store_true',
1153+
help='Overwrites the output file if it exists')
1154+
parser.add_option('--verbosity', type=int, help=VERBOSITY)
11181155
return parser
11191156

11201157

11211158
if __name__ == "__main__":
11221159
parser = get_parser()
1123-
args = parser.parse_args()
1160+
(options, args) = parser.parse_args()
11241161

1125-
main(parser, args)
1162+
main(parser, options, args)

0 commit comments

Comments
 (0)