Show More
@@ -51,18 +51,57 b' Inheritance diagram:' | |||
|
51 | 51 | # The full license is in the file COPYING.txt, distributed with this software. |
|
52 | 52 | #----------------------------------------------------------------------------- |
|
53 | 53 | import argparse |
|
54 | import re | |
|
54 | 55 | |
|
55 | 56 | # Our own imports |
|
56 | 57 | from IPython.core.error import UsageError |
|
58 | from IPython.utils.decorators import undoc | |
|
57 | 59 | from IPython.utils.process import arg_split |
|
58 | 60 | from IPython.utils.text import dedent |
|
59 | 61 | |
|
62 | NAME_RE = re.compile(r"[a-zA-Z][a-zA-Z0-9_-]*$") | |
|
63 | ||
|
64 | @undoc | |
|
60 | 65 | class MagicHelpFormatter(argparse.RawDescriptionHelpFormatter): |
|
61 | """ A HelpFormatter which dedents but otherwise preserves indentation. | |
|
66 | """A HelpFormatter with a couple of changes to meet our needs. | |
|
62 | 67 | """ |
|
68 | # Modified to dedent text. | |
|
63 | 69 | def _fill_text(self, text, width, indent): |
|
64 | 70 | return argparse.RawDescriptionHelpFormatter._fill_text(self, dedent(text), width, indent) |
|
65 | 71 | |
|
72 | # Modified to wrap argument placeholders in <> where necessary. | |
|
73 | def _format_action_invocation(self, action): | |
|
74 | if not action.option_strings: | |
|
75 | metavar, = self._metavar_formatter(action, action.dest)(1) | |
|
76 | return metavar | |
|
77 | ||
|
78 | else: | |
|
79 | parts = [] | |
|
80 | ||
|
81 | # if the Optional doesn't take a value, format is: | |
|
82 | # -s, --long | |
|
83 | if action.nargs == 0: | |
|
84 | parts.extend(action.option_strings) | |
|
85 | ||
|
86 | # if the Optional takes a value, format is: | |
|
87 | # -s ARGS, --long ARGS | |
|
88 | else: | |
|
89 | default = action.dest.upper() | |
|
90 | args_string = self._format_args(action, default) | |
|
91 | # IPYTHON MODIFICATION: If args_string is not a plain name, wrap | |
|
92 | # it in <> so it's valid RST. | |
|
93 | if not NAME_RE.match(args_string): | |
|
94 | args_string = "<%s>" % args_string | |
|
95 | for option_string in action.option_strings: | |
|
96 | parts.append('%s %s' % (option_string, args_string)) | |
|
97 | ||
|
98 | return ', '.join(parts) | |
|
99 | ||
|
100 | # Override the default prefix ('usage') to our % magic escape, | |
|
101 | # in a code block. | |
|
102 | def add_usage(self, usage, actions, groups, prefix="::\n\n %"): | |
|
103 | super(MagicHelpFormatter, self).add_usage(usage, actions, groups, prefix) | |
|
104 | ||
|
66 | 105 | class MagicArgumentParser(argparse.ArgumentParser): |
|
67 | 106 | """ An ArgumentParser tweaked for use by IPython magics. |
|
68 | 107 | """ |
@@ -113,15 +152,8 b' def construct_parser(magic_func):' | |||
|
113 | 152 | if result is not None: |
|
114 | 153 | group = result |
|
115 | 154 | |
|
116 | # Replace the starting 'usage: ' with IPython's %. | |
|
117 | help_text = parser.format_help() | |
|
118 | if help_text.startswith('usage: '): | |
|
119 | help_text = help_text.replace('usage: ', '%', 1) | |
|
120 | else: | |
|
121 | help_text = '%' + help_text | |
|
122 | ||
|
123 | 155 | # Replace the magic function's docstring with the full help text. |
|
124 |
magic_func.__doc__ = |
|
|
156 | magic_func.__doc__ = parser.format_help() | |
|
125 | 157 | |
|
126 | 158 | return parser |
|
127 | 159 |
General Comments 0
You need to be logged in to leave comments.
Login now