Understand the Commander.js parsing lifecycle
masterCommander.js processes arguments through a hierarchical lifecycle. When a command is executed, it parses and removes the options it recognizes from the argument array, then passes the remaining arguments to the next subcommand. This continues until a leaf (final) command is reached.
Lifecycle for a command (including the top-level program):
- parse options: Recognizes and removes options belonging to the current command from the arguments.
- parse env: Checks for and applies values from environment variables configured for the command.
- process implied: Sets any implied option values.
- Subcommand delegation: If the first remaining argument is a subcommand, the command triggers
preSubcommandhooks and passes the remaining arguments to that subcommand to repeat the process.
Lifecycle for the final (leaf) command:
Once the traversal reaches the final command, the following steps occur in order:
- Validation: Checks for missing mandatory options, conflicting options, and unknown options.
- Argument processing: Processes any remaining arguments as command-arguments.
- Hooks & Execution:
- Call
preActionhooks. - Call the action handler.
- Call
postActionhooks.
- Call