Skip to content
Open
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
16 changes: 14 additions & 2 deletions dor/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ interface CaptureProcess extends StricliProcess {

export async function runCli(argv: string[], options: CliOptions = {}): Promise<CliResult> {
const helpTarget = getHelpTarget(argv);
const [commandName, ...args] = argv[0] === 'help' ? ['--help'] : argv;
const [commandName, ...args] = rewriteHelpArgv(argv);

if (commandName === 'ensure' && !args.includes('-h') && !args.includes('--help')) {
const delimiterCheck = validateEnsureDelimiter(args);
Expand Down Expand Up @@ -126,7 +126,13 @@ type HelpTarget =
| { scope: 'command'; commandName: string };

function getHelpTarget(argv: string[]): HelpTarget | undefined {
if (argv.length === 0 || argv[0] === 'help' || (argv.length === 1 && (argv[0] === '--help' || argv[0] === '-h'))) {
if (argv[0] === 'help') {
const subject = argv[1];
return subject && isCommandName(subject)
? { scope: 'command', commandName: subject }
: { scope: 'root' };
}
if (argv.length === 0 || (argv.length === 1 && (argv[0] === '--help' || argv[0] === '-h'))) {
return { scope: 'root' };
}

Expand All @@ -138,6 +144,12 @@ function getHelpTarget(argv: string[]): HelpTarget | undefined {
return undefined;
}

function rewriteHelpArgv(argv: string[]): string[] {
if (argv[0] !== 'help') return argv;
const subject = argv[1];
return subject && isCommandName(subject) ? [subject, '--help'] : ['--help'];
}

function isCommandName(value: string): value is keyof typeof ROUTES {
return value in ROUTES;
}
Expand Down
13 changes: 12 additions & 1 deletion dor/test/cli-help.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ test('help snapshots cover every command in global help', async (t) => {

assert.deepEqual(await runCli([]), globalHelp, 'no-arg dor should print root help');
assert.deepEqual(await runCli(['help']), globalHelp, 'dor help should print root help');
assert.deepEqual(
await runCli(['help', 'not-a-command']),
globalHelp,
'dor help <unknown> should print root help',
);

await t.test('dor', async () => {
await snapshotHelp({
Expand All @@ -26,14 +31,20 @@ test('help snapshots cover every command in global help', async (t) => {
});

for (const command of commandNames) {
const commandHelp = await runCli([command, '--help']);
await t.test(`dor ${command}`, async () => {
await snapshotHelp({
file: command,
title: `dor ${command}`,
invocation: `dor ${command} --help`,
result: await runCli([command, '--help']),
result: commandHelp,
});
});
assert.deepEqual(
await runCli(['help', command]),
commandHelp,
`dor help ${command} should match dor ${command} --help`,
);
}

await assertSnapshotInventory(['dor', ...commandNames]);
Expand Down
Loading