jasonkuhrt / molt

⚡️ CLI building blocks & framework for the TypeScript era.
MIT License
77 stars 1 forks source link

Parameter builder #258

Open jasonkuhrt opened 10 months ago

jasonkuhrt commented 10 months ago

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