From c8d25ebc5cdf06cbade8ea89dd2a93c522a2660c Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Wed, 21 Nov 2012 10:57:57 +0800 Subject: [PATCH] Better way of outputting a variable from info command --- bash_completion | 2 +- moodle-info.py | 49 ++++++++++++++++++++++++------------------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/bash_completion b/bash_completion index a3713ac..29ed0ac 100644 --- a/bash_completion +++ b/bash_completion @@ -98,7 +98,7 @@ function _moodle() { fi ;; info) - OPTS="--list" + OPTS="--list --var" if [[ "${COMP_CWORD}" == 2 && "$CUR" != -* ]]; then OPTS="$OPTS $($BIN info -ln)" elif [[ "${COMP_CWORD}" == 3 && ("$PREV" == "--list" || "$PREV" == "-l") ]]; then diff --git a/moodle-info.py b/moodle-info.py index 155b494..8d3190a 100755 --- a/moodle-info.py +++ b/moodle-info.py @@ -24,9 +24,8 @@ http://github.com/FMCorz/mdk import sys import argparse -from lib import workplace, moodle, tools +from lib import workplace from lib.tools import debug -from lib.config import C Wp = workplace.Workplace() @@ -36,36 +35,36 @@ parser.add_argument('-l', '--list', action='store_true', help='list the instance parser.add_argument('-i', '--integration', action='store_true', help='used with --list, only display integration instances', dest='integration') parser.add_argument('-s', '--stable', action='store_true', help='used with --list, only display stable instances', dest='stable') parser.add_argument('-n', '--name-only', action='store_true', help='used with --list, only display instances name', dest='nameonly') +parser.add_argument('-v', '--var', metavar='var', default=None, nargs='?', help='variable to output. Does not work with --list.') parser.add_argument('name', metavar='name', default=None, nargs='?', help='name of the instance') -parser.add_argument('var', metavar='var', default=None, nargs='?', help='variable to output') args = parser.parse_args() # List the instances if args.list: - if args.integration != False or args.stable != False: - l = Wp.list(integration = args.integration, stable = args.stable) - else: - l = Wp.list() - l.sort() - for i in l: - if not args.nameonly: - M = Wp.get(i) - print '{0:<25}'.format(i), M.get('release') - else: - print i + if args.integration != False or args.stable != False: + l = Wp.list(integration=args.integration, stable=args.stable) + else: + l = Wp.list() + l.sort() + for i in l: + if not args.nameonly: + M = Wp.get(i) + print '{0:<25}'.format(i), M.get('release') + else: + print i # Loading instance else: - M = Wp.resolve(args.name) - if not M: - debug('This is not a Moodle instance') - sys.exit(1) + M = Wp.resolve(args.name) + if not M: + debug('This is not a Moodle instance') + sys.exit(1) - # Printing variable - if args.var != None: - print M.get(args.var) + # Printing variable + if args.var != None: + print M.get(args.var) - # Printing info - else: - for key, info in M.info().items(): - print '{0:<20}: {1}'.format(key, info) + # Printing info + else: + for key, info in M.info().items(): + print '{0:<20}: {1}'.format(key, info) -- 2.11.0