Skip to content

Commit

Permalink
feat(command): add support for function as description parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Jun 14, 2020
1 parent 1a900be commit 8dfe004
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/command/lib/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BooleanType } from '../types/boolean.ts';
import { NumberType } from '../types/number.ts';
import { StringType } from '../types/string.ts';
import { Type } from '../types/type.ts';
import { IAction, IArgumentDetails, ICommandOption, ICompleteHandler, ICompleteOptions, ICompleteSettings, IEnvVariable, IEnvVarOption, IExample, IHelpCommand, IOption, IParseResult, isHelpCommand, ITypeMap, ITypeOption, ITypeSettings } from './types.ts';
import { IAction, IArgumentDetails, ICommandOption, ICompleteHandler, ICompleteOptions, ICompleteSettings, IDescription, IEnvVariable, IEnvVarOption, IExample, IHelpCommand, IOption, IParseResult, isHelpCommand, ITypeMap, ITypeOption, ITypeSettings } from './types.ts';

const permissions: any = ( Deno as any ).permissions;
const envPermissionStatus: any = permissions && permissions.query && await permissions.query( { name: 'env' } );
Expand All @@ -32,7 +32,7 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
protected _name: string = 'COMMAND';
protected _parent?: BaseCommand;
protected ver: string = '0.0.0';
protected desc: string = 'No description ...';
protected desc: IDescription = '';
protected fn: IAction<O, A> | undefined;
protected options: IOption<O, A>[] = [];
protected commands: Map<string, BaseCommand> = new Map();
Expand Down Expand Up @@ -196,7 +196,7 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
*
* @param description Short command description.
*/
public description( description: string ): this {
public description( description: IDescription ): this {
this.cmd.desc = description;
return this;
}
Expand Down Expand Up @@ -988,8 +988,8 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
* Get command description.
*/
public getDescription(): string {

return this.desc;
// call description method only once
return typeof this.desc === 'function' ? this.desc = this.desc() : this.desc;
}

public getShortDescription(): string {
Expand Down
3 changes: 3 additions & 0 deletions packages/command/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { IFlagArgument, IFlagOptions, ITypeHandler, OptionType } from '../../fla
import { Type } from '../types/type.ts';
import { BaseCommand } from './base-command.ts';

/** Description handler. */
export type IDescription = string | ( ( this: BaseCommand ) => string );

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

Expand Down

0 comments on commit 8dfe004

Please sign in to comment.