sindresorhus / meow

🐈 CLI app helper
MIT License
3.53k stars 150 forks source link

Add `choices` option #171

Closed hosseinmd closed 1 year ago

hosseinmd commented 3 years ago

I want to define a type like this:

      aFlag: {
        type: "global" | "local",
        default: "global",
      },

How this possible? or if that is not possible. could you implement it?

sindresorhus commented 3 years ago

What are you trying to do? First, those type values are not valid, even without the "or". Second, how would it be able to decide which type to use?

hosseinmd commented 3 years ago

Imagine I have an argument which gets two values "local" or "global".

sindresorhus commented 3 years ago

You didn't answer all my questions.

hosseinmd commented 3 years ago

how would it be able to decide which type to use?

I don't familiar with meow project but on Typescript, we could do it like this:

function example<T>(type: T): T extends ReadonlyArray<infer K> ? K : T {
  return "" as any;
}

example(["foo", "bar"] as const) === "foo";
// example return 'foo' | 'bar
sindresorhus commented 3 years ago

Ok, I get it now. The type should still be a string though. What you want is something like .choices() in Yargs: http://yargs.js.org/docs/#choices Basically, specify a list of the possible values to the flag.

That's not supported today, but I'll mark this as a feature request.

sindresorhus commented 1 year ago

If anyone wants to work on this, see the initial attempt in https://github.com/sindresorhus/meow/pull/183 (Also make sure you intend to finish it!)