CLI Utilities
The CLI utilities provide simple command-line argument parsing functionality for Node.js applications.
Installation
Import the utilities from the kempo-server package:
import { getArgs } from 'kempo-server/utils/cli';
getArgs(mapping, argv)
Parses command-line arguments into a key-value object.
Parameters
mapping(object, optional) - Maps short flag names to full namesargv(array, optional) - Array of arguments to parse (defaults toprocess.argv)
Returns
An object where keys are argument names and values are parsed argument values.
Features
- Supports both space-separated (
--flag value) and equals-separated (--flag=value) formats - Automatically converts
"true"and"false"strings to boolean values - Supports short flags with mapping (e.g.,
-pmaps toport) - Handles multiple values for the same flag
- Boolean flags without values are set to
true
Examples
Basic Usage
node app.js --port 8080 --host localhost --verbose
const args = getArgs();
// Result: { port: '8080', host: 'localhost', verbose: true }
With Short Flag Mapping
node app.js -p 8080 -h localhost -v
const args = getArgs({
p: 'port',
h: 'host',
v: 'verbose'
});
// Result: { port: '8080', host: 'localhost', verbose: true }
Equals-Separated Values
node app.js --port=8080 --enabled=true --disabled=false --name=John
const args = getArgs();
// Result: { port: '8080', enabled: true, disabled: false, name: 'John' }
Mixed Formats
node app.js --port=8080 -h localhost --verbose --config=prod.json
const args = getArgs({ h: 'host' });
// Result: { port: '8080', host: 'localhost', verbose: true, config: 'prod.json' }
Values with Equals Signs
node app.js --url=https://example.com?param=value
const args = getArgs();
// Result: { url: 'https://example.com?param=value' }
Other Functions
The CLI utilities module also exports other functions for running child processes and prompting users:
runChildProcess(command)- Run a shell commandrunChildNodeProcess(scriptPath, argsObj)- Run a Node.js script with argumentsrunChildNodeProcessScript(scriptPath)- Run a Node.js scriptpromptUser(query)- Prompt user for inputpromptYN(query, defaultValue)- Prompt for yes/no with default