arcanis / clipanion

Type-safe CLI library / framework with no runtime dependencies
https://mael.dev/clipanion/
1.1k stars 61 forks source link

Feature request: Allow validator to accept async functions #142

Open Ayc0 opened 1 year ago

Ayc0 commented 1 year ago

Hello 👋

I'm using clipanion in a repositories with a lot of CLIs. To avoid 1 CLI to slow down all others, we follow the guidelines here https://mael.dev/clipanion/docs/tips#lazy-evaluation: we moved all our top level imports within the Command classes (except for node builtins and for clipanion).

But when we have to use enums that come from other files for the validator, we have to use a top level import, something like:

import { Command, Option } from 'clipanion';
import * as t from 'typanion';

import { PossibleValues } from './other-file';

class MyCommand extends Command {
    static paths = [['my', 'command']];
    entries: string[] = Option.Array('--entries', [], {
        validator: t.isArray(t.isEnum(PossibleValues)),
    });
    // ...
}

But now, we don't have any control anymore on other-file, and this file could block the main thread without realizing it on load.

Could we introduce the ability for clipanion to support async functions in validator? This would allow to re-write this code this way:

import { Command, Option } from 'clipanion';
import * as t from 'typanion';

class MyCommand extends Command {
    static paths = [['my', 'command']];
    entries: string[] = Option.Array('--entries', [], {
        validator: async (value) => {
            const { PossibleValues } = await import('./other-file');
            return t.isArray(t.isEnum(PossibleValues))
        },
    });
    // ...
}

Which is also compliant with the recommendation in the doc for lazy evaluations.

If this is accepted, I can try writing a PR for it