decarg
decorator based cli arguments parser
npm i decarg
|
pnpm add decarg
|
yarn add decarg
|
Examples
# basic
# view source
example/basic.ts
import { arg, decarg } from 'decarg'
class Options {
@arg('<file> [<file>, decarg.]', 'Files to process') file!: string[]
@arg('--', '[decarg.rest]', 'The rest of the arguments') passArgs = []
@arg('-f', '--flag', 'Flag about something') flag = false
@arg('-c', '--count', 'How many times') count = 42
@arg('-C', 'Choose color', ['blue', 'red', 'yellow']) color = 'blue'
@arg('-s', '--string', 'Some string') string = 'hmm'
@arg('--meh', 'Meh') meh: string[] = []
static examples = {
'-f foo': 'Convert foo by force',
'-f foo -s': 'Convert foo by force smoothly',
}
}
const options = decarg(new Options())
// these fail:
// const options = decarg(new Options(), ['exec'])
// const options = decarg(new Options(), ['exec', '-C'])
// const options = decarg(new Options(), ['exec', '-C=green'])
// const options = decarg(new Options(), ['exec', '-c=not a number'])
// this passes:
// const options = decarg(new Options(), ['exec', 'foo'])
console.log(options)
API
# OptionExpectedValueError
src/errors.ts#L20
# captureStackTrace
(targetObject, constructorOpt)
# OptionHelpRequested
src/errors.ts#L49
# captureStackTrace
(targetObject, constructorOpt)
# captureStackTrace
(targetObject, constructorOpt)
# OptionValidationError
src/errors.ts#L8
# captureStackTrace
(targetObject, constructorOpt)
# arg
(description)
src/arg.ts#L7 # log
decarg<T>(target, argv, { exit, log }) =>
- undefined | T
# parse
(target, argv)
src/parse.ts#L17 Credits
- reflect-metadata by Ron Buckton – Polyfill for Metadata Reflection API
Contributing
All contributions are welcome!