-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): implement
.stopEarly()
method (#39)
- Loading branch information
Showing
4 changed files
with
119 additions
and
6 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,16 @@ | ||
#!/usr/bin/env -S deno run | ||
|
||
import { Command } from '../../packages/command/lib/command.ts'; | ||
|
||
await new Command() | ||
.version( '0.1.0' ) | ||
|
||
.stopEarly() | ||
.option( '-d, --debug-level <level:string>', 'Debug level.' ) | ||
.arguments( '[script:string] [...args:number]' ) | ||
.action( ( options: any, script: string, args: string[] ) => { | ||
console.log( 'options:', options ); | ||
console.log( 'script:', script ); | ||
console.log( 'args:', args ); | ||
} ) | ||
.parse( Deno.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Command } from '../../lib/command.ts'; | ||
import { assertEquals, assertThrowsAsync } from '../lib/assert.ts'; | ||
|
||
Deno.test( 'flags stopEarly disable', async () => { | ||
|
||
const { options, args, literal } = await new Command() | ||
.throwErrors() | ||
.option( '-f, --flag [value:boolean]', 'description ...' ) | ||
.option( '-s, --script-arg1 [value:boolean]', 'description ...' ) | ||
.option( '-S, --script-arg2 [value:boolean]', 'description ...' ) | ||
.arguments( '[script:string] [args...:string]' ) | ||
.action( () => {} ) | ||
.parse( [ | ||
'-f', 'true', 'run', 'script-name', '--script-arg1', '--script-arg2', '--', '--literal-arg1', '--literal-arg2' | ||
] ); | ||
|
||
assertEquals( options, { flag: true, scriptArg1: true, scriptArg2: true } ); | ||
assertEquals( args, [ 'run', [ 'script-name' ] ] ); | ||
assertEquals( literal, [ '--literal-arg1', '--literal-arg2' ] ); | ||
} ); | ||
|
||
Deno.test( 'flags stopEarly enabled', async () => { | ||
|
||
const { options, args, literal } = await new Command() | ||
.throwErrors() | ||
.stopEarly() | ||
.option( '-f, --flag [value:boolean]', 'description ...' ) | ||
.option( '-s, --script-arg1 [value:boolean]', 'description ...' ) | ||
.option( '-S, --script-arg2 [value:boolean]', 'description ...' ) | ||
.arguments( '[script:string] [args...:string]' ) | ||
.action( () => {} ) | ||
.parse( [ | ||
'-f', 'true', 'run', 'script-name', '--script-arg1', '--script-arg2', '--script-arg3', '--', '--literal-arg1', '--literal-arg2' | ||
] ); | ||
|
||
assertEquals( options, { flag: true } ); | ||
assertEquals( args, [ 'run', [ 'script-name', '--script-arg1', '--script-arg2', '--script-arg3' ] ] ); | ||
assertEquals( literal, [ '--literal-arg1', '--literal-arg2' ] ); | ||
} ); | ||
|
||
Deno.test( 'flags stopEarly unknown option', async () => { | ||
|
||
const cmd = new Command() | ||
.throwErrors() | ||
.stopEarly() | ||
.option( '-f, --flag [value:boolean]', 'description ...' ) | ||
.option( '-s, --script-arg1 [value:boolean]', 'description ...' ) | ||
.option( '-S, --script-arg2 [value:boolean]', 'description ...' ) | ||
.action( () => {} ); | ||
|
||
await assertThrowsAsync( async () => { | ||
await cmd.parse( [ | ||
'-f', 'true', '-t', 'true', 'run', 'script-name', '--script-arg1', '--script-arg2', '--script-arg3', '--', '--literal-arg1', '--literal-arg2' | ||
] ); | ||
}, Error, 'Unknown option: -t' ); | ||
} ); |