Skip to content

feat: allow hyphens in custom command names and aliases #8

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

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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