Skip to content

Commit

Permalink
feat(flags): implement stopEarly option (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Jun 4, 2020
1 parent 4f18db7 commit ee683d3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/flags/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ if ( flags.help ) {
| Param | Type | Required | Description |
| ----- | :--: | :--: | ----------- |
| allowEmpty | `boolean` | No | Allow no arguments. Defaults to `false` |
| stopEarly | `boolean` | No | If enabled, all values starting from the first non option argument will be added to `unknown`. |
| flags | `IFlagOptions[]` | No | Array of flag options. |
| parse | `function` | No | Custom type parser. |

Expand Down
6 changes: 5 additions & 1 deletion packages/flags/lib/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function parseFlags<O = any>( args: string[], opts: IParseOptions = {} ):
const flags: IFlags = {};
const literal: string[] = [];
const unknown: string[] = [];
let stopEarly: boolean = false;

opts.flags.forEach( opt => {
opt.depends?.forEach( flag => {
Expand Down Expand Up @@ -63,7 +64,7 @@ export function parseFlags<O = any>( args: string[], opts: IParseOptions = {} ):
const isFlag = current.length > 1 && current[ 0 ] === '-';
const next = () => normalized[ i + 1 ];

if ( isFlag ) {
if ( isFlag && !stopEarly ) {

if ( current[ 2 ] === '-' || ( current[ 1 ] === '-' && current.length === 3 ) ) {
throw new Error( `Invalid flag name: ${ current }` );
Expand Down Expand Up @@ -234,6 +235,9 @@ export function parseFlags<O = any>( args: string[], opts: IParseOptions = {} ):
}

} else {
if ( opts.stopEarly ) {
stopEarly = true;
}
unknown.push( current );
}
}
Expand Down
81 changes: 81 additions & 0 deletions packages/flags/test/setting/stop-early_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { parseFlags } from '../../lib/flags.ts';
import { OptionType } from '../../lib/types.ts';
import { assertEquals, assertThrows } from '../lib/assert.ts';

Deno.test( 'flags stopEarly disable', () => {

const { flags, unknown, literal } = parseFlags( [
'-f', 'true', 'run', 'script-name', '--script-arg1', '--script-arg2', '--', '--literal-arg1', '--literal-arg2'
], {
stopEarly: false,
flags: [ {
name: 'flag',
aliases: [ 'f' ],
type: OptionType.BOOLEAN
}, {
name: 'script-arg1',
aliases: [ 's' ],
type: OptionType.BOOLEAN
}, {
name: 'script-arg2',
aliases: [ 'S' ],
type: OptionType.BOOLEAN
} ]
} );

assertEquals( flags, { flag: true, scriptArg1: true, scriptArg2: true } );
assertEquals( unknown, [ 'run', 'script-name' ] );
assertEquals( literal, [ '--literal-arg1', '--literal-arg2' ] );
} );

Deno.test( 'flags stopEarly enabled', () => {

const { flags, unknown, literal } = parseFlags( [
'-f', 'true', 'run', 'script-name', '--script-arg1', '--script-arg2', '--script-arg3', '--', '--literal-arg1', '--literal-arg2'
], {
stopEarly: true,
flags: [ {
name: 'flag',
aliases: [ 'f' ],
type: OptionType.BOOLEAN
}, {
name: 'script-arg1',
aliases: [ 's' ],
type: OptionType.BOOLEAN
}, {
name: 'script-arg2',
aliases: [ 'S' ],
type: OptionType.BOOLEAN
} ]
} );

assertEquals( flags, { flag: true } );
assertEquals( unknown, [ 'run', 'script-name', '--script-arg1', '--script-arg2', '--script-arg3' ] );
assertEquals( literal, [ '--literal-arg1', '--literal-arg2' ] );
} );

Deno.test( 'flags stopEarly unknown option', () => {

assertThrows(
() => parseFlags( [
'-f', 'true', '-t', 'true', 'run', 'script-name', '--script-arg1', '--script-arg2', '--script-arg3', '--', '--literal-arg1', '--literal-arg2'
], {
stopEarly: true,
flags: [ {
name: 'flag',
aliases: [ 'f' ],
type: OptionType.BOOLEAN
}, {
name: 'script-arg1',
aliases: [ 's' ],
type: OptionType.BOOLEAN
}, {
name: 'script-arg2',
aliases: [ 'S' ],
type: OptionType.BOOLEAN
} ]
} ),
Error,
'Unknown option: -t'
);
} );

0 comments on commit ee683d3

Please sign in to comment.