Skip to content

Commit

Permalink
refactor(command): add version option only if version is set
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Aug 5, 2020
1 parent 6968c1d commit 32e6687
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
12 changes: 6 additions & 6 deletions packages/command/lib/command.ts
Expand Up @@ -40,7 +40,7 @@ export class Command<O = any, A extends Array<any> = any> {
private _name: string = 'COMMAND';
private _parent?: Command;
private _globalParent?: Command;
private ver: string = '0.0.0';
private ver?: string;
private desc: IDescription = '';
private fn?: IAction<O, A>;
private options: IOption<O, A>[] = [];
Expand Down Expand Up @@ -606,22 +606,22 @@ export class Command<O = any, A extends Array<any> = any> {

private registerDefaults(): this {

if ( this.getParent() || this.hasDefaults ) {
if ( this.hasDefaults || this.getParent() ) {
return this;
}
this.hasDefaults = true;

this.reset();

if ( this._versionOption !== false ) {
if ( this.ver && this._versionOption !== false ) {
this.option(
this._versionOption?.flags || '-V, --version',
this._versionOption?.desc || 'Show the version number for this program.',
Object.assign( {
standalone: true,
prepend: true,
action: async function ( this: Command ) {
await Deno.stdout.writeSync( encode( this.getVersion() + '\n' ) );
await Deno.stdout.writeSync( encode( this.ver + '\n' ) );
Deno.exit( 0 );
}
}, this._versionOption?.opts ?? {} ) );
Expand Down Expand Up @@ -962,8 +962,8 @@ export class Command<O = any, A extends Array<any> = any> {
/**
* Get command version.
*/
public getVersion(): string {
return this.ver || ( this._parent?.getVersion() ?? '' );
public getVersion(): string | undefined {
return this.ver ?? this._parent?.getVersion();
}

/**
Expand Down
12 changes: 8 additions & 4 deletions packages/command/lib/help-generator.ts
Expand Up @@ -27,11 +27,15 @@ export class HelpGenerator {
}

private generateHeader(): string {
const rows = [
[ bold( 'Usage:' ), magenta( `${ this.cmd.getPath() }${ this.cmd.getArgsDefinition() ? ' ' + this.cmd.getArgsDefinition() : '' }` ) ]
];
const version: string | undefined = this.cmd.getVersion();
if ( version ) {
rows.push( [ bold( 'Version:' ), yellow( `v${ this.cmd.getVersion() }` ) ] );
}
return '\n' +
Table.from( [
[ bold( 'Usage:' ), magenta( `${ this.cmd.getName() }${ this.cmd.getArgsDefinition() ? ' ' + this.cmd.getArgsDefinition() : '' }` ) ],
[ bold( 'Version:' ), yellow( `v${ this.cmd.getVersion() }` ) ]
] )
Table.from( rows )
.indent( this.indent )
.padding( 1 )
.toString() + '\n';
Expand Down
7 changes: 4 additions & 3 deletions packages/command/lib/zsh-completions-generator.ts
Expand Up @@ -24,14 +24,15 @@ export class ZshCompletionsGenerator {
*/
private generate(): string {

const version: string | undefined = this.cmd.getVersion();
const versionStr = version ? `# version: ${ this.cmd.getVersion() || '-' }\n#\n` : '';

return `
# compdef _${ snakeCase( this.cmd.getPath() ) } ${ this.cmd.getPath() }
#
# zsh completion for ${ this.cmd.getPath() }
#
# version: ${ this.cmd.getVersion() }
#
${versionStr}
autoload -U is-at-least
(( $+functions[__${ snakeCase( this.cmd.getName() ) }_complete] )) ||
Expand Down

0 comments on commit 32e6687

Please sign in to comment.