Skip to content

Commit

Permalink
fix(command): separator option is ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 21, 2020
1 parent 0c2f400 commit 0405244
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/command/list-option-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { options } = await new Command()
// comma separated list
.option( '-l, --list <items:number[]>', 'comma separated list of numbers.' )
// space separated list
.option( '-o, --other-list <items:string[]>', 'comma separated list of strings.', { separator: ' ' } )
.option( '-o, --other-list <items:string[]>', 'space separated list of strings.', { separator: ' ' } )
.parse( Deno.args );

console.log( options );
8 changes: 8 additions & 0 deletions packages/command/lib/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
...opts
};

if ( option.separator ) {
for ( const arg of args ) {
if ( arg.list ) {
arg.separator = option.separator;
}
}
}

for ( const part of result.args ) {

const arg = part.trim();
Expand Down
25 changes: 25 additions & 0 deletions packages/command/test/option/list_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Command } from '../../lib/command.ts';
import { assertEquals } from '../lib/assert.ts';

const cmd = new Command()
.throwErrors()
// comma separated list
.option( '-l, --list <items:number[]>', 'comma separated list of numbers.' )
// space separated list
.option( '-o, --other-list <items:string[]>', 'space separated list of strings.', { separator: ' ' } );

Deno.test( 'command: list option', async () => {

const { options, args } = await cmd.parse( [ '-l', '1,2,3' ] );

assertEquals( options, { list: [ 1, 2, 3 ] } );
assertEquals( args, [] );
} );

Deno.test( 'command: list option separator', async () => {

const { options, args } = await cmd.parse( [ '-o', '1 2 3' ] );

assertEquals( options, { otherList: [ '1', '2', '3' ] } );
assertEquals( args, [] );
} );

0 comments on commit 0405244

Please sign in to comment.