Skip to content

Commit

Permalink
feat(args): add defaultHint opt, update usage()
Browse files Browse the repository at this point in the history
- add UsageOpts.showDefaults to display arg default vals
- add ArgSpecBase.defaultHint for default vals for display purposes
  • Loading branch information
postspectacular committed Jan 11, 2021
1 parent 841b062 commit f8a4146
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/args/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface ArgSpecBase {
alias?: string;
desc?: string;
hint?: string;
defaultHint?: string;
fn?: Fn<string, boolean>;
}

Expand Down Expand Up @@ -92,16 +93,29 @@ export interface UsageOpts {
* @defaultValue true
*/
color: Partial<ColorTheme> | false;
/**
* If true (default), display argument default values.
*
* @defaultValue true
*/
showDefaults: boolean;
}

/**
* Color theme for {@link usage}. Each item is an ANSI color code:
*
* https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
*/
export interface ColorTheme {
default: number;
hint: number;
multi: number;
param: number;
required: number;
}

export const DEFAULT_THEME: ColorTheme = {
default: 95,
hint: 90,
multi: 90,
param: 96,
Expand Down
15 changes: 14 additions & 1 deletion packages/args/src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
kebab,
padRight,
repeat,
stringify,
stripAnsi,
wordWrapLines,
} from "@thi.ng/strings";
Expand All @@ -15,6 +16,7 @@ export const usage = <T extends IObjectOf<any>>(
opts = {
lineWidth: 80,
paramWidth: 32,
showDefaults: true,
...opts,
};
const theme =
Expand Down Expand Up @@ -45,10 +47,21 @@ export const usage = <T extends IObjectOf<any>>(
isRequired ? theme.required! : theme.multi!
)
: "";
const defaults =
opts.showDefaults && spec.default
? ansi(
` (default: ${stringify()(
spec.defaultHint != undefined
? spec.defaultHint
: spec.default
)})`,
theme.default
)
: "";
return (
padRight(opts.paramWidth!)(params, stripAnsi(params).length) +
wordWrapLines(
prefix + (spec.desc || ""),
prefix + (spec.desc || "") + defaults,
opts.lineWidth! - opts.paramWidth!
)
.map((l, i) => (i > 0 ? indent : "") + l)
Expand Down

0 comments on commit f8a4146

Please sign in to comment.