Skip to content

Commit

Permalink
feat(command): add exit option help method (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
wlfio committed Aug 22, 2021
1 parent a6138b2 commit ab38cc7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions command/README.md
Expand Up @@ -1249,6 +1249,8 @@ await new Command()
hints: true, // default: true
// Enable/disable colors.
colors: false, // default: true
// Enable/disable exiting after print
exit: true, // default: true
})
.option("-f, --foo [val:number]", "Some description.", {
required: true,
Expand Down
24 changes: 20 additions & 4 deletions command/command.ts
Expand Up @@ -34,7 +34,7 @@ import { NumberType } from "./types/number.ts";
import { StringType } from "./types/string.ts";
import { Type } from "./type.ts";
import { HelpGenerator } from "./help/_help_generator.ts";
import type { HelpOptions } from "./help/_help_generator.ts";
import type { HelpGeneratorOptions } from "./help/_help_generator.ts";
import type {
IAction,
IArgument,
Expand Down Expand Up @@ -89,6 +89,10 @@ type MapArgumentTypes<A extends Array<unknown>> = A extends Array<unknown>
: // deno-lint-ignore no-explicit-any
any;

export interface HelpOptions extends HelpGeneratorOptions {
exit?: boolean;
}

export class Command<
// deno-lint-ignore no-explicit-any
CO extends Record<string, any> | void = any,
Expand Down Expand Up @@ -133,6 +137,7 @@ export class Command<
private _versionOption?: IDefaultOption | false;
private _helpOption?: IDefaultOption | false;
private _help?: IHelpHandler;
private exitOnHelp: undefined | boolean;

/** Disable version option. */
public versionOption(enable: false): this;
Expand Down Expand Up @@ -432,7 +437,7 @@ export class Command<

/**
* Set command help.
* @param help Help string or method that returns the help string.
* @param help Help string, method, or config for generator that returns the help string.
*/
public help(
help:
Expand All @@ -445,6 +450,7 @@ export class Command<
} else if (typeof help === "function") {
this.cmd._help = help;
} else {
this.cmd.exitOnHelp = help.exit;
this.cmd._help = (cmd: Command): string =>
HelpGenerator.generate(cmd, help);
}
Expand Down Expand Up @@ -671,6 +677,12 @@ export class Command<
return this.cmd.throwOnError || !!this.cmd._parent?.shouldThrowErrors();
}

/** Check wether the command should exit after printing help */
protected shouldExitOnHelp(): boolean {
return this.cmd.exitOnHelp ??
(this.cmd._parent?.shouldExitOnHelp() ?? true);
}

public globalOption<G extends Record<string, unknown> | void = CG>(
flags: string,
desc: string,
Expand Down Expand Up @@ -953,7 +965,9 @@ export class Command<
prepend: true,
action: function () {
this.showVersion();
Deno.exit(0);
if (this.shouldExitOnHelp()) {
Deno.exit(0);
}
},
...(this._versionOption?.opts ?? {}),
},
Expand All @@ -970,7 +984,9 @@ export class Command<
prepend: true,
action: function () {
this.showHelp();
Deno.exit(0);
if (this.shouldExitOnHelp()) {
Deno.exit(0);
}
},
...(this._helpOption?.opts ?? {}),
},
Expand Down
11 changes: 7 additions & 4 deletions command/help/_help_generator.ts
Expand Up @@ -18,7 +18,7 @@ import type { IArgument } from "../types.ts";
import type { IEnvVar, IExample, IOption } from "../types.ts";
import { Type } from "../type.ts";

export interface HelpOptions {
export interface HelpGeneratorOptions {
types?: boolean;
hints?: boolean;
colors?: boolean;
Expand All @@ -27,14 +27,17 @@ export interface HelpOptions {
/** Help text generator. */
export class HelpGenerator {
private indent = 2;
private options: Required<HelpOptions>;
private options: Required<HelpGeneratorOptions>;

/** Generate help text for given command. */
public static generate(cmd: Command, options?: HelpOptions): string {
public static generate(cmd: Command, options?: HelpGeneratorOptions): string {
return new HelpGenerator(cmd, options).generate();
}

private constructor(private cmd: Command, options: HelpOptions = {}) {
private constructor(
private cmd: Command,
options: HelpGeneratorOptions = {},
) {
this.options = {
types: false,
hints: true,
Expand Down

0 comments on commit ab38cc7

Please sign in to comment.