-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
Feature: Support extracting options/args types from Command. #589
Comments
Hi @NfNitLoop, sry for late replay, i'm on a long trip currently. |
Is there currently any way to extract the action handler types? I am currently running into a similar issue when using a For example, in main entry:
And
|
@xe54ck You can use the infer type. For example, you can do something like this: entry.ts export type GlobalOptions = typeof args extends
Command<void, void, void, [], infer Options extends Record<string, unknown>>
? Options
: never;
const args = new Command()
.globalOption(
"-p, --project-path <project-path:file>",
"Path to project folder",
{
default: config.projectPath,
},
);
await args
.command("init", Init)
.parse(); init.ts import type { GlobalOptions } from "./entry.ts";
export const Init = new Command<GlobalOptions>()
.description("init")
.action(async (options) => {
// This works but type error
// `Property 'projectPath' does not exist on type 'void`
const { projectPath } = options;
}); |
Thank you, @DrakeTDL! That worked great, been looking for something like this for a while! 😊 |
Context
For complicated CLIs, there can be lots of
.action(…)
s, and having your command implementation bodies indented inside of theCommand
builder is not always ideal. Even for small projects, I prefer this pattern which decouples my implementation from the arg parsing:But to do this as of now (unless I'm missing something? 😅) I have to define my own
Options
type which conforms to the options thatCommand.action()
will pass me.Feature request
It would be nice if Command supported an "infer" like Zod.
In Zod, you do something like:
Maybe in Cliffy's Command, we could do something like:
The implementation of
z.infer
is not something that I've looked into, so I'm not sure how easy it would be to apply to Command. But as a user it would be nice to have. 😊 Thanks!The text was updated successfully, but these errors were encountered: