-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add color support for help option #301
Comments
This comment was marked as outdated.
This comment was marked as outdated.
1 similar comment
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This has gathered a few likes over the years, so not closing quite yet. I have some problems with choosing colors:
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 comment was marked as outdated.
This comment was marked as outdated.
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: 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 |
This comment was marked as outdated.
This comment was marked as outdated.
IMO, color support is crucial for a CLI tool. // user code:
import { Command } from 'commander';
import c from 'chalk';
const formatter = {
helpWidth: 120,
indent: 2,
titles: str => c.yellow.bold(str),
usage: str => c.gray(str),
option: flags => c.green(flags),
command: (name, args) => c.green(name) + ' ' + c.gray(args),
description: desc => c.white(desc),
default: val => `[default: ${c.gray(val)}}`
};
const program = new Command();
program
.name('colorful-cli')
.usage('<command> [options]')
.version(pkg.version)
.format(formatter) // <—— pass formatter object to commander
.option('--debug', 'output extra logs')
// ... more commands & options... Pls re-open this issue. |
This comment was marked as outdated.
This comment was marked as outdated.
This old issue has been getting recent upvotes, and I have started looking at what is needed to make it easier for authors to provide custom colours in the help. I'll reopen at least while I am working on it. |
This comment was marked as outdated.
This comment was marked as outdated.
Open draft PR in #2251. Added a style layer of simple hooks so don't need to subclass to add colour: program.configureHelp({
styleTitle: (str) => styleText('bold', str),
styleCommandText: (str) => styleText('cyan', str),
styleCommandDescription: (str) => styleText('magenta', str),
styleItemDescription: (str) => styleText('italic', str),
styleOptionText: (str) => styleText('green', str),
styleArgumentText: (str) => styleText('yellow', str),
styleSubcommandText: (str) => styleText('blue', str),
}); |
A prerelease is available for v13. The release is tagged as next and can be installed with:
|
No description provided.
The text was updated successfully, but these errors were encountered: