Fix call of layout2layout as a module.

If we call parser.parse_args(), thus with no arguments, the parser uses
sys.argv (because that is the default). We should pass argv since that was
the purpose of handling argv in the main function.

We pass argv[1:] since when parsing the arguments we always ignore the name
of the program.

Use the full power of argparse to declare the default value of the end_format.
This commit is contained in:
José Matos 2017-05-13 15:25:44 +01:00
parent 5608f6fdb6
commit cc7ca138da

View File

@ -1172,14 +1172,14 @@ def main(argv):
parser = argparse.ArgumentParser(**args) parser = argparse.ArgumentParser(**args)
parser.add_argument("-t", "--to", type=int, dest="format", parser.add_argument("-t", "--to", type=int, dest="format", default= currentFormat,
help=("destination layout format, default %i (latest)") % currentFormat) help=("destination layout format, default %i (latest)") % currentFormat)
parser.add_argument("input_file", nargs='?', type=cmd_arg, default=None, parser.add_argument("input_file", nargs='?', type=cmd_arg, default=None,
help="input file (default stdin)") help="input file (default stdin)")
parser.add_argument("output_file", nargs='?', type=cmd_arg, default=None, parser.add_argument("output_file", nargs='?', type=cmd_arg, default=None,
help="output file (default stdout)") help="output file (default stdout)")
options = parser.parse_args(argv) options = parser.parse_args(argv[1:])
# Open files # Open files
if options.input_file: if options.input_file:
@ -1192,19 +1192,14 @@ def main(argv):
else: else:
output = sys.stdout output = sys.stdout
if options.format: if options.format > currentFormat:
end_format = options.format error("Format %i does not exist" % options.format);
else:
end_format = currentFormat
if end_format > currentFormat:
error("Format %i does not exist" % end_format);
# Do the real work # Do the real work
lines = read(source) lines = read(source)
format = 1 format = 1
while (format < end_format): while (format < options.format):
format = convert(lines, end_format) format = convert(lines, options.format)
write(output, lines) write(output, lines)
# Close files # Close files