summaryrefslogtreecommitdiff
path: root/panfry/cli.py
diff options
context:
space:
mode:
authorScott Bahling <sbahling@mudgum.net>2013-03-17 21:26:02 +0100
committerScott Bahling <sbahling@mudgum.net>2013-03-17 21:26:02 +0100
commitbf0b3f2a096832a9063f7816dcc14a0607f67271 (patch)
treeefff29003b44d8c471587f751740b6f9eac48478 /panfry/cli.py
downloadpanfry-bf0b3f2a096832a9063f7816dcc14a0607f67271.tar.gz
panfry-bf0b3f2a096832a9063f7816dcc14a0607f67271.tar.xz
panfry-bf0b3f2a096832a9063f7816dcc14a0607f67271.zip
initial commit
Diffstat (limited to 'panfry/cli.py')
-rwxr-xr-xpanfry/cli.py144
1 files changed, 144 insertions, 0 deletions
diff --git a/panfry/cli.py b/panfry/cli.py
new file mode 100755
index 0000000..6b4a64d
--- /dev/null
+++ b/panfry/cli.py
@@ -0,0 +1,144 @@
+#
+# Copyright (c) 2013 Scott Bahling, <sbahling@mudgum.net>
+#
+# 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
+
+__version__ = '0.0.1'
+
+#################################################################
+#
+# 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.
+
+
+Parser:
+ help:
+ args:
+ - debug_enabled
+ - run_quiet
+ - logfile
+
+Subparsers:
+ gen:
+ help: generate document from source
+ args:
+ - src_path
+ - pub_path
+ - templates_path
+ - 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