|
24 | 24 | from __future__ import print_function |
25 | 25 | from __future__ import with_statement |
26 | 26 |
|
27 | | -import argparse |
| 27 | +import optparse |
28 | 28 | import codecs |
29 | 29 | import csv |
30 | 30 | import errno |
@@ -1064,11 +1064,11 @@ def isvalid_about_file(file_name): |
1064 | 1064 | 2 - Print error and warning messages""" |
1065 | 1065 |
|
1066 | 1066 |
|
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] |
1072 | 1072 |
|
1073 | 1073 | # TODO: need more path normalization (normpath, expanduser) |
1074 | 1074 | # input_path = abspath(input_path) |
@@ -1104,22 +1104,59 @@ def main(parser, args): |
1104 | 1104 |
|
1105 | 1105 |
|
1106 | 1106 | 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) |
1118 | 1155 | return parser |
1119 | 1156 |
|
1120 | 1157 |
|
1121 | 1158 | if __name__ == "__main__": |
1122 | 1159 | parser = get_parser() |
1123 | | - args = parser.parse_args() |
| 1160 | + (options, args) = parser.parse_args() |
1124 | 1161 |
|
1125 | | - main(parser, args) |
| 1162 | + main(parser, options, args) |
0 commit comments