Skip to content

Commit

Permalink
feat(command): make executed command accessible with this in action…
Browse files Browse the repository at this point in the history
… handler (#28)
  • Loading branch information
c4spar committed Jun 13, 2020
1 parent ddd8208 commit 461145f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/command/lib/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
const actionOption = this.findActionFlag( options );

if ( actionOption && actionOption.action ) {
await actionOption.action( options, ...args );
await actionOption.action.call( this, options, ...args );
return { options, args: args as any as A, cmd: this, literal: this.literalArgs };
}

Expand Down
2 changes: 1 addition & 1 deletion packages/command/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Type } from '../types/type.ts';
import { BaseCommand } from './base-command.ts';

/** Action handler. */
export type IAction<O, A extends Array<any>> = ( options: O, ...args: A ) => void | Promise<void>;
export type IAction<O, A extends Array<any>> = ( this: BaseCommand, options: O, ...args: A ) => void | Promise<void>;

/** Omit key from object. */
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
Expand Down
65 changes: 65 additions & 0 deletions packages/command/test/command/action_test.ts
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 );
} );
66 changes: 54 additions & 12 deletions packages/command/test/option/action_test.ts
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 );
} );

0 comments on commit 461145f

Please sign in to comment.