Skip to content

Commit

Permalink
feat(command): add .getHelp() method to HelpCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 20, 2020
1 parent 0e2e860 commit 9b96d10
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 29 deletions.
66 changes: 37 additions & 29 deletions packages/command/commands/help.ts
@@ -1,3 +1,4 @@
import { encode } from 'https://deno.land/std@v0.50.0/encoding/utf8.ts';
import { blue, bold, dim, green, magenta, red, yellow } from 'https://deno.land/std@v0.50.0/fmt/colors.ts';
import { IFlagOptions, IFlags } from '../../flags/lib/types.ts';
import { Table } from '../../table/lib/table.ts';
Expand Down Expand Up @@ -27,76 +28,81 @@ export class HelpCommand extends BaseCommand implements IHelpCommand {
} );
}

public show( name?: string ) {
Deno.stdout.writeSync( encode( this.getHelp( name ) ) );
}

/**
* Render help output.
*/
public show( name?: string ): void {
public getHelp( name?: string ): string {

const cmd: BaseCommand = name ? this.parent.getCommand( name ) : this.parent;

let output = '';
const indent = 2;

const renderHelp = () => {

// Header
renderLine();
Table.from( getHeader() )
.indent( indent )
.padding( 1 )
.render();
output += Table.from( getHeader() )
.indent( indent )
.padding( 1 )
.toString();

// Description
if ( cmd.getDescription() ) {
renderLabel( 'Description' );
Table.from( getDescription() )
.indent( indent * 2 )
.maxCellWidth( 140 )
.padding( 1 )
.render();
output += Table.from( getDescription() )
.indent( indent * 2 )
.maxCellWidth( 140 )
.padding( 1 )
.toString();
}

// Options
if ( cmd.hasOptions() ) {
renderLabel( 'Options' );
Table.from( getOptions() )
.padding( [ 2, 2, 1, 2 ] )
.indent( indent * 2 )
.maxCellWidth( [ 60, 60, 80, 60 ] )
.render();
output += Table.from( getOptions() )
.padding( [ 2, 2, 1, 2 ] )
.indent( indent * 2 )
.maxCellWidth( [ 60, 60, 80, 60 ] )
.toString();
}

// Commands
if ( cmd.hasCommands() ) {
renderLabel( 'Commands' );
Table.from( getCommands() )
.padding( [ 2, 2, 1, 2 ] )
.indent( indent * 2 )
.render();
output += Table.from( getCommands() )
.padding( [ 2, 2, 1, 2 ] )
.indent( indent * 2 )
.toString();
}

// Environment variables
if ( cmd.hasEnvVars() ) {
renderLabel( 'Environment variables' );
Table.from( getEnvVars() )
.padding( 2 )
.indent( indent * 2 )
.render();
output += Table.from( getEnvVars() )
.padding( 2 )
.indent( indent * 2 )
.toString();
}

// Examples
if ( cmd.hasExamples() ) {
renderLabel( 'Examples' );
Table.from( getExamples() )
.padding( 1 )
.indent( indent * 2 )
.maxCellWidth( 150 )
.render();
output += Table.from( getExamples() )
.padding( 1 )
.indent( indent * 2 )
.maxCellWidth( 150 )
.toString();
}

renderLine();
};

const renderLine = ( ...args: any[] ) => this.log( ...args );
const renderLine = ( ...args: any[] ) => output += ( args.length ? ' '.repeat( indent ) + format( ...args ) : '' ) + '\n';

const renderLabel = ( label: string ) => {
renderLine();
Expand Down Expand Up @@ -189,6 +195,8 @@ export class HelpCommand extends BaseCommand implements IHelpCommand {
};

renderHelp();

return output;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/command/lib/types.ts
Expand Up @@ -74,6 +74,7 @@ export type ICompleteHandlerMap = IGenericObject<ICompleteHandler>

export interface IHelpCommand<O = any> extends BaseCommand<O> {
show( name?: string ): void;
getHelp( name?: string ): string;
}

export function isHelpCommand<O = any>( cmd: BaseCommand<O> ): cmd is IHelpCommand<O> {
Expand Down
57 changes: 57 additions & 0 deletions packages/command/test/command/help-command_test.ts
@@ -0,0 +1,57 @@
import { stripeColors } from '../../../table/lib/utils.ts';
import { Command } from '../../lib/command.ts';
import { assertEquals } from '../lib/assert.ts';

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

const cmd: Command = new Command()
.throwErrors()
.version( '1.0.0' )
.description( 'Test description ...' )
.option( '-t, --test [val:string]', 'test description' )
.option( '-D, --default [val:string]', 'I have a default value!', { default: 'test' } )
.option( '-r, --required [val:string]', 'I am required!', { required: true } )
.option( '-H, --hidden [val:string]', 'Nobody knows about me!', { hidden: true } )
.option( '-d, --depends [val:string]', 'I depend on test!', { depends: [ 'test' ] } )
.option( '-c, --conflicts [val:string]', 'I conflict with test!', { conflicts: [ 'test' ] } )
.option( '-a, --all <val:string>', 'I have many hints!', {
default: 'test',
required: true,
depends: [ 'test' ],
conflicts: [ 'depends' ]
} )
.command( 'sub-command <input:string> <output:string>' )
.description( 'sub command description.' )
.command( 'hidden-command <input:string> <output:string>' )
.description( 'Nobody knows about me!' )
.hidden();

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.
-t, --test [val:string] - test description
-D, --default [val:string] - I have a default value! (Default: test)
-r, --required [val:string] - I am required! (required)
-d, --depends [val:string] - I depend on test! (depends: test)
-c, --conflicts [val:string] - I conflict with test! (conflicts: test)
-a, --all <val:string> - I have many hints! (required, Default: test, depends: test, conflicts: depends)
Commands:
help [command:command] - Show this help or the help of a sub-command.
completions - Generate shell completions for zsh and bash.
sub-command <input:string> <output:string> - sub command description.
` );
} );

0 comments on commit 9b96d10

Please sign in to comment.