Namespaces allow you to group Alconna settings (like prefixes, separators, and formatters). By default, Alconna uses the "Alconna" namespace.
Creating and using a Namespace
You can create a custom Namespace and assign it to an Alconna instance, or use a context manager to apply settings to all commands created within that block.
from arclet.alconna import Alconna, namespace, Namespace, config
# Create a namespace that requires a '/' prefix
ns = Namespace("foo", prefixes=["/"])
# Use the namespace in Alconna
alc = Alconna("pip", namespace=ns)
# Or use a context manager to inject settings into all commands in the block
with namespace("bar") as np1:
np1.prefixes = ["!"]
# Commands created here will use the 'bar' namespace
Managing Namespaces
- Global Config: Use
config.namespaces["name"] = ns to register a namespace. - Default Namespace: Modify
config.default_namespace to change settings for all commands without an explicit namespace.
from arclet.alconna import Alconna, namespace, Namespace, config
ns = Namespace("foo", prefixes=["/"])
alc = Alconna("pip", Subcommand("install", Args["package", str]), namespace=ns)
with namespace("bar") as np1:
np1.prefixes = ["!"]
np1.formatter_type = ShellTextFormatter
np1.builtin_option_name["help"] = {"帮助", "-h"}
config.namespaces["foo"] = ns