Skip to content

Commit

Permalink
refactor(command): remove DefaultCommand class (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Jun 24, 2020
1 parent c7f5a5a commit 9cdc2d2
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 101 deletions.
10 changes: 1 addition & 9 deletions packages/command/commands/completions.ts
@@ -1,14 +1,13 @@
import { bold, dim, italic } from 'https://deno.land/std@v0.52.0/fmt/colors.ts';
import { BaseCommand } from '../lib/base-command.ts';
import { DefaultCommand } from '../lib/default-command.ts';
import { BashCompletionsCommand } from './completions/bash.ts';
import { CompleteCommand } from './completions/complete.ts';
import { ZshCompletionsCommand } from './completions/zsh.ts';

/**
* Generates source code for interactive shell completions used in multiple shell's.
*/
export class CompletionsCommand extends DefaultCommand {
export class CompletionsCommand extends BaseCommand {

public constructor( cmd?: BaseCommand ) {

Expand Down Expand Up @@ -48,11 +47,4 @@ or create a separate file in the ${ dim( italic( 'zsh_completion.d' ) ) } direct
.command( 'complete', new CompleteCommand( cmd ).hidden() )
.reset();
}

/**
* @inheritDoc
*/
public command( nameAndArguments: string, cmd?: BaseCommand | string, override?: boolean ): this {
return super.command( nameAndArguments, cmd || new DefaultCommand(), override );
}
}
10 changes: 1 addition & 9 deletions packages/command/commands/completions/bash.ts
@@ -1,10 +1,9 @@
import { BaseCommand } from '../../lib/base-command.ts';
import { DefaultCommand } from '../../lib/default-command.ts';

/**
* Generates bash completion code.
*/
export class BashCompletionsCommand extends DefaultCommand {
export class BashCompletionsCommand extends BaseCommand {

public constructor( _cmd?: BaseCommand ) {
super();
Expand All @@ -13,11 +12,4 @@ export class BashCompletionsCommand extends DefaultCommand {
throw new Error( 'Bash completions not supported at this moment.' );
} );
}

/**
* @inheritDoc
*/
public command( nameAndArguments: string, cmd?: BaseCommand | string, override?: boolean ): this {
return super.command( nameAndArguments, cmd || new DefaultCommand(), override );
}
}
10 changes: 1 addition & 9 deletions packages/command/commands/completions/complete.ts
@@ -1,13 +1,12 @@
import { encode } from 'https://deno.land/std@v0.52.0/encoding/utf8.ts';
import { IFlags } from '../../../flags/lib/types.ts';
import { BaseCommand } from '../../lib/base-command.ts';
import { DefaultCommand } from '../../lib/default-command.ts';
import { ICompleteSettings } from '../../lib/types.ts';

/**
* Execute complete method for specific action and command.
*/
export class CompleteCommand extends DefaultCommand {
export class CompleteCommand extends BaseCommand {

public constructor( cmd?: BaseCommand ) {
super();
Expand Down Expand Up @@ -36,11 +35,4 @@ export class CompleteCommand extends DefaultCommand {
.default( 'help' )
.reset();
}

/**
* @inheritDoc
*/
public command( nameAndArguments: string, cmd?: BaseCommand | string, override?: boolean ): this {
return super.command( nameAndArguments, cmd || new DefaultCommand(), override );
}
}
10 changes: 1 addition & 9 deletions packages/command/commands/completions/zsh.ts
@@ -1,11 +1,10 @@
import { BaseCommand } from '../../lib/base-command.ts';
import { DefaultCommand } from '../../lib/default-command.ts';
import { ZshCompletionsGenerator } from '../../lib/zsh-completions-generator.ts';

/**
* Generates zsh completion code.
*/
export class ZshCompletionsCommand extends DefaultCommand {
export class ZshCompletionsCommand extends BaseCommand {

public constructor( cmd?: BaseCommand ) {
super();
Expand All @@ -16,11 +15,4 @@ export class ZshCompletionsCommand extends DefaultCommand {
) );
} );
}

/**
* @inheritDoc
*/
public command( nameAndArguments: string, cmd?: BaseCommand | string, override?: boolean ): this {
return super.command( nameAndArguments, cmd || new DefaultCommand(), override );
}
}
41 changes: 30 additions & 11 deletions packages/command/lib/command.ts
@@ -1,6 +1,5 @@
import { CompletionsCommand } from '../commands/completions.ts';
import { BaseCommand } from './base-command.ts';
import { DefaultCommand } from './default-command.ts';
import { IParseResult } from './types.ts';

/**
* A command with pre configured command's and option's:
Expand All @@ -12,17 +11,37 @@ import { DefaultCommand } from './default-command.ts';
* -h, --help Output's autogenerated help.
* -V, --version Output's version number
*/
export class Command<O = any, A extends Array<any> = any> extends DefaultCommand<O, A> {
export class Command<O = any, A extends Array<any> = any> extends BaseCommand<O, A> {

public constructor() {
#hasDefaults: Boolean = false;

super();

this.command( 'completions', new CompletionsCommand() )
.reset();
public parse( args: string[] = Deno.args, dry?: boolean ): Promise<IParseResult<O, A>> {
this.#registerDefaults();
return super.parse( args, dry );
}

public command( nameAndArguments: string, cmd?: BaseCommand | string, override?: boolean ): this {
return super.command( nameAndArguments, cmd || new Command(), override );
}
#registerDefaults = (): this => {
if ( this._parent || this.#hasDefaults ) {
return this;
}
this.#hasDefaults = true;
this.reset()
.option( '-h, --help', 'Show this help.', {
standalone: true,
global: true,
action: function ( this: BaseCommand ) {
this.help();
Deno.exit( 0 );
}
} )
.option( '-V, --version', 'Show the version number for this program.', {
standalone: true,
action: () => {
this.log( this.ver );
Deno.exit( 0 );
}
} );

return this;
};
}
43 changes: 0 additions & 43 deletions packages/command/lib/default-command.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/command/mod.ts
@@ -1,7 +1,8 @@
export * from './lib/types.ts';
export * from './lib/base-command.ts';
export * from './lib/default-command.ts';
export * from './lib/command.ts';
export * from './commands/completions.ts';
export * from './commands/help.ts';
export * from './types/action-list.ts';
export * from './types/boolean.ts';
export * from './types/command-list.ts';
Expand Down
16 changes: 6 additions & 10 deletions packages/command/test/option/hidden_test.ts
Expand Up @@ -7,7 +7,7 @@ function command(): Command {
.throwErrors()
.version( '1.0.0' )
.description( 'Test description ...' )
.option( '-h, --hidden <value:string>', 'Nobody knows about me!', { hidden: true } )
.option( '-H, --hidden <value:string>', 'Nobody knows about me!', { hidden: true } )
.hidden();
}

Expand All @@ -23,9 +23,10 @@ Deno.test( 'hidden option', async () => {
Deno.test( 'hidden option help', async () => {

const cmd: Command = command();
await cmd.parse( [], true );
const output: string = cmd.getHelp();

assertEquals( stripeColors( output ), `
assertEquals( `
Usage: COMMAND
Version: v1.0.0
Expand All @@ -35,13 +36,8 @@ Deno.test( 'hidden option help', async () => {
Options:
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-h, --help - Show this help.
-V, --version - 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.
` );
`, stripeColors( output ) );
} );

0 comments on commit 9cdc2d2

Please sign in to comment.