-
-
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): make executed command accessible with
this
in action…
… handler (#28)
- Loading branch information
Showing
4 changed files
with
121 additions
and
14 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
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,65 @@ | ||
import { BaseCommand } from '../../lib/base-command.ts'; | ||
import { Command } from '../../lib/command.ts'; | ||
import { assertEquals } from '../lib/assert.ts'; | ||
|
||
interface IStats { | ||
context: null | BaseCommand, | ||
options: any, | ||
args: any | ||
} | ||
|
||
function createStats(): IStats { | ||
return { | ||
context: null, | ||
options: null, | ||
args: null | ||
}; | ||
} | ||
|
||
Deno.test( 'flags allowEmpty enabled', async () => { | ||
|
||
const stats: IStats = createStats(); | ||
|
||
const cmd: Command = new Command() | ||
.throwErrors() | ||
.arguments( '[beep:string]' ) | ||
.option( '-f, --foo [value:string]', 'description ...' ) | ||
.action( function ( options, ...args ) { | ||
stats.context = this; | ||
stats.options = options; | ||
stats.args = args; | ||
} ); | ||
|
||
const { options, args } = await cmd.parse( [ '--foo', 'bar', 'beep' ] ); | ||
|
||
assertEquals( stats.context, cmd ); | ||
assertEquals( stats.options, { foo: 'bar' } ); | ||
assertEquals( stats.args, [ 'beep' ] ); | ||
assertEquals( stats.options, options ); | ||
assertEquals( stats.args, args ); | ||
} ); | ||
|
||
Deno.test( 'flags allowEmpty enabled', async () => { | ||
|
||
const stats: IStats = createStats(); | ||
let subCmd: Command; | ||
|
||
const cmd: Command = new Command() | ||
.throwErrors() | ||
.command( 'foo', subCmd = new Command() | ||
.arguments( '[beep:string]' ) | ||
.option( '-b, --bar [value:string]', 'description ...' ) | ||
.action( function ( options, ...args ) { | ||
stats.context = this; | ||
stats.options = options; | ||
stats.args = args; | ||
} ) ); | ||
|
||
const { options, args } = await cmd.parse( [ 'foo', '--bar', 'baz', 'beep' ] ); | ||
|
||
assertEquals( stats.context, subCmd ); | ||
assertEquals( stats.options, { bar: 'baz' } ); | ||
assertEquals( stats.args, [ 'beep' ] ); | ||
assertEquals( stats.options, options ); | ||
assertEquals( stats.args, 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 |
---|---|---|
@@ -1,25 +1,67 @@ | ||
import { BaseCommand } from '../../lib/base-command.ts'; | ||
import { Command } from '../../lib/command.ts'; | ||
import { assertEquals } from '../lib/assert.ts'; | ||
|
||
interface IStats { | ||
context: null | BaseCommand, | ||
options: any, | ||
args: any | ||
} | ||
|
||
function createStats(): IStats { | ||
return { | ||
context: null, | ||
options: null, | ||
args: null | ||
}; | ||
} | ||
|
||
Deno.test( 'command optionAction action', async () => { | ||
|
||
let actionOptions!: any; | ||
let actionArgs!: string[]; | ||
const stats: IStats = createStats(); | ||
|
||
const cmd = new Command() | ||
.throwErrors() | ||
.arguments( '[argument]' ) | ||
.option( '-a, --action <action:string>', 'action ...', { | ||
action: ( options: any, ...args: string[] ) => { | ||
actionOptions = options; | ||
actionArgs = args; | ||
.arguments( '[beep:string]' ) | ||
.option( '-f, --foo [value:string]', 'action ...', { | ||
action: function ( options, ...args ) { | ||
stats.context = this; | ||
stats.options = options; | ||
stats.args = args; | ||
} | ||
} ); | ||
|
||
const { options, args } = await cmd.parse( [ '-a', 'my-action', 'arg' ] ); | ||
const { options, args } = await cmd.parse( [ '--foo', 'bar', 'beep' ] ); | ||
|
||
assertEquals( stats.context, cmd ); | ||
assertEquals( stats.options, { foo: 'bar' } ); | ||
assertEquals( stats.args, [ 'beep' ] ); | ||
assertEquals( stats.options, options ); | ||
assertEquals( stats.args, args ); | ||
} ); | ||
|
||
Deno.test( 'command optionAction action', async () => { | ||
|
||
const stats: IStats = createStats(); | ||
let subCmd: Command; | ||
|
||
const cmd = new Command() | ||
.throwErrors() | ||
.command( 'foo', subCmd = new Command() | ||
.arguments( '[beep:string]' ) | ||
.option( '-b, --bar [value:string]', 'action ...', { | ||
action: function ( options, ...args ) { | ||
stats.context = this; | ||
stats.options = options; | ||
stats.args = args; | ||
} | ||
} ) ); | ||
|
||
const { options, args } = await cmd.parse( [ 'foo', '--bar', 'baz', 'beep' ] ); | ||
|
||
assertEquals( options, { action: 'my-action' } ); | ||
assertEquals( actionOptions, { action: 'my-action' } ); | ||
assertEquals( actionArgs, [ 'arg' ] ); | ||
assertEquals( args, [ 'arg' ] ); | ||
assertEquals( stats.context, subCmd ); | ||
assertEquals( stats.options, { bar: 'baz' } ); | ||
assertEquals( stats.args, [ 'beep' ] ); | ||
assertEquals( stats.options, options ); | ||
assertEquals( stats.args, args ); | ||
} ); |