Split styleItemDescription into option/subcommand/argument (#2283)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by GitHub · parent 5629947 · Browse files · 6 files·+63 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+63 added−26 removed
- tests:+42 added−10 removed
- other:+21 added−16 removed
Provenance
- Commit
437e4e3- Tree
ec98aa1- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
5629947
6 files changed+63 added−26 removed
modified:examples/color-help-replacement.mjs+1 added-1 removed
45 unmodified lines4647484950505152535459 unmodified lines45 unmodified lines } styleCommandDescription(str) { return this.chalk.magenta(str); } styleItemDescription(str) { styleDescriptionText(str) { return this.chalk.italic(str); } styleOptionText(str) { return this.chalk.green(str);59 unmodified linesmodified:examples/color-help.mjs+1 added-1 removed
7 unmodified lines89101112121314151625 unmodified lines7 unmodified linesprogram.configureHelp({ styleTitle: (str) => styleText('bold', str), styleCommandText: (str) => styleText('cyan', str), styleCommandDescription: (str) => styleText('magenta', str), styleItemDescription: (str) => styleText('italic', str), styleDescriptionText: (str) => styleText('italic', str), styleOptionText: (str) => styleText('green', str), styleArgumentText: (str) => styleText('yellow', str), styleSubcommandText: (str) => styleText('blue', str),});25 unmodified linesmodified:lib/help.js+13 added-12 removed
399 unmodified lines400 const termWidth = helper.padWidth(cmd, helper);401 const helpWidth = helper.helpWidth ?? 80; // in case prepareContext() was not called402 403 function callFormatItem(term, description) {404- return helper.formatItem(405- term,406- termWidth,407- helper.styleItemDescription(description),408- helper,409- );404+ return helper.formatItem(term, termWidth, description, helper);405 }406 407 // Usage408 let output = [16 unmodified lines425 // Arguments426 const argumentList = helper.visibleArguments(cmd).map((argument) => {427 return callFormatItem(428 helper.styleArgumentTerm(helper.argumentTerm(argument)),434- helper.argumentDescription(argument),429+ helper.styleArgumentDescription(helper.argumentDescription(argument)),430 );431 });432 if (argumentList.length > 0) {433 output = output.concat([6 unmodified lines440 // Options441 const optionList = helper.visibleOptions(cmd).map((option) => {442 return callFormatItem(443 helper.styleOptionTerm(helper.optionTerm(option)),449- helper.optionDescription(option),444+ helper.styleOptionDescription(helper.optionDescription(option)),445 );446 });447 if (optionList.length > 0) {448 output = output.concat([8 unmodified lines457 .visibleGlobalOptions(cmd)458 .map((option) => {459 return callFormatItem(460 helper.styleOptionTerm(helper.optionTerm(option)),466- helper.optionDescription(option),461+ helper.styleOptionDescription(helper.optionDescription(option)),462 );463 });464 if (globalOptionList.length > 0) {465 output = output.concat([7 unmodified lines473 // Commands474 const commandList = helper.visibleCommands(cmd).map((cmd) => {475 return callFormatItem(476 helper.styleSubcommandTerm(helper.subcommandTerm(cmd)),482- helper.subcommandDescription(cmd),477+ helper.styleSubcommandDescription(helper.subcommandDescription(cmd)),478 );479 });480 if (commandList.length > 0) {481 output = output.concat([39 unmodified lines521 return this.styleCommandText(word); // Restrict to initial words?522 })523 .join(' ');524 }530- styleItemDescription(str) {525+ styleCommandDescription(str) {526 return this.styleDescriptionText(str);527 }533- styleCommandDescription(str) {528+ styleOptionDescription(str) {529 return this.styleDescriptionText(str);530 }531+ styleSubcommandDescription(str) {532+ return this.styleDescriptionText(str);533+ }534+ styleArgumentDescription(str) {535+ return this.styleDescriptionText(str);536+ }537 styleDescriptionText(str) {538 return str;539 }540 styleOptionTerm(str) {169 unmodified linesmodified:tests/help.style.test.js+42 added-10 removed
52 unmodified lines53 plainHelpInformation.replace(usageString, red(usageString)),54 );55 });56 57- test('styleItemDescription', () => {57+ test('styleCommandDescription', () => {58 const program = makeProgram();59 program.configureHelp({60- styleItemDescription: (str) => red(str),60+ styleCommandDescription: (str) => red(str),61 displayWidth,62 });63 const helpText = program.helpInformation();64 expect(helpText).toEqual(65- plainHelpInformation66- .replace('arg description', red('arg description'))67- .replace('sub description', red('sub description'))68- .replace(/display help for command/g, red('display help for command')),65+ plainHelpInformation.replace(66+ 'program description',67+ red('program description'),68+ ),69 );70 });71 72- test('styleCommandDescription', () => {72+ test('styleOptionDescription', () => {73 const program = makeProgram();74 program.configureHelp({75- styleCommandDescription: (str) => red(str),75+ styleOptionDescription: (str) => red(str),76 displayWidth,77 });78 const helpText = program.helpInformation();79 expect(helpText).toEqual(80 plainHelpInformation.replace(81- 'program description',82- red('program description'),81+ /(-h, --help *)(display help for command)/,82+ (match, p1, p2) => p1 + red(p2),83 ),84 );85 });86 87+ test('styleSubcommandDescription', () => {88+ const program = makeProgram();89+ program.configureHelp({90+ styleSubcommandDescription: (str) => red(str),91+ displayWidth,92+ });93+ const helpText = program.helpInformation();94+ expect(helpText).toEqual(95+ plainHelpInformation96+ .replace(97+ /(\[subarg\] *)(sub description)/,98+ (match, p1, p2) => p1 + red(p2),99+ )100+ .replace(101+ /(help \[command\] *)(display help for command)/,102+ (match, p1, p2) => p1 + red(p2),103+ ),104+ );105+ });106+107+ test('styleArgumentDescription', () => {108+ const program = makeProgram();109+ program.configureHelp({110+ styleArgumentDescription: (str) => red(str),111+ displayWidth,112+ });113+ const helpText = program.helpInformation();114+ expect(helpText).toEqual(115+ plainHelpInformation.replace('arg description', red('arg description')),116+ );117+ });118+119 test('styleDescriptionText', () => {120 const program = makeProgram();121 program.configureHelp({122 styleDescriptionText: (str) => red(str),158 unmodified linesmodified:typings/index.d.ts+3 added-1 removed
269 unmodified lines270 styleUsage(str: string): string;271 /** Style for command name in usage string. */272 styleCommandText(str: string): string;273 274- styleItemDescription(str: string): string;274 styleCommandDescription(str: string): string;275+ styleOptionDescription(str: string): string;276+ styleSubcommandDescription(str: string): string;277+ styleArgumentDescription(str: string): string;278 /** Base style used by descriptions. */279 styleDescriptionText(str: string): string;280 281 styleOptionTerm(str: string): string;748 unmodified linesmodified:typings/index.test-d.ts+3 added-1 removed
603 unmodified lines604 605 expectType<string>(helper.styleUsage('foo [options] <file>'));606 expectType<string>(helper.styleCommandText('foo'));607 608-expectType<string>(helper.styleItemDescription('description'));608 expectType<string>(helper.styleCommandDescription('description'));609+expectType<string>(helper.styleOptionDescription('description'));610+expectType<string>(helper.styleSubcommandDescription('description'));611+expectType<string>(helper.styleArgumentDescription('description'));612 expectType<string>(helper.styleDescriptionText('description'));613 614 expectType<string>(helper.styleOptionTerm('-a, --all'));615 expectType<string>(helper.styleSubcommandTerm('bar [options]'));123 unmodified lines