Skip to content

Commit

Permalink
feat: add --help flag
Browse files Browse the repository at this point in the history
This adds a `--help` flag which takes precedence over all other arguments or positionals.

Closes #249
  • Loading branch information
boneskull committed Dec 7, 2023
1 parent 8524909 commit 427aaba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
16 changes: 14 additions & 2 deletions markdownlint-cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,19 +912,31 @@ const main = async (params) => {
let fixDefault = false;
// eslint-disable-next-line unicorn/no-useless-undefined
let configPath = undefined;

let shouldShowHelp = false;
const argvFiltered = (argv || []).filter((arg) => {
if (configPath === null) {
configPath = arg;
return false;
} else if (arg === "--config") {
}
if (arg === "--config") {
configPath = null;
return false;
} else if (arg === "--fix") {
}
if (arg === "--fix") {
fixDefault = true;
return false;
}
if (arg === '--help') {
shouldShowHelp = true;
return false;
}
return true;
});
if (shouldShowHelp) {
showHelp(logMessage);
return 2;
}
// Read argv configuration file (if relevant and present)
let optionsArgv = null;
let relativeDir = null;
Expand Down
28 changes: 28 additions & 0 deletions test/markdownlint-cli2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,31 @@ test("custom fs, using fsMock simulating symbolic links", (t) => {
t.is(exitCode, 1);
});
});

test("--help", (t) => {
t.plan(2);
const stdouts = [];
return markdownlintCli2({
"argv": [ "--help" ],
"logMessage": (msg) => stdouts.push(msg),
"logError": (msg) => t.fail(`message logged: ${msg}`)
})
.then((exitCode) => {
t.is(exitCode, 2);
t.regex(stdouts.join('\n'), /Syntax: markdownlint-cli2/u);
});
});

test("--help, using globs", (t) => {
t.plan(2);
const stdouts = [];
return markdownlintCli2({
"argv": [ "README.md", "--help" ],
"logMessage": (msg) => stdouts.push(msg),
"logError": (msg) => t.fail(`message logged: ${msg}`)
})
.then((exitCode) => {
t.is(exitCode, 2);
t.regex(stdouts.join('\n'), /Syntax: markdownlint-cli2/u);
});
})

Check notice

Code scanning / CodeQL

Semicolon insertion Note test

Avoid automated semicolon insertion (97% of all statements in
the enclosing script
have an explicit semicolon).

0 comments on commit 427aaba

Please sign in to comment.