Skip to content

Commit

Permalink
[FEAT] if --help flag is specified on a cmd, the long help is print…
Browse files Browse the repository at this point in the history
…ed, otherwise the `use` is printed.

[FEAT] When a flag has a conflict on `name` or `short`, it now will print the first match of the conflict to help fix the conflict.
  • Loading branch information
aricart committed Jul 18, 2021
1 parent 9fdd064 commit 74b81b9
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { parse } from "https://deno.land/std/flags/mod.ts";
import { sprintf } from "https://deno.land/std/fmt/printf.ts";

export interface Flag {
type: "string" | "boolean" | "number";
name: string;
usage: string;
short: string;
required: boolean;
persistent: boolean;
changed: boolean;
default: null | unknown | unknown[];
value: null | unknown | unknown[];
}

export interface Flags {
value<T>(n: string): T;
values<T>(n: string): T[];
getFlag(n: string): Flag | null;
checkRequired(): void;
}

export type Run = (
cmd: Command,
args: string[],
Expand All @@ -19,18 +38,6 @@ export function isCommand(t: Command | Cmd): t is Command {
return v.cmd !== undefined;
}

export interface Flag {
type: "string" | "boolean" | "number";
name: string;
usage: string;
short: string;
required: boolean;
persistent: boolean;
changed: boolean;
default: null | unknown | unknown[];
value: null | unknown | unknown[];
}

function flag(f: Partial<Flag>): Flag {
const d = {} as Flag;
d.usage = "";
Expand All @@ -44,13 +51,6 @@ function flag(f: Partial<Flag>): Flag {
return Object.assign(d, f);
}

export interface Flags {
value<T>(n: string): T;
values<T>(n: string): T[];
getFlag(n: string): Flag | null;
checkRequired(): void;
}

export class FlagsImpl implements Flags {
m: Map<string, Flag>;

Expand Down Expand Up @@ -158,11 +158,12 @@ export class Command implements Cmd {
Deno.stderr.writeSync(new TextEncoder().encode(s));
}

help(): void {
help(long = false): void {
// usage for the parent
this.stderr(`${this.short ?? this.use}\n\n`);
if (this.long) {
this.stderr(`${this.long}`);
if (long) {
this.stderr(`${this.long ?? this.short ?? this.use}\n`);
} else {
this.stderr(`${this.short ?? this.use}\n`);
}
this.stderr("\nUsage:\n");
if (!this.commands) {
Expand Down Expand Up @@ -210,7 +211,11 @@ export class Command implements Cmd {
return sn || sh;
});
if (f.length > 0) {
return true;
throw new Error(
`--${flag.name} has conflict with: --${f[0].name} ${
f[0].short ? "-" + f[0].short : ""
}`,
);
}
}
if (this.parent) {
Expand All @@ -224,9 +229,7 @@ export class Command implements Cmd {
pf.name = pf.name ?? "";
pf.short = pf.short ?? "";
this.flags = this.flags ?? [];
if (this.checkFlags(pf)) {
throw new Error(`flag ${pf.name} already exists`);
}
this.checkFlags(pf);
this.flags.push(pf);
return pf;
}
Expand Down Expand Up @@ -422,7 +425,7 @@ export class RootCommand extends Command implements Execute {
this.lastCmd = { cmd: cmd, args: a, flags: fm };

if (this._help.value) {
cmd.help();
cmd.help(true);
this.lastCmd.helped = true;
return Promise.resolve(1);
}
Expand Down

0 comments on commit 74b81b9

Please sign in to comment.