Parse command line arguments by defining a struct with TypeScript decorators.
MIT License
Decorator-based CLI option parser
npm install --save structopt
Or if you use Yarn:
yarn add structopt
import { StructOpt, Option, fromArgs } from 'structopt'
@StructOpt({
name: 'example',
about: 'An example of StructOpt usage.',
})
class Opt {
// short and long flags (-d, --debug) will be deduced from the field's name
@Option({ short: true, long: true, description: 'Activate debug mode' })
debug!: boolean
// we don't want to name it "speed", need to look smart
@Option({ short: '-v', long: '--velocity', defaultValue: '42', description: 'Set speed' })
speed!: number
@Option({ required: true, description: 'Input file' })
input!: string
@Option({ description: 'Output file, stdout if not present' })
output?: string
}
const opt = fromArgs(Opt)
console.log(opt)
And run
$ ts-node examples/basic.ts --debug -v 80 /tmp/input_file
# Result:
# {
# debug: true,
# speed: 80,
# input: '/tmp/input_file'
# }
It also auto generates help text:
$ ts-node -T examples/basic.ts -h
example 0.1.5
An example of StructOpt usage.
USAGE:
basic.ts [FLAGS] [OPTIONS] <input> [ARGS]
FLAGS:
-d, --debug Activate debug mode
OPTIONS:
-v, --velocity <speed> Set speed [default: 42]
-f, <format>
ARGS:
<input> search pattern
<output> Output file, stdout if not present
Author
Contact
Bug reports
tar
command)