Usage::
- mysql-to-sql.py [-i infile] [-o outfile]
+ mysql-to-sql.py [infile] [[-o] outfile]
Options::
- -i, --infile infile Input file, stdin if absent
- -o, --outfile outfile Output file, stdout if absent
+ infile Input file, stdin if absent or '-'
+ -o, --outfile outfile Output file, stdout if absent or '-'
+
+Option `-o` is useful when infile is absent (input is redirected), for
+example::
+
+ mysql-to-sql.py -o outfile.sql < infile.sql
+ cat infile.sql | mysql-to-sql.py -o outfile.sql
+
+But of course it simple can be::
+
+ mysql-to-sql.py - outfile.sql < infile.sql
+ cat infile.sql | mysql-to-sql.py - outfile.sql
Indices and tables
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert MySQL to SQL')
- parser.add_argument('-i', '--infile', help='input file name')
parser.add_argument('-o', '--outfile', help='output file name')
+ parser.add_argument('infile', help='input file name')
+ parser.add_argument('output_file', nargs='?', help='output file name')
args = parser.parse_args()
if args.infile:
- infile = open(args.infile, 'rt')
+ if args.infile == '-':
+ infile = sys.stdin
+ else:
+ infile = open(args.infile, 'rt')
else:
infile = sys.stdin
- if infile.isatty():
- print("Error: cannot input from console", file=sys.stderr)
- parser.print_help()
- sys.exit()
+
+ if infile.isatty():
+ print("Error: cannot read from console", file=sys.stderr)
+ parser.print_help()
+ sys.exit(1)
if args.outfile:
+ if args.output_file:
+ print("Error: too many output files", file=sys.stderr)
+ parser.print_help()
+ sys.exit(1)
+
+ outfile = args.outfile
+
+ elif args.output_file:
+ outfile = args.output_file
+
+ else:
+ outfile = '-'
+
+ if outfile == '-':
+ outfile = sys.stdout
+ else:
try:
- outfile = open(args.outfile, 'wt')
+ outfile = open(outfile, 'wt')
except:
if infile is not sys.stdin:
infile.close()
raise
- else:
- outfile = sys.stdout
main(infile, outfile)