Search code examples
pythonpre-commit.com

How to run a single pre-commit hook with particular arguments that are different to the yaml definition


I know that I can run a hook using

pre-commit run <hook id>

But if <hook id> is typically run with a set of arguments, and I want to run it with a different set, how should I go about that ?

For example, I might have a black hook that just runs with no arguments, but want to run it with black --preview.

Currently if I wanted to do that I would have black managed by my project venv and pre-commits venv, which seems overkill.

I guess I'm wondering if something like the following exists:

pre-commit run --just-call-the-package black --preview

Solution

  • pre-commit is designed so that all contributors to a repository run exactly the same tools and commands -- it is not a generalized environment manager and command runner. that said you can customize the arguments for each tool using args:

    # ...
        -   id: black
            args: [--preview]
    

    though in black's case you probably instead want to use its configuration file

    if you want to have one off "commands" with a separate fixed set of arguments you can utilize the stages: [manual] stage for a hook that won't normally run (but can be run manually)


    disclaimer: I wrote pre-commit