Guest User

Untitled

a guest
Nov 15th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. """
  2. Example Generate basic documeentation for arguments. :)
  3. python generate_doc.py -md > doc.md
  4. """
  5. import argparse
  6.  
  7. def add_md_help_argument(parser):
  8. """ md help parser """
  9. parser.add_argument('-md', action=MarkdownHelpAction,
  10. help='print Markdown-formatted help text and exit.')
  11.  
  12.  
  13. # MARKDOWN boilerplate
  14.  
  15. # Copyright 2016 The Chromium Authors. All rights reserved.
  16. # Use of this source code is governed by a BSD-style license that can be
  17. # found in the LICENSE file.
  18. class MarkdownHelpFormatter(argparse.HelpFormatter):
  19. """A really bare-bones argparse help formatter that generates valid markdown.
  20. This will generate something like:
  21. usage
  22. # **section heading**:
  23. ## **--argument-one**
  24. ```
  25. argument-one help text
  26. ```
  27. """
  28.  
  29. def _format_usage(self, usage, actions, groups, prefix):
  30. return ""
  31.  
  32. def format_help(self):
  33. print(self._prog)
  34. self._root_section.heading = '# Options: %s' % self._prog
  35. return super(MarkdownHelpFormatter, self).format_help()
  36.  
  37. def start_section(self, heading):
  38. super(MarkdownHelpFormatter, self) \
  39. .start_section('### **%s**' % heading)
  40.  
  41. def _format_action(self, action):
  42. if action.dest == "help" or action.dest == "md":
  43. return ""
  44. lines = []
  45. lines.append('* **-%s %s** ' % (action.dest,
  46. "[%s]" % action.default
  47. if action.default else "[]"))
  48. if action.help:
  49. help_text = self._expand_help(action)
  50. lines.extend(self._split_lines(help_text, 80))
  51. lines.extend(['', ''])
  52. return '\n'.join(lines)
  53.  
  54.  
  55. class MarkdownHelpAction(argparse.Action):
  56. """ MD help action """
  57.  
  58. def __init__(self, option_strings,
  59. dest=argparse.SUPPRESS, default=argparse.SUPPRESS,
  60. **kwargs):
  61. super(MarkdownHelpAction, self).__init__(
  62. option_strings=option_strings,
  63. dest=dest,
  64. default=default,
  65. nargs=0,
  66. **kwargs)
  67.  
  68. def __call__(self, parser, namespace, values, option_string=None):
  69. parser.formatter_class = MarkdownHelpFormatter
  70. parser.print_help()
  71. parser.exit()
  72.  
  73. def model_opts(parser):
  74. """
  75. Example args
  76. """
  77. parser.add_argument('-rnn_size', type=int, default=500,
  78. help='Size of LSTM hidden states')
  79. parser.add_argument('-emb_size', type=int, default=200,
  80. help='Size of embedding layer')
  81. parser.add_argument('-rnn_type', type=str, default='LSTM',
  82. choices=['LSTM', 'GRU'],
  83. help="""The gate type to use in the RNNs""")
  84.  
  85. def main():
  86. parser = argparse.ArgumentParser(description='train.py')
  87. model_opts(parser)
  88. add_md_help_argument(parser)
  89. options = parser.parse_args()
  90.  
  91. if __name__ == '__main__':
  92. main()
Add Comment
Please, Sign In to add comment