citty icon indicating copy to clipboard operation
citty copied to clipboard

Support argument normalizer/validator

Open peterroe opened this issue 1 year ago • 4 comments

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?

peterroe avatar Nov 07 '23 07:11 peterroe