Auto-Register Commands

Hard ⏱ 18 min 28% acceptance ★★★★★ 4.6
CLI frameworks auto-discover commands via decorators. Build a module-level dict COMMANDS and the decorator command(name) that registers the function under name and returns it unmodified (registration decorators don't wrap). Then dispatch(name, *args) looks up and calls the command, raising KeyError naturally for unknown names.

Examples

Example 1
Input
@command('greet') on hello; dispatch('greet', 'Ana')
Output
hello('Ana')'s result
Explanation

The decorator filed the function; dispatch found it.

Constraints

  • The decorator returns func itself.
  • COMMANDS maps name → function.

Topics

Decoratorsregistry pattern

Companies

HashiCorpDockerRed Hat

Hints

Hint 1

Registration happens at import time.

Hint 2

return func — no wrapper layer.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.