summaryrefslogtreecommitdiff
path: root/panfry/cli.py
blob: 650b7243da640ed4a03b67c25b49fd12c211b47a (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#
# 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
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'

  doc_path:
    flags: [-s, --docdir]
    default: '.'
    help: 'Directory where document content 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'

  pandoc_options:
    flags: [-O, --pandoc-options]
    default: ''
    help: |
        Space delimited list of options to pass to Pandoc.
        Options must be in the long format without the leading
        double dash.
        
        Example --pandoc-options="smart base-header-level=2"

  json_toc:
    flags: [--json-toc]
    action: store_true
    default: False
    help: |
        Generate toc.json file instead of toc integrated in
        the html

  port:
    flags: [-P, --port]
    default: 8080
    help: 'HTTP port'



Parser:
  help:
  args:
    - debug_enabled
    - run_quiet
    - logfile

Subparsers:
  gen:
    help: generate document from source
    args:
      - doc_path
      - pub_path
      - templates_path
      - simple_toc
      - json_toc
      - assets
      - clean
      - css
      - pandoc_options

  serve:
    help: start simple http server in current directory
    args:
      - port
      - pub_path
"""

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