1.2.0 • Published 5 years ago

@phylum/command v1.2.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

@phylum/command

Build Status Coverage Status Latest License

Configurable command line parser

Usage

npm i @phylum/command
import { CommandSpec } from '@phylum/command';

new CommandSpec()
	.add({name: 'foo', multiple: true})
	.add({name: 'bar', alias: 'b' type: 'flag'})
	.parse(['--foo', 'bar', 'baz', '-b']);

// => { foo: ['bar', 'baz'], bar: true }

Command Line Format

FormatOutputSpec
"--foo bar"{foo: 'bar'}{name: 'foo'}
"--foo=bar"{foo: 'bar'}{name: 'foo'}
"--foo bar baz"{foo: ['bar', 'baz']}{name: 'foo', multiple: true}
"bar baz"{foo: ['bar', 'baz']}{name: 'foo', multiple: true, defaultFallback: true}
"--foo"{foo: true}{name: 'foo', type: 'flag'}
"--foo 42"{foo: 42}{name: 'foo', type: 'number'}
"-f bar"{foo: 'bar'}{name: 'foo', alias: 'f'}
"-fb"{foo: true, bar: true}{name: 'foo', alias: 'f', type: 'flag'}, {name: 'bar', alias: 'b', type: 'flag'}
""{foo: 'bar'}{name: 'foo', defaultValue: 'bar'}
-- bar --baz{foo: ['bar', '--baz']}{name: 'foo', type: 'rest'}

Options

options.partial

If specified, unknown arguments will be ignored instead of throwing an error.

new CommandSpec()
	.add({name: 'foo'})
	.parse(['--foo', 'bar', '--baz'], {partial: true});

// => { foo: 'bar' }

options.sparse

If specified, arguments that follow others may be associated with the default argument.

new CommandSpec()
	.add({name: 'foo', default: true})
	.add({name: 'bar'})
	.parse(['--bar', 'baz', 'foo'], {sparse: true});

// => { bar: 'baz', foo: 'foo' }

Custom Types

function myType(value: string, spec: ArgumentSpec, options: ArgumentOptions) {
	return value.toUpperCase();
}

new CommandSpec()
	.add({name: 'foo', type: myType})
	.parse(['--foo', 'bar']);

// => { foo: 'BAR' }

Packaged Code

PathTypeEntry Point
/dist/nodeES2017, CommonJS Modulesmain
/dist/es2015ES2015, ES Modulesbrowser
/dist/es2017ES2017, ES Modules
/srcTypeScript Sources