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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter builder #258

Open
jasonkuhrt opened this issue Nov 20, 2023 · 0 comments
Open

Parameter builder #258

jasonkuhrt opened this issue Nov 20, 2023 · 0 comments
Labels
type/feat New feature or request

Comments

@jasonkuhrt
Copy link
Owner

  • Let's have a new export Parameter that allows building a parameter that can be passed to .parameter or .parameters.
  • parameters is a new method that accepts an array of parameters.
  • Some type concepts will move to it: description, optionality
  • How will the extension system work?
    • Allow extensions to extend the parameter builder
    • But we don't need it, in the sense that the mapping of zod to parameter can be a pure function
    • We can revisit extensions in the future
    • The reason it doesn't work fully is because of the concepts zod brings into the type like optionality and description... but anyways it doesn't seem like the zod part matters much, we can start a bit fresh... we can
  • User can alias Parameter to _ for clean code (see example below)
  • a .type method accepts a Type builder
  • c, p, t can be named exports that alias Command, Parameter, Type
  • methods for each t method like .string() and .boolean() that then adjust the typing of .default() if given after
  • validation methods after the type has been given
  • .name is a constructor method

Examples:

const args = await Command
  .name('intro')
  .parameters([
    _
      .name('filePath')
      .type(zod(z.string()))
      .description(`Path to the file to convert.`),
    _
      .name('to')
      .type(zod(z.enum([`json`, `yaml`, `toml`])))
      .description(`Format to convert to.`),
    _
      .name(`from`)
      .enum([`json`, `yaml`, `toml`])
      .optional(),
    _
      .name(`verbose v`)
      .boolean()
      .default(false)
      .description(`Log detailed progress as conversion executes.`),
    _
      .name(`move m`)
      .type(t.boolean())
      .default(false)
      .description(`Delete the original file after it has been converted.`),
  ])
  .settings({
    prompt: {
      // TODO allow making parameter level opt-in or opt-out
      // default: false,
      when: [
        {
          result: `rejected`,
          error: `ErrorMissingArgument`,
        },
        { result: `omitted` },
      ],
    },
  })
  .parse()
const args = await Command
  .name('intro')
  .parameter('filePath', {
    type: zod(z.string()),
    description: `Path to the file to convert.`,
  })
  .parameter(`to`, {
    type: zod(z.enum([`json`, `yaml`, `toml`])),
    description: `Format to convert to.`,
  })
  .parameter(`from`, {
    type: zod(z.enum([`json`, `yaml`, `toml`])),
    optionality: Optionality.optional(),
  })
  .parameter(`verbose v`, {
    type: zod(z.boolean()),
    optionality: Optionality.default(false),
    description: `Log detailed progress as conversion executes.`,
  })
  .parameter(`move m`, {
    type: zod(z.boolean().default(false).describe(`Delete the original file after it has been converted.`)),
  })
  .settings({
    prompt: {
      // TODO allow making parameter level opt-in or opt-out
      // default: false,
      when: [
        {
          result: `rejected`,
          error: `ErrorMissingArgument`,
        },
        { result: `omitted` },
      ],
    },
  })
  .parse()

CleanShot 2023-11-19 at 22 30 33@2x

CleanShot 2023-11-19 at 22 30 42@2x

@jasonkuhrt jasonkuhrt added the type/feat New feature or request label Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/feat New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant