# # Copyright (c) 2013 Scott Bahling, # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program (see the file COPYING); if not, write to the # Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA # ################################################################ # # cli.py # command line argument handling import yaml import argparse import copy from panfry import __version__ ################################################################# # # The argparse setup is mapped out # in a yaml based config called cli_config. The init_argparse # methd uses the cli_config to setup the main and sub command # parsers. # ################################################################# ################################################################# # Argument Parser configuration (YAML) ################################################################# cli_config = """ Args: debug_enabled: flags: [-d, --debug] action: store_true default: False help: 'Turn on verbose messages.' run_quiet: flags: [-q, --quiet] action: store_true default: False help: 'Turn off all (debug, warn, info) messages.' logfile: flags: [-L, --logfile] default: '' help: 'File to write logs to. Default ./pldptools.log' pub_path: flags: [-p, --pubdir] default: './pub' help: 'Directory to place generated documents' src_path: flags: [-s, --srcdir] default: '.' help: 'Directory where document source is located' templates_path: flags: [-T, --templates] default: '' help: 'Directory where document templates are located' css: flags: [-C, --css] default: 'css/style.css' help: | css file for html pages. Includes full path relative to html directory. simple_toc: flags: [--simple-toc] action: store_true default: False help: 'Do not generate full level table of contents' assets: flags: [--assets] action: store_true default: False help: 'Only copy assets to pub (do not regenerate document)' clean: flags: [--clean] action: store_true default: False help: 'Remove pub path and all contents before generating' Parser: help: args: - debug_enabled - run_quiet - logfile Subparsers: gen: help: generate document from source args: - src_path - pub_path - templates_path - simple_toc - assets - clean - css """ config = yaml.load(cli_config) parser_config = config.get('Parser', {}) args = config.get('Args', {}) subs = config.get('Subparsers', {}) def _add_arg(parser, argtype): opts = copy.copy(args.get(argtype)) if opts: opts.setdefault('dest', argtype) flags = opts.pop('flags', []) parser.add_argument(*flags, **opts) else: print "Unknown argument type: %s" % argtype def init_argparser(): description = parser_config.get('description', '') formatter_class = argparse.RawTextHelpFormatter parser = argparse.ArgumentParser(description=description, formatter_class=formatter_class, ) subparsers = parser.add_subparsers() version = 'Panfry version: %s' % __version__ parser.add_argument('--version', action='version', version=version) # setup main parser for item in config['Parser'].get('args', []): _add_arg(parser, item) # setup sub-command parsers for sub, conf in subs.items(): subparser = subparsers.add_parser(sub, help=conf.get('help', ''), formatter_class=formatter_class, ) subparser.set_defaults(cmd=sub) for item in conf.get('args', []): _add_arg(subparser, item) return parser