From bf247de21ba21a47a42e79fa6f8c8b5ba1756d46 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 8 May 2013 03:44:00 +0200 Subject: bin/hostinfo: add getopt for advanced parameter support --- bin/hostinfo | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/bin/hostinfo b/bin/hostinfo index c7cf8f5..ea785d8 100755 --- a/bin/hostinfo +++ b/bin/hostinfo @@ -3,6 +3,7 @@ import sys import socket +import getopt import yaml import os.path from dns import resolver,reversename @@ -14,14 +15,14 @@ if os.path.exists(os.path.join(lib, 'hostinfo')): from hostinfo import printer -def print_info(path, key=None): +def print_info(path, key=None, oneline=False): stream = file(path, 'r') data = yaml.load(stream) - p = printer.Printer(data, False) + p = printer.Printer(data, oneline) p.info(key) -def find_host(host, key=None): +def find_host(host, key=None, oneline=False): path = "/usr/local/share/hostinfo/%s" % host if not os.path.exists(path): @@ -41,22 +42,28 @@ def find_host(host, key=None): if not os.path.exists(path): return False - print_info(path, key) + print_info(path, key, oneline) return True def main(): - if len(sys.argv) < 2: - print('Usage: %s [info]' % sys.argv[0]) + self_name = sys.argv.pop(0) + + optlist, args = getopt.getopt(sys.argv, 'o', ['oneline']) + flags = [opt for (opt, _) in optlist] + oneline = '--oneline' in flags or '-o' in flags + + if len(args) < 1: + print('Usage: %s [info]' % self_name) sys.exit(1) - if len(sys.argv) == 2: - if find_host(sys.argv[1]): + if len(args) == 1: + if find_host(args[0], oneline=oneline): sys.exit(0) else: - if find_host(sys.argv[1], sys.argv[2]): + if find_host(args[0], args[1], oneline=oneline): sys.exit(0) - print("Host '%s' could not be found!" % sys.argv[1]) + print("Host '%s' could not be found!" % args[0]) sys.exit(1) -- cgit v1.2.3-1-g7c22