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

Don't indent single line help / description text #232

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,11 @@ const meow = (helpText, options = {}) => {

const {pkg: package_} = options;
const argv = parseArguments(options.argv, parserOptions);
let help = redent(trimNewlines((options.help ?? '').replace(/\t+\n*$/, '')), 2);
let help = trimNewlines((options.help || '').replace(/\t+\n*$/, ''));

if (help.includes('\n')) {
help = redent(help, 2);
}

normalizePackageData(package_);

Expand All @@ -295,7 +299,12 @@ const meow = (helpText, options = {}) => {
({description} = package_);
}

help = (description ? `\n ${description}\n` : '') + (help ? `\n${help}\n` : '\n');
// Change to `&&=` when targeting Node 15+
if (description) {
description = help ? `\n ${description}\n` : `\n${description}`;
}
Comment on lines +302 to +305
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node 15+ supports logical AND assignment, which makes this simpler:

description &&= help ? `\n  ${description}\n` : `\n${description}`;


help = (description || '') + (help ? `\n${help}\n` : '\n');

const showHelp = code => {
console.log(help);
Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,26 @@ test('options - multiple validation errors', t => {
`});
});

test('single line help messages are not indented', t => {
const cli = meow({
importMeta,
description: false,
help: 'single line',
});

t.is(cli.help, '\nsingle line\n');
});

test('descriptions with no help are not indented', t => {
const cli = meow({
importMeta,
help: false,
description: 'single line',
});

t.is(cli.help, '\nsingle line\n');
});

if (NODE_MAJOR_VERSION >= 14) {
test('supports es modules', async t => {
try {
Expand Down