Skip to content

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:

js
'/install/:package'

In the above pattern, :package represents a variable. Alternatively, you can use a space as a separator, like this:

js
'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:

js
'app build\\:dev'

The above command pattern allows the command to be executed as follows:

bash
my-cli app build:dev

Optional Parameters ​

You can define optional route parameter

js
'app build :target?'

In the example above parameter target will be optional.

bash
my-cli app build
bash
my-cli app build my-target

Both 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 :param and the handler receives the original value.

Next Steps ​

Released under the MIT License.