Created
November 15, 2018 14:27
-
-
Save parajain/772e138f7604c0ecb7e57898e45a903a to your computer and use it in GitHub Desktop.
Generate basic documeentation for arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example Generate basic documeentation for arguments. :) | |
python generate_doc.py -md > doc.md | |
""" | |
import argparse | |
def add_md_help_argument(parser): | |
""" md help parser """ | |
parser.add_argument('-md', action=MarkdownHelpAction, | |
help='print Markdown-formatted help text and exit.') | |
# MARKDOWN boilerplate | |
# Copyright 2016 The Chromium Authors. All rights reserved. | |
# Use of this source code is governed by a BSD-style license that can be | |
# found in the LICENSE file. | |
class MarkdownHelpFormatter(argparse.HelpFormatter): | |
"""A really bare-bones argparse help formatter that generates valid markdown. | |
This will generate something like: | |
usage | |
# **section heading**: | |
## **--argument-one** | |
``` | |
argument-one help text | |
``` | |
""" | |
def _format_usage(self, usage, actions, groups, prefix): | |
return "" | |
def format_help(self): | |
print(self._prog) | |
self._root_section.heading = '# Options: %s' % self._prog | |
return super(MarkdownHelpFormatter, self).format_help() | |
def start_section(self, heading): | |
super(MarkdownHelpFormatter, self) \ | |
.start_section('### **%s**' % heading) | |
def _format_action(self, action): | |
if action.dest == "help" or action.dest == "md": | |
return "" | |
lines = [] | |
lines.append('* **-%s %s** ' % (action.dest, | |
"[%s]" % action.default | |
if action.default else "[]")) | |
if action.help: | |
help_text = self._expand_help(action) | |
lines.extend(self._split_lines(help_text, 80)) | |
lines.extend(['', '']) | |
return '\n'.join(lines) | |
class MarkdownHelpAction(argparse.Action): | |
""" MD help action """ | |
def __init__(self, option_strings, | |
dest=argparse.SUPPRESS, default=argparse.SUPPRESS, | |
**kwargs): | |
super(MarkdownHelpAction, self).__init__( | |
option_strings=option_strings, | |
dest=dest, | |
default=default, | |
nargs=0, | |
**kwargs) | |
def __call__(self, parser, namespace, values, option_string=None): | |
parser.formatter_class = MarkdownHelpFormatter | |
parser.print_help() | |
parser.exit() | |
def model_opts(parser): | |
""" | |
Example args | |
""" | |
parser.add_argument('-rnn_size', type=int, default=500, | |
help='Size of LSTM hidden states') | |
parser.add_argument('-emb_size', type=int, default=200, | |
help='Size of embedding layer') | |
parser.add_argument('-rnn_type', type=str, default='LSTM', | |
choices=['LSTM', 'GRU'], | |
help="""The gate type to use in the RNNs""") | |
def main(): | |
parser = argparse.ArgumentParser(description='train.py') | |
model_opts(parser) | |
add_md_help_argument(parser) | |
options = parser.parse_args() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment