Skip to content

ash-uncover/js-utils-command

Repository files navigation

js-utils-command

npm version Last Push

js-utils-command is a small JavaScript library helping to analyse script commands and their arguments

Installation

npm i --save-d @uncover/js-utils-command

Content

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.

CommandArgument

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

CommandArgumentTypes

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

CommandOptions

Option Default Description
prefix -- Prefix of the argument declaration

Usage

Define Command arguments

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])

Output

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
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published