Skip to content
Myr

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

6 files+63 added−26 removed

modified:examples/color-help-replacement.mjs+1 added-1 removed
modified:examples/color-help.mjs+1 added-1 removed
modified:lib/help.js+13 added-12 removed
399 unmodified lines
400 const termWidth = helper.padWidth(cmd, helper);401 const helpWidth = helper.helpWidth ?? 80; // in case prepareContext() was not called402403 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 }406407 // Usage408 let output = [
16 unmodified lines
425 // 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 lines
440 // 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 lines
457 .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 lines
473 // 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 lines
521 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 lines
modified:tests/help.style.test.js+42 added-10 removed
52 unmodified lines
53 plainHelpInformation.replace(usageString, red(usageString)),54 );55 });5657 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 });7172 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 });8687 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 });106107 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 });118119 test('styleDescriptionText', () => {120 const program = makeProgram();121 program.configureHelp({122 styleDescriptionText: (str) => red(str),
158 unmodified lines
modified:typings/index.d.ts+3 added-1 removed
269 unmodified lines
270 styleUsage(str: string): string;271 /** Style for command name in usage string. */272 styleCommandText(str: string): string;273274 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;280281 styleOptionTerm(str: string): string;
748 unmodified lines
modified:typings/index.test-d.ts+3 added-1 removed
603 unmodified lines
604605expectType<string>(helper.styleUsage('foo [options] <file>'));606expectType<string>(helper.styleCommandText('foo'));607608expectType<string>(helper.styleItemDescription('description'));608expectType<string>(helper.styleCommandDescription('description'));609expectType<string>(helper.styleOptionDescription('description'));610expectType<string>(helper.styleSubcommandDescription('description'));611expectType<string>(helper.styleArgumentDescription('description'));612expectType<string>(helper.styleDescriptionText('description'));613614expectType<string>(helper.styleOptionTerm('-a, --all'));615expectType<string>(helper.styleSubcommandTerm('bar [options]'));
123 unmodified lines