Move argparser to powerline.shell so it can be re-used

This commit is contained in:
Kim Silkebækken 2013-03-11 07:52:24 +01:00
parent 59ef974b29
commit 40fe38f641
2 changed files with 21 additions and 18 deletions

View File

@ -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

View File

@ -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: