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

Support detecting args that is specified but with no values #142

Open
1 task done
hyf0 opened this issue May 3, 2024 · 0 comments
Open
1 task done

Support detecting args that is specified but with no values #142

hyf0 opened this issue May 3, 2024 · 0 comments

Comments

@hyf0
Copy link

hyf0 commented May 3, 2024

Describe the feature

Generally I have to using customized args parser to write tje following code. I was hope that citty could support the requirement said in the title, since it already use mir under the hood.

import process from 'node:process'
import parseArgs from 'mri'
import { defineCommand, runMain, showUsage } from 'citty'
import { ensureConfig, logger } from './utils.js'
import { bundle } from './commands/bundle.js'
import {
  version,
  description,
} from '../../package.json' assert { type: 'json' }
import { DEFAULT_CONFIG_FILENAME } from './constants.js'

interface ParsedArgs {
  config?: string | true
  c?: string | true
  // `citty` intercept the help option, so we don't need to deal with it
  // help?: boolean
  // h?: boolean
}

const main = defineCommand({
  meta: {
    name: 'rolldown',
    version,
    description,
  },
  args: {
    config: {
      type: 'string',
      alias: 'c',
      description:
        'Use this config file (if argument is used but value is unspecified, defaults to `rolldown.config.js`)',
    },
    help: {
      type: 'boolean',
      alias: 'h',
      description: 'Show this help message',
    },
  },
  async run(_ctx) {
    // FIXME: `citty` doesn't support detecting if an argument is unspecified
    const parsedArgs = parseArgs<ParsedArgs>(process.argv.slice(2))
    let argConfig = parsedArgs.c || parsedArgs.config
    if (argConfig) {
      // If config is specified, we will ignore other arguments and bundle with the specified config
      if (argConfig == true) {
        argConfig = DEFAULT_CONFIG_FILENAME
      }
      await bundle(argConfig)
      return
    }

    showUsage(main)
  },
})

runMain(main)

Additional information

  • Would you be willing to help implement this feature?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant