args.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Commander adapter for the dsh-sdk subcommand surface.
  3. *
  4. * @module @deepseek-ai/dsh-scripts/args
  5. */
  6. import { parseArgs as parseNodeArgs } from 'node:util'
  7. import { Command } from 'commander'
  8. /** Commands implemented by the dsh-sdk launcher. */
  9. type DshSdkCommand = 'start' | 'dev' | 'build' | 'config'
  10. /** Parsed dsh-sdk invocation. */
  11. export interface DshSdkArgs {
  12. command?: DshSdkCommand
  13. target?: string
  14. forwarded: readonly string[]
  15. help: boolean
  16. }
  17. /** Parse arbitrary project flags through Node's zero-schema argument parser. */
  18. export function parseSdkBootArgs(argv: readonly string[]): Record<string, string | boolean | undefined> {
  19. return parseNodeArgs({
  20. args: [...argv],
  21. strict: false,
  22. allowPositionals: true,
  23. allowNegative: true,
  24. }).values
  25. }
  26. /** Parse one launcher invocation through real Commander subcommands. */
  27. export function parseDshSdkArgs(argv: readonly string[]): DshSdkArgs {
  28. if (argv.length === 0 || argv[0] === '--help' || argv[0] === '-h') {
  29. return { forwarded: [], help: true }
  30. }
  31. const separator = argv.indexOf('--')
  32. const launcherArgv = separator === -1 ? argv : argv.slice(0, separator)
  33. const passthrough = separator === -1 ? [] : argv.slice(separator + 1)
  34. let parsed: DshSdkArgs | undefined
  35. const program = new Command()
  36. .name('dsh-sdk')
  37. .helpOption(false)
  38. .showHelpAfterError(false)
  39. .exitOverride()
  40. .configureOutput({
  41. /* v8 ignore next -- the command wrapper renders the package-owned usage template */
  42. writeOut: () => {},
  43. /* v8 ignore next -- Commander errors are returned to the command wrapper */
  44. writeErr: () => {},
  45. })
  46. program.command('start [target]').helpOption(false).action((target?: string) => {
  47. parsed = { command: 'start', ...target ? { target } : {}, forwarded: [], help: false }
  48. })
  49. program.command('dev [target]').helpOption(false).action((target?: string) => {
  50. parsed = { command: 'dev', ...target ? { target } : {}, forwarded: [], help: false }
  51. })
  52. program.command('build [args...]').helpOption(false).allowUnknownOption(true).action((args: string[] = []) => {
  53. parsed = { command: 'build', forwarded: args, help: false }
  54. })
  55. program.command('config').helpOption(false).action(() => {
  56. parsed = { command: 'config', forwarded: [], help: false }
  57. })
  58. program.parse([...launcherArgv], { from: 'user' })
  59. /* v8 ignore next -- every registered Commander action above assigns parsed or Commander throws */
  60. if (!parsed) throw new Error('dsh-sdk command did not resolve')
  61. if (parsed.command === 'config' && passthrough.length > 0) {
  62. throw new Error('dsh-sdk config does not accept forwarded arguments')
  63. }
  64. return { ...parsed, forwarded: [...parsed.forwarded, ...passthrough] }
  65. }