Fix `rome --help` and add test case
MODIFY
packages/@romejs/cli-flags/Parser.test.ts
packages/@romejs/cli-flags/Parser.test.ts
@@ -17,12 +17,14 @@ async function testParser<T>(
{
defineFlags,
args,
- callback,
+ preInit,
+ postInit,
options: opts,
}: {
defineFlags: (consumer: Consumer) => T;
args: Array<string>;
- callback?: (parser: Parser<T>, flags: T) => void;
+ preInit?: (parser: Parser<T>) => void;
+ postInit?: (parser: Parser<T>, flags: T) => void;
options?: Partial<ParserOptions<T>>;
},
) {
@@ -40,10 +42,13 @@ async function testParser<T>(
);
const {diagnostics} = await catchDiagnostics(async () => {
+ if (preInit !== undefined) {
+ preInit(parser);
+ }
const flags = await parser.init();
t.namedSnapshot("flags", flags);
- if (callback !== undefined) {
- callback(parser, flags);
+ if (postInit !== undefined) {
+ postInit(parser, flags);
}
});
@@ -300,6 +305,35 @@ test(
},
);
+test(
+ "command required with no command but with --help flag",
+ async (t) => {
+ await testParser(
+ t,
+ {
+ options: {
+ commandRequired: true,
+ noProcessExit: true,
+ },
+ defineFlags: () => {
+ return {};
+ },
+ args: ["--help"],
+ preInit(p) {
+ p.addCommand({
+ name: "foo",
+ callback() {},
+ });
+ p.addCommand({
+ name: "bar",
+ callback() {},
+ });
+ },
+ },
+ );
+ },
+);
+
test(
"command required with wrong command",
async (t) => {
@@ -313,7 +347,7 @@ test(
return {};
},
args: ["foo"],
- callback(p) {
+ preInit(p) {
p.addCommand({
name: "foobar",
callback() {},

MODIFY
packages/@romejs/cli-flags/Parser.ts
packages/@romejs/cli-flags/Parser.ts
@@ -68,6 +68,7 @@ export type ParserOptions<T> = {
description?: string;
version?: string;
ignoreFlags?: Array<string>;
+ noProcessExit?: boolean;
commandRequired?: boolean;
commandSuggestions?: Dict<{
commandName: string;
@@ -383,7 +384,7 @@ export default class Parser<T> {
).asBoolean(false);
if (shouldDisplayVersion) {
this.reporter.logAll(version);
- process.exit(0);
+ this.exit(0);
}
}
@@ -398,7 +399,7 @@ export default class Parser<T> {
).asStringSetOrVoid(["fish", "bash"]);
if (generateAutocomplete !== undefined) {
await this.generateAutocomplete(generateAutocomplete);
- process.exit(0);
+ this.exit(0);
}
// Show help for --help
@@ -432,7 +433,7 @@ export default class Parser<T> {
this.currentCommand = undefined;
- if (this.opts.commandRequired) {
+ if (this.opts.commandRequired && !shouldShowHelp) {
this.commandRequired(definedCommand !== undefined, flagsConsumer);
}
@@ -444,7 +445,7 @@ export default class Parser<T> {
await this.showHelp(
definedCommand === undefined ? undefined : definedCommand.command,
);
- process.exit(1);
+ this.exit(1);
}
if (definedCommand !== undefined) {
@@ -962,6 +963,12 @@ export default class Parser<T> {
return flags;
}
+
+ exit(code: number) {
+ if (!this.opts.noProcessExit) {
+ process.exit(code);
+ }
+ }
}
export class ParserInterface<T> {

Fix `rome --help` and add test case (#695)