README
decarg
decorator based cli arguments parser
🔧 Install · 🧩 Example · 📜 API docs · 🔥 Releases · 💪🏼 Contribute · 🖐️ Help
Install
$ npm i decarg
Example
import { arg, decarg } from 'decarg'
class Options {
@arg('<file> [<file>, ...]', 'Files to process')
file!: string[]
@arg('--', '[...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())
API
Table of Contents
decarg
Process an options object and handle errors.
Parameters
target
T The decorated options object.argv
Arguments values. (optional, defaultprocess.argv.slice(1)
)overrides
Object (optional, default{}
)overrides.exit
The exit function to use. (optional, defaultprocess.exit
)overrides.log
The log function to use. (optional, defaultconsole.error
)
Returns any The populated options object when successful, otherwise prints help and exits.
parse
Process an options object and throw errors so that they can be managed manually.
Parameters
target
T The decorated options object.argv
Arguments values. (optional, defaultprocess.argv.slice(1)
)
- Throws any An AggregateError with OptionValidationError when there are multiple errors
or an OptionHelpRequested error.
The
.message
property can be printed and is the same as the managed version decarg.
Returns any The populated options object when successful, otherwise throws.
Errors
OptionValidationError
Extends Error
OptionValidationError.
Thrown when the arguments input was somehow invalid.
Parameters
message
string
OptionExpectedValueError
Extends OptionValidationError
OptionExpectedValueError.
Thrown when an argument expects a value and it was not given.
Parameters
option
Option<T>
OptionInvalidValueError
Extends OptionValidationError
OptionInvalidValueError.
Thrown when an invalid option was passed, i.e not in choices when a multiple choices argument or not a number when a Number argument.
Parameters
option
Option<T>value
any
OptionHelpRequested
Extends Error
OptionHelpRequested.
Not an error per se, but thrown anyway when --help
is passed in the arguments.
Parameters
options
Options<T>
Notes
Decorators must be enabled in your TypeScript configuration:
...
"compilerOptions": {
...
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
...
}
...
Contribute
All contributions are welcome!
License
MIT © 2021 stagas