Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CommandLine command registration #1611

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mikeroll
Copy link

Fixes: #1610

Refactors CommandLine internals so that it's easier to plug custom commands into the cli, in addition to the builtins alembic.command.
No public interface changes whatsoever.

The topic was somewhat discussed here #1456

Description

The point from the linked discussion still applies - this does not enable any kind of plugin system, one would still need to have their own subclass of CommandLine and call it from their own script. However, the implementation of such a subclass should be much cleaner now. I would expect it to look like so:

def frobnicate(config: Config, revision: str) -> None:
    """Frobnicates according to the frobnication specification.

    :param config: a :class:`.Config` instance
    :param revision: the revision to frobnicate
    """

    config.print_stdout(f"Revision {revision} successfully frobnicated.")


class MyCommandLine(CommandLine):
    def _generate_args(self, prog: str | None) -> None:
        super()._generate_args(prog)

        self._register_command(frobnicate)


if __name__ == "__main__":
    MyCommandLine().main()
❯ python cli.py frobnicate 42
Revision 42 successfully frobnicated.

Checklist

This pull request is:

  • A short code fix
    • please include the issue number, and create an issue if none exists, which
      must include a complete example of the issue. one line code fixes without an
      issue and demonstration will not be accepted.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests. one line code fixes without tests will not be accepted.

This is purely internal refactoring, so no new tests are required. For extra confidence I:

  • ran tests with tox locally;
  • compared the text of alembic --help before and after this change and made sure there was no diff.

Have a nice day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CommandLine should be more easily extendable with custom commands
1 participant