Skip to content

Commit

Permalink
feat(command): add support for hidden options's #23
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 20, 2020
1 parent 1866b75 commit 42f701f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/command/lib/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,18 +891,26 @@ export class BaseCommand<O = any, A extends Array<any> = any> {
/**
* Checks whether the command has options or not.
*/
public hasOptions(): boolean {
public hasOptions( includeHidden?: boolean ): boolean {

return this.options.length > 0;
if ( includeHidden ) {
return this.options.length > 0;
}

return this.options.filter( opt => !opt.hidden ).length > 0;
}

public getOptions(): IOption<O>[] {
public getOptions( includeHidden?: boolean ): IOption<O>[] {

if ( includeHidden ) {
return this.options;
}

return this.options;
return this.options.filter( opt => !opt.hidden );
}

/**
* Checks whether the command has an option with given name not.
* Checks whether the command has an option with given name or not.
*/
public hasOption( name: string ): boolean {

Expand Down
1 change: 1 addition & 0 deletions packages/command/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface ICommandOption<O, A extends Array<any>> extends Omit<Omit<Omit<
'variadic'>,
'list'> {
override?: boolean;
hidden?: boolean;
action?: IAction<O, A>;
}

Expand Down
47 changes: 47 additions & 0 deletions packages/command/test/option/hidden_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 ...' )
.option( '-h, --hidden <value:string>', 'Nobody knows about me!', { hidden: true } )
.hidden();
}

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

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

assertEquals( options, { hidden: 'test' } );
assertEquals( args, [] );
} );

Deno.test( 'hidden option 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 42f701f

Please sign in to comment.