From 40fe38f64104799722d5caa18e29dedd2985c5af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Mon, 11 Mar 2013 07:52:24 +0100 Subject: [PATCH] Move argparser to powerline.shell so it can be re-used --- powerline/shell.py | 19 ++++++++++++++++++- scripts/powerline | 20 +++----------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/powerline/shell.py b/powerline/shell.py index d394b7ec..713a6575 100644 --- a/powerline/shell.py +++ b/powerline/shell.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from powerline import Powerline -from powerline.lib import mergedicts +from powerline.lib import mergedicts, parsedotval def mergeargs(argvalue): @@ -39,3 +39,20 @@ class ShellPowerline(Powerline): return [self.args.config_path] else: return super(ShellPowerline, self).get_config_paths() + + +def get_argparser(parser=None, *args, **kwargs): + if not parser: + import argparse + parser = argparse.ArgumentParser + p = parser(*args, **kwargs) + p.add_argument('ext', nargs=1) + p.add_argument('side', nargs='?', choices=('left', 'right')) + p.add_argument('-r', '--renderer_module', metavar='MODULE', type=str) + p.add_argument('-w', '--width', type=int) + p.add_argument('--last_exit_code', metavar='INT', type=int) + p.add_argument('--last_pipe_status', metavar='LIST', default='', type=lambda s: [int(status) for status in s.split()]) + p.add_argument('-c', '--config', metavar='KEY.KEY=VALUE', type=parsedotval, action='append') + p.add_argument('-t', '--theme_option', metavar='THEME.KEY.KEY=VALUE', type=parsedotval, action='append') + p.add_argument('-p', '--config_path', metavar='PATH') + return p diff --git a/scripts/powerline b/scripts/powerline index f43b7bab..01a74892 100755 --- a/scripts/powerline +++ b/scripts/powerline @@ -1,31 +1,17 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- '''Powerline prompt and statusline script.''' -import argparse import sys -import json -from powerline.lib import parsedotval try: - from powerline.shell import ShellPowerline + from powerline.shell import ShellPowerline, get_argparser except ImportError: import os sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - from powerline.shell import ShellPowerline # NOQA - -parser = argparse.ArgumentParser(description=__doc__) -parser.add_argument('ext', nargs=1) -parser.add_argument('side', nargs='?', choices=('left', 'right')) -parser.add_argument('-r', '--renderer_module', metavar='MODULE', type=str) -parser.add_argument('-w', '--width', type=int) -parser.add_argument('--last_exit_code', metavar='INT', type=int) -parser.add_argument('--last_pipe_status', metavar='LIST', default='', type=lambda s: [int(status) for status in s.split()]) -parser.add_argument('-c', '--config', metavar='KEY.KEY=VALUE', type=parsedotval, action='append') -parser.add_argument('-t', '--theme_option', metavar='THEME.KEY.KEY=VALUE', type=parsedotval, action='append') -parser.add_argument('-p', '--config_path', metavar='PATH') + from powerline.shell import ShellPowerline, get_argparser # NOQA if __name__ == '__main__': - args = parser.parse_args() + args = get_argparser(description=__doc__).parse_args() powerline = ShellPowerline(args) rendered = powerline.renderer.render(width=args.width, side=args.side) try: