-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(flags): default option incompatible with depends option and boole…
…an flag's * fix(flags): fix default value for boolean flags with optional value * fix(flags): default option is incompatible with depends option (#3) * test(command): add test's for `default` and `depends` option's
- Loading branch information
Showing
7 changed files
with
337 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Command } from '../../lib/command.ts'; | ||
import { assertEquals } from '../lib/assert.ts'; | ||
|
||
Deno.test( 'command: option -> default', async () => { | ||
|
||
const { options, args } = await new Command() | ||
.throwErrors() | ||
|
||
.option( '--flag1', 'flag 1' ) | ||
.option( '--flag2 <val:string>', 'flag 2', { default: 'example' } ) | ||
|
||
.parse( [] ); | ||
|
||
assertEquals( options, { flag2: 'example' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'command: option -> default', async () => { | ||
|
||
const { options, args } = await new Command() | ||
.throwErrors() | ||
|
||
.option( '--flag1', 'flag 1' ) | ||
.option( '--flag2 <val:string>', 'flag 2', { default: 'example' } ) | ||
|
||
.parse( [ '--flag1' ] ); | ||
|
||
assertEquals( options, { flag1: true, flag2: 'example' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'command: option -> default', async () => { | ||
|
||
const { options, args } = await new Command() | ||
.throwErrors() | ||
|
||
.option( '--flag1', 'flag 1' ) | ||
.option( '--flag2 <val:string>', 'flag 2', { default: 'example' } ) | ||
|
||
.parse( [ '--flag2', 'test' ] ); | ||
|
||
assertEquals( options, { flag2: 'test' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'command: option -> default', async () => { | ||
|
||
const { options, args } = await new Command() | ||
.throwErrors() | ||
|
||
.option( '--flag1', 'flag 1' ) | ||
.option( '--flag2 <val:string>', 'flag 2', { default: 'example' } ) | ||
|
||
.parse( [ '--flag1', '--flag2', 'test' ] ); | ||
|
||
assertEquals( options, { flag1: true, flag2: 'test' } ); | ||
assertEquals( args, [] ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { assertThrows } from '../../../flags/test/lib/assert.ts'; | ||
import { Command } from '../../lib/command.ts'; | ||
import { assertEquals } from '../lib/assert.ts'; | ||
|
||
const cmd: Command = new Command() | ||
.throwErrors() | ||
.option( '--flag1', 'flag 1' ) | ||
.option( '--flag2 <val:string>', 'flag 2', { depends: [ 'flag1' ], default: 'example' } ); | ||
|
||
Deno.test( 'command depends option with default value: should accept no arguments', async () => { | ||
|
||
const { options, args } = await cmd.parse( [] ); | ||
|
||
assertEquals( options, { flag2: 'example' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'command depends option with default value: should accept -h', async () => { | ||
|
||
const { options, args } = await cmd.parse( [ '-h' ] ); | ||
|
||
assertEquals( options, { flag2: 'example' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'command depends option with default value: should accept --flag1', async () => { | ||
|
||
const { options, args } = await cmd.parse( [ '--flag1' ] ); | ||
|
||
assertEquals( options, { flag1: true, flag2: 'example' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'command depends option with default value: should accept --flag1 --flag2 test', async () => { | ||
|
||
const { options, args } = await cmd.parse( [ '--flag1', '--flag2', 'test' ] ); | ||
|
||
assertEquals( options, { flag1: true, flag2: 'test' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'command depends option with default value: should not accept --flag2 test', async () => { | ||
|
||
assertThrows( | ||
() => cmd.parse( [ '--flag2', 'test' ] ), | ||
Error, | ||
'Option --flag2 depends on option: --flag1' | ||
); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.