js-utils-command is a small JavaScript library helping to analyse script commands and their arguments
npm i --save-d @uncover/js-utils-command
Main object providing a static parse method
Command.parse(args: CommandArgument[], options?: CommandOptions)
This methods will parse the current process.argv, perform checks on the received arguments and return a javascript object containing the arguments.
Field | Type | Details |
---|---|---|
key | string | Name of the argument |
alias | string[ ] | List of alias/shorthands for the argument |
description | string | Description of the argument |
required | boolean | Whether or not the argument is required |
type | CommandArgumentType | The type of arguments. Will affect the checks performed on the value |
Restricted argument keys and aliases: 'help', 'h', '?' which are used by the autogenerated Help argument
Type | Details |
---|---|
VALUE | String argument |
BOOLEAN | Boolean argument (no value, true is present) |
FILE | Relative path to a file |
FILE_EXIST | Relative path to an existing file |
FILE_FOLDER | Relative path to a file or folder |
FILE_FOLDER_EXIST | Relative path to an existing file or folder |
FOLDER | Relative path to a folder |
FOLDER_EXIST | Relative path to an existing foldert |
Option | Default | Description |
---|---|---|
prefix | -- | Prefix of the argument declaration |
Example
import { Command, CommandArgumentsTypes } from '@uncover/js-utils-command
const arg1 = {
key: 'arg1',
type: CommandArgumentsTypes.VALUE,
required: true
}
const arg2 = {
key: 'arg2',
type: CommandArgumentsTypes.BOOLEAN
}
Command.parse([arg1, arg2])
Example 1:
node my-custom-script.js --arg1=value1 --arg2
Will return
{
arg1: 'value1',
arg2: true
}
Example 2:
node my-custom-script.js --arg1=value2
Will return
{
arg1: 'value2',
arg2: false
}