Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colouring fails in options #1651

Closed
GHNewbiee opened this issue Dec 10, 2021 · 5 comments
Closed

Colouring fails in options #1651

GHNewbiee opened this issue Dec 10, 2021 · 5 comments

Comments

@GHNewbiee
Copy link

GHNewbiee commented Dec 10, 2021

The following works:

  .option(`${chalk.cyan('-c')}, --config [file]`, 'the file name')

but not the next one:

  .option(`${chalk.cyan('-c')}, ${chalk.cyan('--config [file]')}`, 'the file name')

It returns:

/.../node_modules/commander/lib/option.js:181
    return str + word[0].toUpperCase() + word.slice(1);

Tia

@shadowspawn
Copy link
Collaborator

First, the problem. Commander is using that first parameter to determine:

  • short flag -c
  • long flag --config
  • whether has option argument [file]
  • whether option-argument is required or optional

Commander uses some simple code to pull apart the parameter. Colouring the text is inserting a bunch of ascii escape sequences into the string, and that is not going to go well...

I tried your first example to check, and although the short flag is displayed in cyan in the help, it does not work on command line.

% node index.js -c fhf
error: unknown option '-c'

@shadowspawn
Copy link
Collaborator

The way to change the format of the help is to modify the Help class, either by a subclass or by overriding individual methods using configureHelp().

See: https://github.com/tj/commander.js#more-configuration-2

For your case, the option flags are supplied by the optionTerm method:

program.configureHelp({
  optionTerm: (option) => {
    return chalk.cyan(option.flags);
  }
});

@GHNewbiee
Copy link
Author

GHNewbiee commented Dec 13, 2021

For your case, the option flags are supplied by the optionTerm method:

I am not sure I fully understand.

In the following option

  .option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0)

there are 5 parts or 4 arguments:

  • -v (as a part),
  • --verbose (as a part)
  • -v, --verbose (as an argument)
  • verbosity that can be increased
  • increaseVerbosity
  • 0

Please, clarify which method can format each part/argument. Tia

@shadowspawn
Copy link
Collaborator

The Help class has methods for what is displayed in the help. The two methods used for each option are .optionTerm() and .optionDescription(). If your colour customisation fits into that pattern, you could supply a custom version of those routines.

The Help class takes the Option created by:

.option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0)

and displays it in the help as "term" and "description":

  -v, --verbose        verbosity that can be increased (default: 0)

The Option class has properties for: short, long, flags, description, parseArg, defaultValue and more.

The default implementation of Help.optionTerm() is:

  optionTerm(option) {
    return option.flags;
  }

@shadowspawn
Copy link
Collaborator

For interest, reopened: #301

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants