Skip to content

Commit

Permalink
feat: allow hyphens in custom command names and aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
paduszyk committed Sep 25, 2024
1 parent 1558c0f commit 4603805
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
18 changes: 9 additions & 9 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ Example:

```python
MANAGEMENT_COMMANDS_PATHS = {
"mycommand": "mysite.commands.MyCommand",
"my-command": "mysite.commands.MyCommand",
}
```

You can now run the custom command `mycommand` implemented in the `MyCommand` class
You can now run the custom command `my-command` implemented in the `MyCommand` class
from the `mysite.commands` module:

```console
python manage.py mycommand
python manage.py my-command
```

> In Django, the class representing a command must be named `Command`. The plugin
Expand All @@ -141,8 +141,8 @@ python manage.py mycommand
**Important Notes:**

- All keys and values must be valid Python identifiers and absolute dotted paths,
respectively.
- All keys and values must be valid Python identifiers (with hyphens allowed) and
absolute dotted paths, respectively.
- Paths must point to command classes, not modules.
- Commands must subclass `django.core.management.base.BaseCommand`.
- This setting takes precedence over others when discovering commands.
Expand Down Expand Up @@ -214,26 +214,26 @@ Example:

```python
MANAGEMENT_COMMANDS_ALIASES = {
"fullcheck": [
"full-check": [
"check --fail-level ERROR --deploy",
"makemigrations --check --dry-run --no-input",
"migrate --no-input",
],
}
```

You can now execute all the commands aliased by `fullcheck` with a single command:
You can now execute all the commands aliased by `full-check` with a single command:

```console
python manage.py fullcheck
python manage.py full-check
```

Aliases can refer to commands defined in the `MANAGEMENT_COMMANDS_PATHS` setting
or other aliases.

**Important Notes:**

- Keys must be valid Python identifiers.
- Keys must be valid Python identifiers (with hyphens allowed).
- Values should be command expressions with parsable arguments and options.
- Circular references within aliases are not allowed, as they lead to infinite recursion.

Expand Down
6 changes: 3 additions & 3 deletions src/management_commands/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def _is_identifier(s: str) -> bool:
return s.isidentifier() and not iskeyword(s)
return s.replace("-", "_").isidentifier() and not iskeyword(s)


def _is_dotted_path(s: str, /, *, min_parts: int = 0) -> bool:
Expand Down Expand Up @@ -53,7 +53,7 @@ def configure_paths(self, setting_value: dict[str, str]) -> dict[str, str]:
if not _is_identifier(key):
msg = (
f"invalid key {key!r} in PATHS; "
f"keys must be valid Python identifiers"
f"keys must be valid Python identifiers (with hyphens allowed)"
)

raise self.improperly_configured(msg, "paths.key")
Expand Down Expand Up @@ -103,7 +103,7 @@ def configure_aliases(
if not _is_identifier(key):
msg = (
f"invalid key {key!r} in ALIASES; "
f"keys must be valid Python identifiers"
f"keys must be valid Python identifiers (with hyphens allowed)"
)

raise self.improperly_configured(msg, "aliases.key")
Expand Down

0 comments on commit 4603805

Please sign in to comment.