Skip to content

Commit

Permalink
feat(command): make arguments generic
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 20, 2020
1 parent 09a3d00 commit 8a153a7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
41 changes: 20 additions & 21 deletions packages/command/lib/base-command.ts
Expand Up @@ -23,7 +23,7 @@ export type ITypeMap = IGenericObject<Type<any> | ITypeHandler<any>>
/**
* Base command implementation without pre configured command's and option's.
*/
export class BaseCommand<O = any> {
export class BaseCommand<O = any, A extends Array<any> = any> {

protected types: ITypeMap = {
string: new StringType(),
Expand All @@ -37,8 +37,8 @@ export class BaseCommand<O = any> {
protected _parent?: BaseCommand;
protected ver: string = '0.0.0';
protected desc: string = 'No description ...';
protected fn: IAction<O> | undefined;
protected options: IOption<O>[] = [];
protected fn: IAction<O, A> | undefined;
protected options: IOption<O, A>[] = [];
protected commands: Map<string, CommandMap> = new Map();
protected examples: IExample[] = [];
protected envVars: IEnvVariable[] = [];
Expand Down Expand Up @@ -210,7 +210,7 @@ export class BaseCommand<O = any> {
*
* @param fn Callback method.
*/
public action( fn: IAction<O> ): this {
public action( fn: IAction<O, A> ): this {
this.cmd.fn = fn;
this.reset();
return this;
Expand Down Expand Up @@ -320,8 +320,8 @@ export class BaseCommand<O = any> {
* @param desc Flag description.
* @param opts Flag options.
*/
public option( flags: string, desc: string, opts?: ICommandOption<O> ): this;
public option( flags: string, desc: string, opts?: ICommandOption<O> | IFlagValueHandler ): this {
public option( flags: string, desc: string, opts?: ICommandOption<O, A> ): this;
public option( flags: string, desc: string, opts?: ICommandOption<O, A> | IFlagValueHandler ): this {

if ( typeof opts === 'function' ) {
return this.option( flags, desc, { value: opts } );
Expand All @@ -335,7 +335,7 @@ export class BaseCommand<O = any> {

const args: IArgumentDetails[] = result.typeDefinition ? this.parseArgsDefinition( result.typeDefinition ) : [];

const option: IOption<O> = {
const option: IOption<O, A> = {
name: '',
description: desc,
args,
Expand Down Expand Up @@ -442,7 +442,7 @@ export class BaseCommand<O = any> {
* @param args Command line args to parse. Ex: `cmd.parse( Deno.args )`
* @param dry Execute command after parsed.
*/
public async parse( args: string[], dry?: boolean ): Promise<IFlagsParseResult<O>> {
public async parse( args: string[], dry?: boolean ): Promise<IFlagsParseResult<O, A>> {

// if ( !this.name ) {
// throw new Error( 'Missing command name' );
Expand All @@ -464,15 +464,15 @@ export class BaseCommand<O = any> {
await this.executeExecutable( this.rawArgs );
}

return { options: {} as O, args: this.rawArgs, cmd: this };
return { options: {} as O, args: this.rawArgs as any as A, cmd: this };

} else if ( this._useRawArgs ) {

if ( dry ) {
return { options: {} as O, args: this.rawArgs, cmd: this };
return { options: {} as O, args: this.rawArgs as any as A, cmd: this };
}

return await this.execute( {} as O, ...this.rawArgs );
return await this.execute( {} as O, ...this.rawArgs as A );

} else {

Expand All @@ -483,7 +483,7 @@ export class BaseCommand<O = any> {
this.validateEnvVars();

if ( dry ) {
return { options: flags, args: params, cmd: this };
return { options: flags, args: params as any as A, cmd: this };
}

return await this.execute( flags, ...params );
Expand All @@ -496,13 +496,13 @@ export class BaseCommand<O = any> {
* @param options A map of options.
* @param args Command arguments.
*/
protected async execute( options: O, ...args: IFlagValue[] ): Promise<IFlagsParseResult<O>> {
protected async execute( options: O, ...args: A ): Promise<IFlagsParseResult<O, A>> {

const actionOption = this.findActionFlag( options, args );
const actionOption = this.findActionFlag( options );

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

if ( this.fn ) {
Expand All @@ -526,7 +526,7 @@ export class BaseCommand<O = any> {
}
}

return { options, args, cmd: this };
return { options, args: args as any as A, cmd: this };
}

/**
Expand Down Expand Up @@ -655,7 +655,7 @@ export class BaseCommand<O = any> {
/**
* Match commands and arguments from command line arguments.
*/
protected parseArguments( args: string[], flags: O ): IFlagValue[] {
protected parseArguments( args: string[], flags: O ): A {

const params: IFlagValue[] = [];

Expand Down Expand Up @@ -688,7 +688,7 @@ export class BaseCommand<O = any> {
}
}

return params;
return params as A;
}

for ( const expectedArg of this.getArguments() ) {
Expand All @@ -715,7 +715,7 @@ export class BaseCommand<O = any> {
}
}

return params;
return params as A;
}

/**
Expand Down Expand Up @@ -783,9 +783,8 @@ export class BaseCommand<O = any> {
* Execute help command if help flag is set.
*
* @param flags Command options.
* @param args Command arguments.
*/
protected findActionFlag( flags: O, args: IFlagValue[] ): IOption<O> | undefined {
protected findActionFlag( flags: O ): IOption<O, A> | undefined {

const flagNames = Object.keys( flags );

Expand Down
2 changes: 1 addition & 1 deletion packages/command/lib/command.ts
Expand Up @@ -12,7 +12,7 @@ import { DefaultCommand } from './default-command.ts';
* -h, --help Output's autogenerated help.
* -V, --version Output's version number
*/
export class Command<O = any> extends DefaultCommand<O> {
export class Command<O = any, A extends Array<any> = any> extends DefaultCommand<O, A> {

public constructor() {

Expand Down
2 changes: 1 addition & 1 deletion packages/command/lib/default-command.ts
Expand Up @@ -10,7 +10,7 @@ import { BaseCommand } from './base-command.ts';
* -h, --help Output's autogenerated help.
* -V, --version Output's version number
*/
export class DefaultCommand<O = any> extends BaseCommand<O> {
export class DefaultCommand<O = any, A extends Array<any> = any> extends BaseCommand<O, A> {

public constructor() {

Expand Down
14 changes: 7 additions & 7 deletions packages/command/lib/types.ts
@@ -1,5 +1,5 @@
import { BaseCommand } from '../../command/lib/base-command.ts';
import { IFlagArgument, IFlagOptions, IFlagValue, IGenericObject, OptionType } from '../../flags/lib/types.ts';
import { IFlagArgument, IFlagOptions, IGenericObject, OptionType } from '../../flags/lib/types.ts';

/** Command map. */
export interface CommandMap<O = any> {
Expand All @@ -9,7 +9,7 @@ export interface CommandMap<O = any> {
}

/** Action handler. */
export type IAction<O> = ( options: O, ...args: any[] ) => void | Promise<void>;
export type IAction<O, A extends Array<any>> = ( 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 All @@ -25,7 +25,7 @@ export interface IArgumentDetails extends IFlagArgument {
}

/** Command settings. */
export interface ICommandOption<O> extends Omit<Omit<Omit<Omit<Omit<Omit<Omit<IFlagOptions,
export interface ICommandOption<O, A extends Array<any>> extends Omit<Omit<Omit<Omit<Omit<Omit<Omit<IFlagOptions,
'name'>,
'args'>,
'type'>,
Expand All @@ -34,11 +34,11 @@ export interface ICommandOption<O> extends Omit<Omit<Omit<Omit<Omit<Omit<Omit<IF
'variadic'>,
'list'> {
override?: boolean;
action?: IAction<O>;
action?: IAction<O, A>;
}

/** Command option setting's. */
export interface IOption<O = any> extends ICommandOption<O>, IFlagOptions {
export interface IOption<O = any, A extends Array<any> = any> extends ICommandOption<O, A>, IFlagOptions {
description: string,
flags: string;
typeDefinition?: string;
Expand All @@ -60,9 +60,9 @@ export interface IExample {
}

/** Result of `cmd.parse()`. */
export interface IFlagsParseResult<O> {
export interface IFlagsParseResult<O, A> {
options: O,
args: IFlagValue[]
args: A
cmd: BaseCommand<O>;
}

Expand Down

0 comments on commit 8a153a7

Please sign in to comment.