summaryrefslogtreecommitdiff
path: root/panfry/cli.py
blob: 6b4a64d48b49d2790f8627b9f09b0158a724374e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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