Skip to content

Commit

Permalink
feat(flags): add support for shorthand flag's with value e.g. -n5 r…
Browse files Browse the repository at this point in the history
…esults in `{n: 5}` but `-abc` will still result in `{a: true, b: true, c: true}`
  • Loading branch information
c4spar committed Jun 11, 2020
1 parent 53ba110 commit 775c528
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions packages/flags/lib/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,47 @@ export function normalize( args: string[] ) {

if ( inLiteral ) {
normalized.push( arg );

} else if ( arg === '--' ) {
inLiteral = true;
normalized.push( arg );

} else if ( arg[ 0 ] === '-' ) {
const isLong = arg[ 1 ] === '-';

if ( arg.includes( '=' ) ) {
const parts = arg.split( '=' );
const flag = parts.shift() as string;
if ( arg[ 1 ] === '-' ) {

if ( isLong ) {
normalized.push( flag );
} else {
flag.slice( 1 ).split( '' ).forEach( val => normalized.push( `-${ val }` ) );
normalizeShortFlags( flag );
}
normalized.push( parts.join( '=' ) );
} else if ( arg[ 1 ] === '-' ) {

} else if ( isLong ) {
normalized.push( arg );

} else {
arg.slice( 1 ).split( '' ).forEach( val => normalized.push( `-${ val }` ) );
normalizeShortFlags( arg );
}
} else {
normalized.push( arg );
}
}

return normalized;

function normalizeShortFlags( flag: string ): void {

const flags = flag.slice( 1 ).split( '' );

if ( isNaN( flag[ flag.length - 1 ] as any ) ) {
flags.forEach( val => normalized.push( `-${ val }` ) );
} else {
normalized.push( `-${ flags.shift() }` );
normalized.push( flags.join( '' ) );
}
}
}

0 comments on commit 775c528

Please sign in to comment.