resolve-cli-args
A simple function to resolve cli arguments.
Install
npm i resolve-cli-args
Usage
import { resolveCliArgs } from 'resolve-cli-args'
const { args } = resolveCliArgs(process.argv.slice(2))
if (args['--help'] || args['-h']) {
}
The type of the return value is:
interface ResolvedCliArgs {
args: Record<string, string[] | undefined>
unnamedValues: string[]
}
If the option do not have any values, the value of this option will be set to an empty array.
Examples
import { resolveCliArgs } from 'resolve-cli-args'
console.log(resolveCliArgs([
'--config', 'config.json', 'input.txt', 'output.txt'
]))
console.log(resolveCliArgs([
'--log-level=2', '--type', 'typescript'
]))
console.log(resolveCliArgs([
'--compress', '-q'
]))
console.log(resolveCliArgs([
'--a', '1', 'a', '--b', '2', 'b', '--c=3', 'c'
]))
console.log(resolveCliArgs([
'--ext=.js', '--ext=.ts', '--ext', '.jsx', '--ext', '.tsx'
]))
console.log(resolveCliArgs([
'--var=a=b', '--var', '---c=d'
]))
console.log(resolveCliArgs([
'--a=1', '--', '--c=d', '-e', 'f'
]))
Note: The string that starts with "--"
or "-"
will be treated as option name (not including the string that starts with "---"
).
License
MIT