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