Commander.js: Add color support for help option

Created on 8 Dec 2014  路  10Comments  路  Source: tj/commander.js

Most helpful comment

Sywac isn't well documented, but it has a way to specify colors like this:

// See http://sywac.io
const cli = require('sywac')

cli
  .string('-c, --cloudEnv <env>', {
    description: `The cloud env to use.`,
    required: true,
  })
  .version('-v, --version')
  .help('-h, --help')
  .showHelpByDefault()
  // Add colors to the help output!
  .style({
    usagePrefix: str => chalk().yellow(str.slice(0, 6)) + ' ' + chalk().bold(str.slice(7)),
    usageCommandPlaceholder: str => chalk().bold(str),
    usagePositionals: str => chalk().bold(str),
    usageArgsPlaceholder: str => chalk().bold(str),
    usageOptionsPlaceholder: str => chalk().bold(str),
    group: str => chalk().yellow(str),
    flags: str => chalk().bold(str),
    desc: str => chalk().cyan(str),
    hints: str => chalk().gray.dim(str),
    groupError: str => chalk().red.bold(str),
    flagsError: str => chalk().red.bold(str),
    descError: str => chalk().red.bold(str),
    hintsError: str => chalk().red(str),
    messages: str => chalk().red.bold(str), // these are error messages
  })
  .parseAndExit()
  .then(main)
  .catch(e => {
    console.error(e)
    process.exit(1)
  })

let c
function chalk() {
  // lazily load chalk, only used if help text displayed
  if (!c) c = require('chalk')
  return c
}

async function main(opts) {
  const {cloudEnv} = opts
  console.log(cloudEnv)
}

Example output with color:

color

It'd be neat to have some sort of color configurability for Commander.

All 10 comments

As example:
cli_color

+1

:+1:

+1

Still open after 3+ years? Anyhow commander can be extended using class myconsole extends Command and override, but also can be direct prototype which I did here: https://gist.github.com/kevyworks/653b40af20dc9a2509f865cd7fca1ef0 -- This issue should be closed already.

This has gathered a few likes over the years, so not closing quite yet. I have some problems with choosing colors:

  • color choice is quite personal
  • color support in some Windows shells is poor
  • light and dark themed shells makes color choice harder

I suspect it is too much trouble to build in directly, but perhaps commander can make it easier for people to customise the help output.

This issue has not had any activity in over six months (other than a comment suggesting it be closed). It isn't likely to get acted on due to this report.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

Thank you for your contributions.

Sywac isn't well documented, but it has a way to specify colors like this:

// See http://sywac.io
const cli = require('sywac')

cli
  .string('-c, --cloudEnv <env>', {
    description: `The cloud env to use.`,
    required: true,
  })
  .version('-v, --version')
  .help('-h, --help')
  .showHelpByDefault()
  // Add colors to the help output!
  .style({
    usagePrefix: str => chalk().yellow(str.slice(0, 6)) + ' ' + chalk().bold(str.slice(7)),
    usageCommandPlaceholder: str => chalk().bold(str),
    usagePositionals: str => chalk().bold(str),
    usageArgsPlaceholder: str => chalk().bold(str),
    usageOptionsPlaceholder: str => chalk().bold(str),
    group: str => chalk().yellow(str),
    flags: str => chalk().bold(str),
    desc: str => chalk().cyan(str),
    hints: str => chalk().gray.dim(str),
    groupError: str => chalk().red.bold(str),
    flagsError: str => chalk().red.bold(str),
    descError: str => chalk().red.bold(str),
    hintsError: str => chalk().red(str),
    messages: str => chalk().red.bold(str), // these are error messages
  })
  .parseAndExit()
  .then(main)
  .catch(e => {
    console.error(e)
    process.exit(1)
  })

let c
function chalk() {
  // lazily load chalk, only used if help text displayed
  if (!c) c = require('chalk')
  return c
}

async function main(opts) {
  const {cloudEnv} = opts
  console.log(cloudEnv)
}

Example output with color:

color

It'd be neat to have some sort of color configurability for Commander.

It would still be nice to have a more simple api to change colors & styles

#1365 is a major refactor of how the help is generated including user customisation, but not specifically about adding color support.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DeoLeung picture DeoLeung  路  4Comments

cmoulliard picture cmoulliard  路  4Comments

hossamelmansy picture hossamelmansy  路  4Comments

youurayy picture youurayy  路  5Comments

mathiasbynens picture mathiasbynens  路  3Comments