From 6dc5f328f317dd627a645cd39a1164d7f3a078a0 Mon Sep 17 00:00:00 2001 From: Wojtek Porczyk Date: Wed, 24 Jun 2015 16:42:06 +0200 Subject: [PATCH] qubes/tools: ArgumentParser prepopulated with common options --- qubes/tools/__init__.py | 19 +++++++++++++++++++ qubes/tools/qubes_create.py | 6 ++---- qubes/tools/qvm_ls.py | 14 +++----------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/qubes/tools/__init__.py b/qubes/tools/__init__.py index 4143f902..0bbdc403 100644 --- a/qubes/tools/__init__.py +++ b/qubes/tools/__init__.py @@ -25,8 +25,27 @@ '''Qubes' command line tools ''' +import argparse import importlib + +# TODO --verbose, logger setup +def get_parser_base(*args, **kwargs): + '''Get base parser with options common to all Qubes OS tools. + + Currently supported options: ``--xml``. + + *args* and *kwargs* are passed to :py:class:`argparser.ArgumentParser`. + ''' + parser = argparse.ArgumentParser(*args, **kwargs) + + parser.add_argument('--xml', metavar='XMLFILE', + action='store', + help='Qubes OS store file') + + return parser + + def get_parser_for_command(command): '''Get parser for given qvm-tool. diff --git a/qubes/tools/qubes_create.py b/qubes/tools/qubes_create.py index ea0906e8..78cef80c 100644 --- a/qubes/tools/qubes_create.py +++ b/qubes/tools/qubes_create.py @@ -28,11 +28,9 @@ import argparse import qubes +import qubes.tools -parser = argparse.ArgumentParser(description='Create new Qubes OS store.') -parser.add_argument('--xml', metavar='XMLFILE', - action='store', - help='Qubes OS store file') +parser = qubes.tools.get_parser_base(description='Create new Qubes OS store.') def main(args=None): '''Main routine of :program:`qubes-create`. diff --git a/qubes/tools/qvm_ls.py b/qubes/tools/qvm_ls.py index 87eb688b..3b6495ef 100644 --- a/qubes/tools/qvm_ls.py +++ b/qubes/tools/qvm_ls.py @@ -37,6 +37,7 @@ import textwrap import qubes import qubes.config +import qubes.tools import qubes.utils @@ -567,7 +568,7 @@ def get_parser(): wrapper = textwrap.TextWrapper(width=80, break_on_hyphens=False, initial_indent=' ', subsequent_indent=' ') - parser = argparse.ArgumentParser( + parser = qubes.tools.get_parser_base( formatter_class=argparse.RawTextHelpFormatter, description='List Qubes domains and their parametres.', epilog='available formats (see --help-formats):\n{}\n\n' @@ -582,7 +583,7 @@ def get_parser(): parser_formats = parser.add_mutually_exclusive_group() parser_formats.add_argument('--format', '-o', metavar='FORMAT', - action='store', choices=formats.keys(), + action='store', choices=formats.keys(), default='simple', help='preset format') parser_formats.add_argument('--fields', '-O', metavar='FIELD,...', @@ -594,15 +595,6 @@ def get_parser(): # action='store', metavar='CFGFILE', # help='Qubes config file') - parser.add_argument('--xml', metavar='XMLFILE', - action='store', - help='Qubes store file') - - parser.set_defaults( - qubesxml=os.path.join(qubes.config.system_path['qubes_base_dir'], - qubes.config.system_path['qubes_store_filename']), - format='simple') - return parser