From 78b9d6d77a8a222c375d3762d9c9683b9dceb102 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 31 May 2013 03:10:46 +0200 Subject: hostinfo: migration from getopts to argparse --- bin/hostinfo | 88 ++++++++++++++++++++++------------------------------- hostinfo/prefix.py | 8 ++--- hostinfo/printer.py | 2 +- 3 files changed, 42 insertions(+), 56 deletions(-) diff --git a/bin/hostinfo b/bin/hostinfo index e10bf5f..9b4f09d 100755 --- a/bin/hostinfo +++ b/bin/hostinfo @@ -3,7 +3,7 @@ import sys import socket -import getopt +import argparse import yaml import os from dns import resolver,reversename @@ -19,7 +19,7 @@ def _get_data(path): stream = file(path, 'r') return yaml.load(stream) -def print_info(path, key=None, flags=list()): +def print_info(path, flags, key=None): data = _get_data(path) p = printer.Printer(data, flags) p.info(key) @@ -88,71 +88,57 @@ def find_host(basepath, host): return None -def print_help(self_name): - print('Usage: %s [info]' % self_name) - def main(): - self_name = sys.argv.pop(0) basepath = '/usr/local/share/hostinfo' if 'HOSTINFO_PATH' in os.environ and os.environ['HOSTINFO_PATH'] != '': basepath = os.environ['HOSTINFO_PATH'] - file = keys = help = hosts = short = False - flags = list() - try: - optlist, args = getopt.gnu_getopt(sys.argv, 'ofkvnh?p:ls', - ['oneline', 'file', 'keys', 'verbose', - 'nospaces', 'help', 'path=', 'hosts', - 'short']) - opts = {} - for key, value in optlist: - opts[key] = value - - file = any([o in opts for o in ['--file', '-f']]) - keys = any([o in opts for o in ['--keys', '-k']]) - help = any([o in opts for o in ['--help', '-h', '-?']]) - hosts = any([o in opts for o in ['--hosts', '-l']]) - short = any([o in opts for o in ['--short', '-s']]) - - if any([o in opts for o in ['--verbose', '-v']]): - flags.append('verbose') - if any([o in opts for o in ['--oneline', '-o']]): - flags.append('oneline') - if any([o in opts for o in ['--nospaces', '-n']]): - flags.append('nospaces') - - if any([o in opts for o in ['--path', '-p']]): - basepath = [opts[o] for o in ['--path', '-p'] if o in opts][0] - except getopt.GetoptError, e: - sys.stderr.write("Error: %s\n\n" % e) - args = [] - - if hosts: - if not print_hosts(basepath, short): + parser = argparse.ArgumentParser() + parser.add_argument("name", nargs="?") + parser.add_argument("filter", nargs="?") + parser.add_argument("-o", "--oneline", action="store_true", + help="each line is a complete record") + parser.add_argument("-f", "--file", action="store_true", + help="print the path of the file the information is read from") + parser.add_argument("-k", "--keys", action="store_true", + help="print only the available keys (used for bash completion)") + parser.add_argument("-v", "--verbose", action="store_true", + help="increase output verbosity") + parser.add_argument("-n", "--nospaces", action="store_true", + help="remove unnecessary spaces from output") + parser.add_argument("-p", "--path", default=basepath, + help="set the basepath to the hostinfo data") + parser.add_argument("-l", "--hosts", action="store_true", + help="lists all available hosts") + parser.add_argument("-s", "--short", action="store_true", + help="remove the domain from the output") + args = parser.parse_args() + + if args.path: + basepath = args.path + + if args.hosts: + if not print_hosts(basepath, args.short): sys.exit(1) sys.exit(0) - if help or len(args) < 1: - print_help(self_name) - if not help: - sys.exit(1) - sys.exit(0) + if args.name is None: + parser.print_help() + sys.exit(1) - path = find_host(basepath, args[0]) + # info + path = find_host(basepath, args.name) if path is None: - sys.stderr.write("Host '%s' could not be found!\n" % args[0]) + sys.stderr.write("Host '%s' could not be found!\n" % args.name) sys.exit(1) - if file: + if args.file: print(path) - elif keys: + elif args.keys: print_keys(path) else: - key=None - if len(args) > 1: - key = args[1] + print_info(path, key=args.filter, flags=args) - print_info(path, key=key, flags=flags) sys.exit(0) if __name__ == '__main__': diff --git a/hostinfo/prefix.py b/hostinfo/prefix.py index e9f0bf2..e1b72b5 100644 --- a/hostinfo/prefix.py +++ b/hostinfo/prefix.py @@ -12,7 +12,7 @@ class Printer: self.output = printer.pprint self.parent = printer - self.empty = 'oneline' in self.__class__.flags + self.empty = self.__class__.flags.oneline self.full_key = full_key self.has_output = False self.label = '' @@ -25,8 +25,8 @@ class Printer: return label label = "%s: " % label - if self.parent is not None and self.parent.full_key == '': - if 'nospaces' in self.__class__.flags: + if self.parent is None or self.parent.full_key == '': + if self.__class__.flags.nospaces: return label.rjust(maxlength+4) else: return (label + " ").rjust(maxlength+6) @@ -41,7 +41,7 @@ class Printer: self.empty = True def space(self, force=False): - if 'nospaces' not in self.__class__.flags: + if not self.__class__.flags.nospaces: if self.has_output or force: self.output('') diff --git a/hostinfo/printer.py b/hostinfo/printer.py index 5840bd5..a52f1ce 100644 --- a/hostinfo/printer.py +++ b/hostinfo/printer.py @@ -113,7 +113,7 @@ class Printer: elif isinstance(value, list): self._print_list(value, printer, filter_key) elif value is None: - if 'verbose' in self.flags: + if self.flags.verbose: printer.pprint('') else: self._print_value(value, printer, filter_key) -- cgit v1.2.3-1-g7c22