Routing
/cliapp$ wooks cli --routing|
Wooks CLI provides a powerful routing system that allows you to define and handle command-line interface (CLI) commands with ease. This documentation will guide you through the process of defining routes, handling arguments, and working with options in Wooks CLI.
INFO
Wooks utilizes @prostojs/router for routing, and its documentation is partially included here for easy reference.
Routing Basics
In Wooks CLI, routing is the process of mapping CLI commands to their respective handlers. A route consists of a command pattern. The command pattern defines the structure of the command, including the command name and arguments.
Command Structure
Wooks CLI represents commands as paths due to the underlying router used. For example, to define the command npm install @wooksjs/event-cli, you can use the following command pattern:
'/install/:package'In the above pattern, :package represents a variable. Alternatively, you can use a space as a separator, like this:
'install :package'Both command patterns serve the same purpose.
If you need to use a colon in your command, it must be escaped with a backslash (\). For example:
'app build\\:dev'The above command pattern allows the command to be executed as follows:
my-cli app build:devOptional Parameters
You can define optional route parameter
'app build :target?'In the example above parameter target will be optional.
my-cli app buildmy-cli app build my-targetBoth commands will be handled by app build :target? pattern. In the first scenario parameter target will be undefined. In the second scenario parameter target will get my-target value.
Gotchas
- Commands are matched as router paths: each positional argument becomes a path segment. A
/inside an argument value does not break the match —run()encodes it as%2F, so the value still binds to a single:paramand the handler receives the original value.
Next Steps
- Command Options — declare and read
--options - Command Usage (Help) — add descriptions, args, aliases, and examples to commands