-
-
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.
feat(command): add support for global option's (#2)
- Loading branch information
Showing
7 changed files
with
212 additions
and
35 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,20 @@ | ||
#!/usr/bin/env -S deno run | ||
|
||
import { Command } from '../../packages/command/lib/command.ts'; | ||
|
||
await new Command() | ||
.version( '0.1.0' ) | ||
.option( '-l, --local [val:string]', 'Only available on this command.' ) | ||
.option( '-g, --global [val:string]', 'Available on this and all nested child command\'s.', { global: true } ) | ||
.action( console.log ) | ||
|
||
.command( 'command1', new Command() | ||
.description( 'Some sub command.' ) | ||
.action( console.log ) | ||
|
||
.command( 'command2', new Command() | ||
.description( 'Some nested sub command.' ) | ||
.action( console.log ) | ||
) | ||
) | ||
.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
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,57 @@ | ||
import { IFlagArgument, IFlagOptions } from '../../../flags/lib/types.ts'; | ||
import { Command } from '../../lib/command.ts'; | ||
import { assertEquals } from '../lib/assert.ts'; | ||
|
||
const cmd = new Command() | ||
.version( '0.1.0' ) | ||
.option( '-b, --base', 'Only available on this command.' ) | ||
.option( '-g, --global [val:custom]', 'Available on all command\'s.', { global: true } ) | ||
.type( 'custom', (option: IFlagOptions, arg: IFlagArgument, value: string) => value.toUpperCase() ) | ||
.command( 'sub-command', new Command() | ||
.option( '-l, --level2 [val:custom]', 'Only available on this command.' ) | ||
.description( 'Some sub command.' ) | ||
.command( 'sub-command', new Command() | ||
.option( '-L, --level3 [val:custom]', 'Only available on this command.' ) | ||
.description( 'Some nested sub command.' ) | ||
) | ||
); | ||
|
||
Deno.test( 'command with global option', async () => { | ||
|
||
const { options, args } = await cmd.parse( [ '-g', 'halo' ] ); | ||
|
||
assertEquals( options, { global: 'HALO' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'sub command with global option', async () => { | ||
|
||
const { options, args } = await cmd.parse( [ 'sub-command', '-g', 'halo' ] ); | ||
|
||
assertEquals( options, { global: 'HALO' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'nested sub command with global option', async () => { | ||
|
||
const { options, args } = await cmd.parse( [ 'sub-command', 'sub-command', '-g', 'halo' ] ); | ||
|
||
assertEquals( options, { global: 'HALO' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'sub command with global option', async () => { | ||
|
||
const { options, args } = await cmd.parse( [ 'sub-command', '-l', 'halo' ] ); | ||
|
||
assertEquals( options, { level2: 'HALO' } ); | ||
assertEquals( args, [] ); | ||
} ); | ||
|
||
Deno.test( 'nested sub command with global option', async () => { | ||
|
||
const { options, args } = await cmd.parse( [ 'sub-command', 'sub-command', '-L', 'halo' ] ); | ||
|
||
assertEquals( options, { level3: 'HALO' } ); | ||
assertEquals( args, [] ); | ||
} ); |