kh2reg.py: switch from getopt to argparse.

This change gives us an automatic --help option, which is always
useful for a script used very rarely. It also makes it that much
easier to add extra options.
This commit is contained in:
Simon Tatham 2019-04-21 14:07:25 +01:00
Родитель 91333f7c74
Коммит ac1dd1bd2e
1 изменённых файлов: 19 добавлений и 11 удалений

Просмотреть файл

@ -16,7 +16,7 @@ import struct
import string import string
import re import re
import sys import sys
import getopt import argparse
import itertools import itertools
import collections import collections
from functools import reduce from functools import reduce
@ -370,18 +370,26 @@ class UnixOutputFormatter(OutputFormatter):
self.fh.write('%s %s\n' % (key, value)) self.fh.write('%s %s\n' % (key, value))
def main(): def main():
output_formatter_class = WindowsOutputFormatter parser = argparse.ArgumentParser(
try: description="Convert OpenSSH known hosts files to PuTTY's format.")
optlist, args = getopt.getopt(sys.argv[1:], '', [ 'win', 'unix' ]) group = parser.add_mutually_exclusive_group()
if any(x[0] == '--unix' for x in optlist): group.add_argument(
output_formatter_class = UnixOutputFormatter "--windows", "--win", action='store_const',
except getopt.error as e: dest="output_formatter_class", const=WindowsOutputFormatter,
sys.stderr.write(str(e) + "\n") help="Produce Windows .reg file output that regedit.exe can import"
sys.exit(1) " (default).")
group.add_argument(
"--unix", action='store_const',
dest="output_formatter_class", const=UnixOutputFormatter,
help="Produce a file suitable for use as ~/.putty/sshhostkeys.")
parser.add_argument("infile", nargs="*",
help="Input file(s) to read from (default stdin).")
parser.set_defaults(output_formatter_class=WindowsOutputFormatter)
args = parser.parse_args()
output_formatter = output_formatter_class(sys.stdout) output_formatter = args.output_formatter_class(sys.stdout)
output_formatter.header() output_formatter.header()
for line in fileinput.input(args): for line in fileinput.input(args.infile):
handle_line(line, output_formatter) handle_line(line, output_formatter)
output_formatter.trailer() output_formatter.trailer()