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

feat: support indent level as option #241

Merged
merged 5 commits into from
Feb 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,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
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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 @@ -31,7 +31,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
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const buildOptions = (helpText, options) => {
hardRejection: true,
allowUnknownFlags: true,
allowParentFlags: true,
helpIndent: 2,
...options,
};

Expand Down
21 changes: 21 additions & 0 deletions test/help.js
Original file line number Diff line number Diff line change
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));
});