structopt
v0.1.0
Published
Parse command line arguments by defining a struct with TypeScript decorators.
Downloads
12
Readme
structopt
Decorator-based CLI option parser
Features
- Strongly-Typed: Use class to declare arguments.
- Declarative: Instead of chaining methods, use class to declare what arguments are needed.
- Impressive: Automatically infers argument names from a property name.
- Friendly: Automatically generates help.
Install
npm install --save structopt
Or if you use Yarn:
yarn add structopt
Example
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
Roadmap
- [ ] Class-validator integration
- [ ] Show other meta info
- [ ]
Author
- [ ]
Contact
- [ ]
Bug reports
- [ ]
- [ ] Sub-command style
- [ ] Multiple parameters
- [ ] For named options (allow multiple times if an option is specified)
- [ ] For arguments (like
tar
command)
- [ ] Environment variable fallback
- [ ] Custom parser, e.g. ) String to Date conversion