|
| 1 | +""" |
| 2 | +File uses code adapted from code with the following license: |
| 3 | +
|
| 4 | +Copyright (c) 2015-2023, Heungsub Lee |
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without modification, |
| 8 | +are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | + Redistributions of source code must retain the above copyright notice, this |
| 11 | + list of conditions and the following disclaimer. |
| 12 | +
|
| 13 | + Redistributions in binary form must reproduce the above copyright notice, this |
| 14 | + list of conditions and the following disclaimer in the documentation and/or |
| 15 | + other materials provided with the distribution. |
| 16 | +
|
| 17 | + Neither the name of the copyright holder nor the names of its |
| 18 | + contributors may be used to endorse or promote products derived from |
| 19 | + this software without specific prior written permission. |
| 20 | +
|
| 21 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 22 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 25 | +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 28 | +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +""" |
| 32 | + |
| 33 | +__all__ = ["DefaultGroupHandler"] |
| 34 | + |
| 35 | +import collections.abc as cabc |
| 36 | + |
| 37 | +import click |
| 38 | + |
| 39 | + |
| 40 | +class DefaultGroupHandler(click.Group): |
| 41 | + """ |
| 42 | + Allows the migration to a new sub-command by allowing the group to run |
| 43 | + one of its sub-commands as the no-args default command. |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self, *args, **kwargs): |
| 47 | + # To resolve as the default command. |
| 48 | + if not kwargs.get("ignore_unknown_options", True): |
| 49 | + raise ValueError("Default group accepts unknown options") |
| 50 | + self.ignore_unknown_options = True |
| 51 | + self.default_cmd_name = kwargs.pop("default", None) |
| 52 | + self.default_if_no_args = kwargs.pop("default_if_no_args", False) |
| 53 | + super().__init__(*args, **kwargs) |
| 54 | + |
| 55 | + def parse_args(self, ctx, args): |
| 56 | + if not args and self.default_if_no_args: |
| 57 | + args.insert(0, self.default_cmd_name) |
| 58 | + return super().parse_args(ctx, args) |
| 59 | + |
| 60 | + def get_command(self, ctx, cmd_name): |
| 61 | + if cmd_name not in self.commands: |
| 62 | + # If it doesn't match an existing command, use the default command name. |
| 63 | + ctx.arg0 = cmd_name |
| 64 | + cmd_name = self.default_cmd_name |
| 65 | + return super().get_command(ctx, cmd_name) |
| 66 | + |
| 67 | + def resolve_command(self, ctx, args): |
| 68 | + cmd_name, cmd, args = super().resolve_command(ctx, args) |
| 69 | + if hasattr(ctx, "arg0"): |
| 70 | + args.insert(0, ctx.arg0) |
| 71 | + cmd_name = cmd.name |
| 72 | + return cmd_name, cmd, args |
| 73 | + |
| 74 | + def format_commands(self, ctx, formatter): |
| 75 | + """ |
| 76 | + Used to wrap the default formatter to clarify which command is the default. |
| 77 | + """ |
| 78 | + formatter = DefaultCommandFormatter(self, formatter, mark=" (default)") |
| 79 | + return super().format_commands(ctx, formatter) |
| 80 | + |
| 81 | + |
| 82 | +class DefaultCommandFormatter: |
| 83 | + """ |
| 84 | + Wraps a formatter to edit the line for the default command to mark it |
| 85 | + with the specified mark string. |
| 86 | + """ |
| 87 | + |
| 88 | + def __init__(self, group, formatter, mark="*"): |
| 89 | + self.group = group |
| 90 | + self.formatter = formatter |
| 91 | + self.mark = mark |
| 92 | + super().__init__() |
| 93 | + |
| 94 | + def __getattr__(self, attr): |
| 95 | + return getattr(self.formatter, attr) |
| 96 | + |
| 97 | + def write_dl(self, rows: cabc.Sequence[tuple[str, str]], *args, **kwargs): |
| 98 | + rows_: list[tuple[str, str]] = [] |
| 99 | + for cmd_name, help_msg in rows: |
| 100 | + if cmd_name == self.group.default_cmd_name: |
| 101 | + rows_.insert(0, (cmd_name + self.mark, help_msg)) |
| 102 | + else: |
| 103 | + rows_.append((cmd_name, help_msg)) |
| 104 | + return self.formatter.write_dl(rows_, *args, **kwargs) |
0 commit comments