def format_options(self, ctx: Context, formatter: HelpFormatter) -> None: opts_by_group = collections.defaultdict(list) for param in self.get_params(ctx): rv = param.get_help_record(ctx) if rv is not None: opts_by_group[getattr(param, 'help_group', None)].append(rv) for group_name, opts in sorted(opts_by_group.items(), key=lambda pair: (pair[0] or '')): with formatter.section(group_name or 'Options'): formatter.write_dl(opts)
def format_commands(self, ctx: Context, formatter: HelpFormatter) -> None: rows_by_prefix = defaultdict(list) for trail, command in self._get_all_commands(ctx): prefix = (' '.join(trail[:1]) if len(trail) > 1 else '') help = (command.short_help or command.help or '').partition('\n')[0] rows_by_prefix[prefix.strip()].append( (' '.join(trail).strip(), help)) for prefix, rows in sorted(rows_by_prefix.items()): title = (f'Commands ({prefix} ...)' if prefix else 'Commands') with formatter.section(title): formatter.write_dl(rows)
def list_commands(): global _gbins if not _gbins: _gbins = GBin().get_bins() formatter = HelpFormatter() sorted_bins = [(name, gbin.doc) for name, gbin in sorted(_gbins.items(), key=lambda x: x[0]) ] if os.isatty(1): doc_groups = itertools.groupby(sorted_bins, key=lambda x: x[0].split('.')[0]) else: doc_groups = [(None, sorted_bins)] with formatter.section(click.style('Commands', fg='green')): for group, doc_pairs in doc_groups: if group: with formatter.section(click.style(group, fg='blue')): formatter.write_dl(doc_pairs or [tuple()]) else: formatter.write_dl(doc_pairs or [tuple()]) print formatter.getvalue()
def list_commands(): global _gbins if not _gbins: _gbins = GBin().get_bins() formatter = HelpFormatter() sorted_bins = [(name, gbin.doc) for name, gbin in sorted(_gbins.items(), key=lambda x: x[0])] if os.isatty(1): doc_groups = itertools.groupby(sorted_bins, key=lambda x: x[0].split('.')[0]) else: doc_groups = [(None, sorted_bins)] with formatter.section(click.style('Commands', fg='green')): for group, doc_pairs in doc_groups: if group: with formatter.section(click.style(group, fg='blue')): formatter.write_dl(doc_pairs or [tuple()]) else: formatter.write_dl(doc_pairs or [tuple()]) print formatter.getvalue()
def list_projects(projects): formatter = HelpFormatter() with formatter.section("Available projects"): formatter.write_dl( sorted( [(name, info['description']) for name, info in projects.items()], key=lambda x: x[0])) click.echo(formatter.getvalue().strip())
def _org_reports_help(ctx, value, header, synopsis, discussion=None, paged=True): """Print a nicely formatted help message. :param ctx click.Context: The current click Context instance :param header str: Heading for this help page :param synopsis str: A brief description of this command :param discussion str: A discussion of this command of arbitrary length Print a help page in a nicer format using ANSI escape codes. """ from click.formatting import HelpFormatter from click.termui import echo, echo_via_pager if not value or ctx.resilient_parsing: return fmt = HelpFormatter() fmt.write_text('[1m' + header + '[0m') fmt.write_paragraph() pieces = ctx.command.collect_usage_pieces(ctx) fmt.write_usage(ctx.command_path, ' '.join(pieces), '[1mUsage[0m\n\n') fmt.write_paragraph() with fmt.indentation(): fmt.write_text(synopsis) ctx.command.format_options(ctx, fmt) fmt.write_paragraph() if discussion: fmt.write_text('[1mDiscussion[0m') fmt.write_paragraph() with fmt.indentation(): fmt.write_text(discussion) if paged: echo_via_pager(fmt.getvalue(), ctx.color) else: echo(fmt.getvalue(), ctx.color) ctx.exit()
def _write_usage(command, args, max_width=80): hf = HelpFormatter(width=max_width) hf.write_usage(command, args) return hf.getvalue()[:-1]