Documented the format options a bit and made them public.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent d5e2fbf · Browse files · 3 files·+57 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+57 added−39 removed
- other:+57 added−39 removed
Provenance
- Commit
373d92d- Tree
58c4736- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d5e2fbf
3 files changed+57 added−39 removed
modified:click/core.py+54 added-36 removed
156 unmodified lines157 def exit(self, code=0):158 """Exits the application with a given exit code."""159 sys.exit(code)160 161- def format_usage(self):161+ def get_usage(self):162 """Helper method to get formatted usage string for the current163 context and command.164 """165- return self.command.format_usage(self).rstrip('\n')165+ return self.command.get_usage(self)166 167- def format_help(self):167+ def get_help(self):168 """Helper method to get formatted help page for the current169 context and command.170 """171- return self.command.format_help(self).rstrip('\n')171+ return self.command.get_help(self)172 173 def invoke(*args, **kwargs):174 """Invokes a command callback in exactly the way it expects.175 """92 unmodified lines268 for param in self.iter_params_for_processing():269 yield param.name270 271 def make_parser(self, ctx):272+ """Creates the underlying option parser for this command."""273 parser = OptionParser(ctx)274 for param in self.params:275 param.add_to_parser(parser, ctx)276 return parser277 277- def format_usage(self, ctx):278- """Formats the usage line."""279- formatter = HelpFormatter()280- self._format_usage(ctx, formatter)281- return formatter.getvalue()278+ def get_usage(self, ctx):279+ """Formats the usage line into a string and returns it. This280+ creates a formatter and will call into the following formatting281+ methods:282 283- def format_help(self, ctx):284- """Formats the help."""283+ - :meth:`format_usage`284+ """285 formatter = HelpFormatter()286- self._format(ctx, formatter)287- return formatter.getvalue()286+ self.format_usage(ctx, formatter)287+ return formatter.getvalue().rstrip('\n')288 289- def _format(self, ctx, formatter):290- self._format_usage(ctx, formatter)291- self._format_help(ctx, formatter)292- self._format_options(ctx, formatter)293- self._format_epilog(ctx, formatter)289+ def get_help(self, ctx):290+ """Formats the help into a string and returns it. This creates a291+ formatter and will call into the following formatting methods:292 295- def _collect_usage_pieces(self, ctx):296- rv = [self.options_metavar]297- for param in self.params:298- rv.extend(param.get_usage_pieces(ctx))299- return rv293+ - :meth:`format_usage`294+ - :meth:`format_help`295+ - :meth:`format_options`296+ - :meth:`format_epilog`297+ """298+ formatter = HelpFormatter()299+ self.format_usage(ctx, formatter)300+ self.format_help(ctx, formatter)301+ self.format_options(ctx, formatter)302+ self.format_epilog(ctx, formatter)303+ return formatter.getvalue().rstrip('\n')304 301- def _format_usage(self, ctx, formatter):302- pieces = self._collect_usage_pieces(ctx)305+ def format_usage(self, ctx, formatter):306+ """Writes the usage line into the formatter."""307+ pieces = self.collect_usage_pieces(ctx)308 formatter.write_usage(ctx.command_path, ' '.join(pieces))309 305- def _format_help(self, ctx, formatter):310+ def format_help(self, ctx, formatter):311+ """Writes the help into the formatter if it exists."""312 if self.help:313 formatter.write_paragraph()314 with formatter.indentation():315 formatter.write_text(self.help)316 311- def _format_options(self, ctx, formatter):317+ def format_options(self, ctx, formatter):318+ """Writes all the options into the formatter if they exist."""319 opts = []320 for param in self.params:321 rv = param.get_help_record(ctx)322 if rv is not None:2 unmodified lines325 if opts:326 with formatter.section('Options'):327 formatter.write_dl(opts)328 322- def _format_epilog(self, ctx, formatter):329+ def format_epilog(self, ctx, formatter):330+ """Writes the epilog into the formatter if it exists."""331 if self.epilog:332 formatter.write_paragraph()333 with formatter.indentation():334 formatter.write_text(self.epilog)335 336+ def collect_usage_pieces(self, ctx):337+ """Returns all the pieces that go into the usage line."""338+ rv = [self.options_metavar]339+ for param in self.params:340+ rv.extend(param.get_usage_pieces(ctx))341+ return rv342+343 def make_context(self, info_name, args, parent=None, **extra):344 """This function when given an info name and arguments will kick345 off the parsing and create a new :class:`Context`. It does not346 invoke the actual command callback though.81 unmodified lines428 echo(file=sys.stderr)429 raise Abort()430 except UsageError as e:431 if e.ctx is not None:417- echo(e.ctx.format_usage() + '\n', file=sys.stderr)432+ echo(e.ctx.get_usage() + '\n', file=sys.stderr)433 echo('Error: %s' % e.message, file=sys.stderr)434 sys.exit(2)435 except Abort:436 echo('Aborted!', file=sys.stderr)37 unmodified lines474 parser = Command.make_parser(self, ctx)475 parser.allow_interspersed_args = False476 return parser477 463- def _collect_usage_pieces(self, ctx):464- rv = Command._collect_usage_pieces(self, ctx)478+ def collect_usage_pieces(self, ctx):479+ rv = Command.collect_usage_pieces(self, ctx)480 rv.append(self.subcommand_metavar)481 return rv482 468- def _format_options(self, ctx, formatter):469- Command._format_options(self, ctx, formatter)470- self._format_commands(ctx, formatter)483+ def format_options(self, ctx, formatter):484+ Command.format_options(self, ctx, formatter)485+ self.format_commands(ctx, formatter)486 472- def _format_commands(self, ctx, formatter):487+ def format_commands(self, ctx, formatter):488+ """Extra format methods for multi methods that adds all the commands489+ after the options.490+ """491 rows = []492 for subcommand in self.list_commands(ctx):493 cmd = self.get_command(ctx, subcommand)494 # What is this, the tool lied about a command. Ignore it11 unmodified lines506 if not ctx.args:507 if self.invoke_without_command:508 return Command.invoke(self, ctx)509 elif self.no_args_is_help:492- echo(ctx.format_help())510+ echo(ctx.get_help())511 ctx.exit()512 ctx.fail('Missing command')513 514 cmd_name = make_str(ctx.args[0])510 unmodified linesmodified:click/decorators.py+1 added-1 removed
248 unmodified lines249 """250 def decorator(f):251 def callback(ctx, value):252 if value:253- echo(ctx.format_help())253+ echo(ctx.get_help())254 ctx.exit()255 attrs.setdefault('is_flag', True)256 attrs.setdefault('expose_value', False)257 attrs.setdefault('help', 'Show this message and exit.')8 unmodified linesmodified:click/formatting.py+2 added-2 removed
110 unmodified lines111 112 def dedent(self):113 self.current_indent -= self.indent_increment114 115- def write_usage(self, prog, args=''):116- prefix = '%*s%s' % (self.current_indent, 'Usage: ', prog)115+ def write_usage(self, prog, args='', prefix='Usage: '):116+ prefix = '%*s%s' % (self.current_indent, prefix, prog)117 self.buffer.append(prefix)118 119 text_width = max(self.width - self.current_indent - len(prefix), 10)120 indent = ' ' * (len(prefix) + 1)63 unmodified lines