Argumental provides a chainable API to define the whole application in one go (unless a modular approach is desired where multiple chains are used).
Once imported, the following methods are available on the app object and all methods return a reference to the parent object for chaining (except for parse()
, emit()
, data()
, and the built-in validators).
- command()
- alias()
- argument()
- option()
- action()
- actionDestruct()
- description()
- required()
- multi()
- immediate()
- default()
- validate()
- validateDestruct()
- sanitize()
- sanitizeDestruct()
- data()
- version()
- parse()
- shared
- global
- top
- config()
- on()
- emit()
- error()
- STRING
- NUMBER
- BOOLEAN
- FILE_PATH
- STRINGS
- NUMBERS
- BOOLEANS
Defines a command.
- name: The command name. Can only contain alphanumeric characters and nonconsecutive spaces.
- description:
Optional
A description to display in application help.
Defines an alias for the current command.
- name: The command alias name. Can only contain alphanumeric characters and spaces.
Defines an argument for the current command.
- syntax: The argument syntax. Use
<>
for required arguments and[]
for optional arguments (e.g.<file_path>
). Argument name can only contain alphanumeric characters,-
s, and_
s. Rest arguments can be defined using...
before the name (e.g.[...args]
or<...args>
). - description:
Optional
A description to display in application help. - validators:
Optional
A single or an array of validators to validate the argument value (destructuring parameters not supported). - defaultValue:
Optional
The default value of the argument if value was not provided (only works with optional arguments).
Defines an option for the current command.
- syntax: The option syntax. You can define a one letter shorthand (e.g.
-p
), an option name (e.g.--port-number
), and one argument (e.g.<port_number>
) in the syntax (e.g.-p --port-number <port_number>
). Option name can only contain alphanumeric characters and-
s. - description:
Optional
A description to display in application help. - required:
Optional
Indicates whether this option is required for the command. - validators:
Optional
A single or an array of validators to validate the option's argument value (only applies to options with an argument and destructuring parameters not supported). - multi:
Optional
Indicates whether this option can be repeated more than once (only practical for options with argument). - defaultValue:
Optional
The default value of the argument if value was not provided (only applies to options with an argument). - immediate:
Optional
Indicates if all other components (arguments, options, etc.) should be ignored in the parsing process and action handlers should be called as soon as possible when this option is provided. This behavior is desired with options such as--help
and--version
. Keep in mind that validators will run before the action handlers for the first encountered immediate option only.
Defines an action for the current command.
- handler: An action handler function which takes the following parameters:
- args: A key-value pair object containing the passed-in arguments (uses camel-cased argument names as keys).
Missing arguments' values arenull
while rest arguments' values are an array of values. - opts: A key-value pair object containing the passed-in options (uses the shorthand and camel-cased option names as keys).
If option definition didn't contain an argument, values would be booleans instead.
If option defined argument, value would beundefined
if option is not provided,null
if option provided without the argument's value, and the actual argument value otherwise.
If option defined argument and can occur multiple times, the value would be an array containing each occurrence's value. - suspend: A function which suspends next action handlers from being executed when called.
- cmd: The invoked command's name.
- args: A key-value pair object containing the passed-in arguments (uses camel-cased argument names as keys).
Action handlers can return a promise for async execution.
Same as action()
but all parameters are provided as a single object to support destructuring parameters.
Defines description for the current command, argument, or option.
- text: Description text to display in application help.
Sets the required flag for the current option.
- value: Required flag's boolean value (defaults to
true
).
Sets the multi flag for the current option.
- value: Multi flag's boolean value (defaults to
true
).
Sets the immediate flag for the current option.
- value: Immediate flag's boolean value (defaults to
true
).
Sets default value for the current argument or option.
- value: The default value to set.
Adds a single or multiple validators to the current option or argument.
- validators: A single or an array of validators to add.
Same as validate()
but all parameters are provided as a single object to support destructuring parameters.
Alias for validate()
.
Alias for validateDestruct()
.
Returns an object for sharing data throughout the application (useful for apps with modular design).
NOTE: If using TypeScript, type definition for the returned data object can be provided by using the generic signature of this method
data<T>()
.
Sets the application version.
- version: The version of the application.
This method defines -v --version
option on the top-level command only regardless of the current context.
Parses the passed in array of command-line arguments (e.g. process.argv
) and ends the chain by returning a void promise which always resolves (unless an error is thrown from an action handler).
- argv: Command-line arguments to parse.
Changes the definition context to shared. All definitions after this would be applied to all commands (excluding the top-level command).
Changes the definition context to global. All definitions after this would be applied to all commands (including the top-level command).
Changes the definition context to top-level. All definitions after this would be applied the top-level command only.
Configures Argumental with the given options. Options object can have any of the following properties:
- colors: Boolean indicating if logs should be colorful (defaults to
true
). - topLevelPlainHelp: When true, application help will be displayed when the top-level command is invoked without any arguments, options, or actions (defaults to
true
).
No default events would be emitted in this case. - help: A help renderer function to invoke when help must be rendered and logged to console. The function takes the following parameters:
- definitions: A key-value pair object containing all command declarations where key
''
refers to the top-level command. - cmd: The invoked command name.
- definitions: A key-value pair object containing all command declarations where key
Registers an event handler.
- event: A default or custom event name.
- handler: An event handler to register which takes the following parameter:
- data: The provided event data (if any).
Event handlers can return a promise for async execution.
Emits a custom event.
- event: The custom event name.
- data: The event data to provide to all handlers.
Displays an error message in the console.
- message: An error message or object.
The following methods are built-in validators that should be used by validate()
or sanitize()
or provided as arguments inside argument()
and option()
methods:
Built-in validator which validates the argument value as string.
Built-in validator which validates the argument value as a number (also converts the input to number).
Built-in validator which validates the argument value as boolean (also converts the input to boolean).
Built-in validator which validates the argument value as a file path (checks for file existence and read access synchronously).
Built-in validator which validates the rest argument value as multiple strings.
Built-in validator which validates the rest argument value as multiple numbers (also converts the input to number).
Built-in validator which validates the rest argument value as booleans (also converts the input to boolean).