Support argument normalizer/validator
Describe the feature
I am using citty in my project, And I think it's maybe a good idea to support it. 👀
This is my source code:
export default defineCommand({
meta: {
name: 'create',
description: 'Generate new project from template'
},
args: {
projectPath: {
type: 'string',
description: 'Project path to create',
valueHint: "PWD",
default: process.cwd()
},
},
setup({ args }) {
args.projectPath = path.resolve(args.projectPath)
},
run() {}
}
And support formatter attribute in args definition, then we can do like this:
export default defineCommand({
meta: {
name: 'create',
description: 'Generate new project from template'
},
args: {
projectPath: {
type: 'string',
description: 'Project path to create',
valueHint: "PWD",
default: process.cwd(),
+ formatter: (inputPath) => path.resolve(inputPath)
},
},
- setup({ args }) {
- args.projectPath = path.resolve(inputPath)
- },
run() {}
}
Additional information
- [X] Would you be willing to help implement this feature?
Are you thinking of a validator/normalizer that normalizes value for ctx.args? that's a good idea.
(formatter could also be nice idea btw but i guess for CLI output in usage)
Yes,normalizer is a good idea. But I also think we need validator to validate ctx.args.
In normalizer,ctx.args will be normalized.
In validator, the NodeJs process will be killed if the ctx.args are not as expected
They are different feature.
What's your opinion? :)
Just ran into this need myself. Seems like a zod-like thing's needed here.
I need to both validate and normalize my-cli --folder=./does-not-exist
- Validation to ensure the folder exists (though it's a valid
stringtype) - Normalize the path to
process.cwd()
For my usecase, I accept a logLevel arg and I'm looking to normalize/transform it to either an enum or a number alongside validation. Would this kind type transformation be a responsibilty of the normalize function or does this qualify another feature request?