Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support argument normalizer/validator #103

Open
1 task done
peterroe opened this issue Nov 7, 2023 · 4 comments
Open
1 task done

Support argument normalizer/validator #103

peterroe opened this issue Nov 7, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@peterroe
Copy link
Contributor

peterroe commented Nov 7, 2023

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

  • Would you be willing to help implement this feature?
@pi0
Copy link
Member

pi0 commented Nov 7, 2023

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)

@pi0 pi0 changed the title Support formatter in args definition Support argument normalizer Nov 7, 2023
@pi0 pi0 changed the title Support argument normalizer Support argument normalizer/validator Nov 7, 2023
@pi0 pi0 added the enhancement New feature or request label Nov 7, 2023
@peterroe
Copy link
Contributor Author

peterroe commented Nov 7, 2023

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? :)

@ericclemmons
Copy link

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

  1. Validation to ensure the folder exists (though it's a valid string type)
  2. Normalize the path to process.cwd()

@Eazash
Copy link

Eazash commented May 8, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants