Skip to content

Commit

Permalink
Add helpIndent option (#241)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
dylanarmstrong and sindresorhus committed Feb 6, 2024
1 parent fd0bc62 commit e9a55cd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -292,6 +292,13 @@ Default: `true`

Whether to allow unknown flags or not.

##### helpIndent

Type `number`\
Default: `2`

The number of spaces to use for indenting the help text.

## Promises

Meow will make unhandled rejected promises [fail hard](https://github.com/sindresorhus/hard-rejection) instead of the default silent fail. Meaning you don't have to manually `.catch()` promises used in your CLI.
Expand Down
7 changes: 7 additions & 0 deletions source/index.d.ts
Expand Up @@ -309,6 +309,13 @@ export type Options<Flags extends AnyFlags> = {
@default true
*/
readonly allowUnknownFlags?: boolean;

/**
The number of spaces to use for indenting the help text.
@default 2
*/
readonly helpIndent?: number;
};

type TypedFlag<Flag extends AnyFlag> =
Expand Down
4 changes: 2 additions & 2 deletions source/index.js
Expand Up @@ -17,7 +17,7 @@ const buildResult = (options, parserOptions) => {
help = trimNewlines((options.help || '').replace(/\t+\n*$/, ''));

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

help = `\n${help}`;
Expand All @@ -30,7 +30,7 @@ const buildResult = (options, parserOptions) => {
({description} = package_);
}

description &&= help ? `\n ${description}\n` : `\n${description}`;
description &&= help ? redent(`\n${description}\n`, options.helpIndent) : `\n${description}`;
help = `${description || ''}${help}\n`;

const showHelp = code => {
Expand Down
1 change: 1 addition & 0 deletions source/options.js
Expand Up @@ -80,6 +80,7 @@ export const buildOptions = (helpText, options) => {
booleanDefault: false,
allowUnknownFlags: true,
allowParentFlags: true,
helpIndent: 2,
...options,
};

Expand Down
21 changes: 21 additions & 0 deletions test/help.js
Expand Up @@ -44,3 +44,24 @@ test('descriptions with no help are not indented', t => {
description: 'single line',
}).help, '\nsingle line\n');
});

test('support help shortcut with no indentation', t => {
t.is(meow(`
unicorn
cat
`, {
helpIndent: 0,
importMeta,
}).help, indentString('\nCLI app helper\n\nunicorn\ncat\n', 0));
});

test('no description and no indentation', t => {
t.is(meow(`
unicorn
cat
`, {
helpIndent: 0,
description: false,
importMeta,
}).help, indentString('\nunicorn\ncat\n', 0));
});

0 comments on commit e9a55cd

Please sign in to comment.