Skip to content

Commit aa595f8

Browse files
committed
example output not mandatory + add example tests [skip publish]
1 parent 1c8e9be commit aa595f8

5 files changed

Lines changed: 116 additions & 23 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"clean": "rm -rf build",
1919
"build": "echo $(pwd); yarn clean && tsc -p tsconfig.build.json && cp package.json README.md src/*.d.ts build",
2020
"develop": "tsc --watch",
21-
"test": "jest && jest-coverage-badges"
21+
"test": "jest"
2222
},
2323
"devDependencies": {
2424
"@types/jest": "^26.0.24",

src/assertions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export function assertOption(option: OptionDef<any, any>, allOptions: OptionDef<
111111

112112
export function assertExample(example: ExampleDef) {
113113
assertType(example.input, "Example", "Input", { required: true, type: "string" })
114-
assertType(example.output, "Example", "Output", { required: true, type: "string" })
114+
assertType(example.output, "Example", "Output", { type: "string" })
115115
assertType(example.description, "Example", "Description", { type: "string" })
116116
}
117117

src/index.ts

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
286286
if (example.description) {
287287
lines.push(
288288
...wrap(this.color(titleColors, example.description), {
289-
colorCount: this._help.useColors ? colorCount(titleColors) : 0,
290-
firstLineIndent: 2,
289+
colorCount: this.colorCount(titleColors),
290+
indent: 2,
291291
printWidth: this._help.printWidth,
292292
})
293293
)
@@ -300,22 +300,30 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
300300
" "
301301
),
302302
{
303-
colorCount: this._help.useColors ? colorCount(highlightColors) : 0,
303+
colorCount: this.colorCount(highlightColors),
304304
firstLineIndent: 2,
305+
indent: 3 + this._help.exampleInputPrefix.length,
306+
// indent: this.colorCount(normalColors) + 4,
305307
printWidth: this._help.printWidth,
306308
}
307309
)
308310
)
309-
lines.push(
310-
...wrap(
311-
[this.color(normalColors, this._help.exampleOutputPrefix), this.color(bodyColors, example.output)].join(" "),
312-
{
313-
colorCount: this._help.useColors ? colorCount(bodyColors) : 0,
314-
firstLineIndent: 2,
315-
printWidth: this._help.printWidth,
316-
}
311+
if (example.output) {
312+
lines.push(
313+
...wrap(
314+
[this.color(normalColors, this._help.exampleOutputPrefix), this.color(bodyColors, example.output)].join(
315+
" "
316+
),
317+
{
318+
colorCount: this.colorCount(bodyColors),
319+
firstLineIndent: 2,
320+
indent: 3 + this._help.exampleOutputPrefix.length,
321+
// indent: this.colorCount(normalColors) + 4,
322+
printWidth: this._help.printWidth,
323+
}
324+
)
317325
)
318-
)
326+
}
319327
lines.push("")
320328
}
321329
return lines
@@ -405,20 +413,18 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
405413

406414
for (const item of list) {
407415
const cmdName = this.color(highlightColors, `${item.name}`).padEnd(
408-
nameFullSize + (this._help.useColors ? colorCount(highlightColors) : 0) * COLOR_CODE_LEN,
416+
nameFullSize + this.colorCount(highlightColors) * COLOR_CODE_LEN,
409417
" "
410418
)
411419
const cmdDescr = this.color(normalColors, item.description ?? "")
412420

413421
for (const line of wrap(cmdName + cmdDescr, {
414422
indent: nameFullSize + INDENT_LEN,
415-
colorCount: this._help.useColors
416-
? colorCount(
417-
normalColors,
418-
highlightColors,
419-
item.additionalColorCount ? new Array({ length: item.additionalColorCount }) : []
420-
)
421-
: 0,
423+
colorCount: this.colorCount(
424+
normalColors,
425+
highlightColors,
426+
item.additionalColorCount ? new Array({ length: item.additionalColorCount }) : []
427+
),
422428
firstLineIndent: INDENT_LEN,
423429
printWidth: this._help.printWidth,
424430
})) {
@@ -532,6 +538,13 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
532538
}
533539
return chalk.reset(output)
534540
}
541+
542+
private colorCount(...colors: any[]): number {
543+
if (!this._help.useColors) {
544+
return 0
545+
}
546+
return colorCount(...colors)
547+
}
535548
}
536549

537550
export function massarg<T extends OptionsBase = OptionsBase>() {

src/options.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export interface ExampleDef {
254254
input: string
255255

256256
/** The output line - to show the output of whatever `input` is regarding. */
257-
output: string
257+
output?: string
258258

259259
/** An optional description which will be used as a title. */
260260
description?: string

tests/examples.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import massarg from "../src"
2+
3+
describe("Examples", () => {
4+
test("should parse examples", () => {
5+
const helpStr = massarg()
6+
.help({ binName: "test", useColors: false })
7+
.example({
8+
input: "my-cmd --number 10",
9+
})
10+
.getHelpString()
11+
.join("\n")
12+
13+
expect(helpStr).toBe(
14+
"Usage: test [command] [options]" +
15+
"\n\n" +
16+
"Options:" +
17+
"\n\n" +
18+
" --help|-h Display help information" +
19+
"\n\n" +
20+
"Examples:" +
21+
"\n\n" +
22+
" $ my-cmd --number 10" +
23+
"\n"
24+
)
25+
})
26+
27+
test("should wrap input properly", () => {
28+
const helpStr = massarg()
29+
.help({ binName: "test", useColors: false })
30+
.example({
31+
input: "my-cmd --number 10 --another-number 20 --and-yet-another-number 30 --what-about-another 40",
32+
})
33+
.getHelpString()
34+
.join("\n")
35+
36+
expect(helpStr).toBe(
37+
"Usage: test [command] [options]" +
38+
"\n\n" +
39+
"Options:" +
40+
"\n\n" +
41+
" --help|-h Display help information" +
42+
"\n\n" +
43+
"Examples:" +
44+
"\n\n" +
45+
" $ my-cmd --number 10 --another-number 20 --and-yet-another-number 30" +
46+
"\n" +
47+
" --what-about-another 40" +
48+
"\n"
49+
)
50+
})
51+
52+
test("should wrap description properly", () => {
53+
const helpStr = massarg()
54+
.help({ binName: "test", useColors: false })
55+
.example({
56+
input: "my-cmd --number 10",
57+
description:
58+
"This is a really long test. Very long text indeed, which should eventually span to more than 1 line, so indentation can be tested.",
59+
})
60+
.getHelpString()
61+
.join("\n")
62+
63+
expect(helpStr).toBe(
64+
"Usage: test [command] [options]" +
65+
"\n\n" +
66+
"Options:" +
67+
"\n\n" +
68+
" --help|-h Display help information" +
69+
"\n\n" +
70+
"Examples:" +
71+
"\n\n" +
72+
" This is a really long test. Very long text indeed, which should eventually" +
73+
"\n" +
74+
" span to more than 1 line, so indentation can be tested." +
75+
"\n\n" +
76+
" $ my-cmd --number 10" +
77+
"\n"
78+
)
79+
})
80+
})

0 commit comments

Comments
 (0)