Skip to content

Commit

Permalink
feat(command): add support for hidden command's #22
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 20, 2020
1 parent 9b96d10 commit 1866b75
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/command/commands/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ or create a separate file in the ${ dim( italic( 'zsh_completion.d' ) ) } direct
.default( 'help' )
.command( 'zsh', new ZshCompletionsCommand( this.parent ) )
.command( 'bash', new BashCompletionsCommand( this.parent ) )
.command( 'complete', new CompleteCommand( this.parent ) )
.command( 'complete', new CompleteCommand( this.parent ).hidden() )
.reset();
}

Expand Down
39 changes: 30 additions & 9 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 { CommandMap, IAction, IArgumentDetails, ICommandOption, ICompleteHandler, ICompleteHandlerMap, IEnvVariable, IExample, IParseResult, IHelpCommand, IOption, isHelpCommand } from './types.ts';
import { CommandMap, IAction, IArgumentDetails, ICommandOption, ICompleteHandler, ICompleteHandlerMap, IEnvVariable, IExample, IHelpCommand, IOption, IParseResult, isHelpCommand } from './types.ts';

const permissions: any = ( Deno as any ).permissions;
const envPermissionStatus: any = permissions && permissions.query && await permissions.query( { name: 'env' } );
Expand Down Expand Up @@ -52,6 +52,7 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
protected defaultCommand: string | undefined;
protected _useRawArgs: boolean = false;
protected args: IArgumentDetails[] = [];
protected isHidden: boolean = false;

/**
* Add new sub-command.
Expand Down Expand Up @@ -146,7 +147,7 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
*/
public reset(): this {
this.cmd = this;
this.getCommands().forEach( cmd => cmd.reset() );
this.getCommands( true ).forEach( cmd => cmd.reset() );
return this;
}

Expand Down Expand Up @@ -195,6 +196,16 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
return this;
}

/**
* Set command description.
*
* @param description Short command description.
*/
public hidden(): this {
this.cmd.isHidden = true;
return this;
}

/**
* Set command arguments.
*
Expand Down Expand Up @@ -665,7 +676,7 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
if ( !this.hasArguments() ) {

if ( args.length ) {
if ( this.hasCommands() ) {
if ( this.hasCommands( true ) ) {
throw this.error( new Error( `Unknown command: ${ args.join( ' ' ) }` ) );
} else {
throw this.error( new Error( `No arguments allowed for command: ${ this._name }` ) );
Expand Down Expand Up @@ -927,25 +938,35 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
/**
* Checks whether the command has sub-commands or not.
*/
public hasCommands(): boolean {
public hasCommands( includeHidden?: boolean ): boolean {

if ( includeHidden ) {
return this.commands.size > 0;
}

return this.commands.size > 0;
return this.getCommandMaps( includeHidden ).length > 0;
}

/**
* Get sub-command maps.
*/
public getCommandMaps(): CommandMap[] {
public getCommandMaps( includeHidden?: boolean ): CommandMap[] {

const cmds: CommandMap[] = Array.from( this.commands.values() );

if ( includeHidden ) {
return cmds;
}

return Array.from( this.commands.values() );
return cmds.filter( ( { cmd }: CommandMap ) => !cmd.isHidden );
}

/**
* Get sub-commands.
*/
public getCommands(): BaseCommand[] {
public getCommands( includeHidden?: boolean ): BaseCommand[] {

return this.getCommandMaps().map( cmd => cmd.cmd );
return this.getCommandMaps( includeHidden ).map( ( { cmd }: CommandMap ) => cmd );
}

/**
Expand Down
48 changes: 48 additions & 0 deletions packages/command/test/command/hidden-command_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { stripeColors } from '../../../table/lib/utils.ts';
import { Command } from '../../lib/command.ts';
import { assertEquals } from '../lib/assert.ts';

function command(): Command {
return new Command()
.throwErrors()
.version( '1.0.0' )
.description( 'Test description ...' )
.command( 'hidden-command <input:string> <output:string>' )
.hidden();
}

Deno.test( 'hidden command', async () => {

const cmd: Command = command();
const { options, args } = await cmd.parse( [ 'hidden-command', 'input-path', 'output-path' ] );

assertEquals( options, {} );
assertEquals( args[ 0 ], 'input-path' );
assertEquals( args[ 1 ], 'output-path' );
} );

Deno.test( 'hidden command help', async () => {

const cmd: Command = command();
const output: string = cmd.getHelpCommand().getHelp();

assertEquals( stripeColors( output ), `
Usage: COMMAND
Version: v1.0.0
Description:
Test description ...
Options:
-h, --help [arg:boolean] - Show this help.
-V, --version [arg:boolean] - Show the version number for this program.
Commands:
help [command:command] - Show this help or the help of a sub-command.
completions - Generate shell completions for zsh and bash.
` );
} );

0 comments on commit 1866b75

Please sign in to comment.