/ commands / plugin / parseArgs.ts
parseArgs.ts
  1  // Parse plugin subcommand arguments into structured commands
  2  export type ParsedCommand =
  3    | { type: 'menu' }
  4    | { type: 'help' }
  5    | { type: 'install'; marketplace?: string; plugin?: string }
  6    | { type: 'manage' }
  7    | { type: 'uninstall'; plugin?: string }
  8    | { type: 'enable'; plugin?: string }
  9    | { type: 'disable'; plugin?: string }
 10    | { type: 'validate'; path?: string }
 11    | {
 12        type: 'marketplace'
 13        action?: 'add' | 'remove' | 'update' | 'list'
 14        target?: string
 15      }
 16  
 17  export function parsePluginArgs(args?: string): ParsedCommand {
 18    if (!args) {
 19      return { type: 'menu' }
 20    }
 21  
 22    const parts = args.trim().split(/\s+/)
 23    const command = parts[0]?.toLowerCase()
 24  
 25    switch (command) {
 26      case 'help':
 27      case '--help':
 28      case '-h':
 29        return { type: 'help' }
 30  
 31      case 'install':
 32      case 'i': {
 33        const target = parts[1]
 34        if (!target) {
 35          return { type: 'install' }
 36        }
 37  
 38        // Check if it's in format plugin@marketplace
 39        if (target.includes('@')) {
 40          const [plugin, marketplace] = target.split('@')
 41          return { type: 'install', plugin, marketplace }
 42        }
 43  
 44        // Check if the target looks like a marketplace (URL or path)
 45        const isMarketplace =
 46          target.startsWith('http://') ||
 47          target.startsWith('https://') ||
 48          target.startsWith('file://') ||
 49          target.includes('/') ||
 50          target.includes('\\')
 51  
 52        if (isMarketplace) {
 53          // This is a marketplace URL/path, no plugin specified
 54          return { type: 'install', marketplace: target }
 55        }
 56  
 57        // Otherwise treat it as a plugin name
 58        return { type: 'install', plugin: target }
 59      }
 60  
 61      case 'manage':
 62        return { type: 'manage' }
 63  
 64      case 'uninstall':
 65        return { type: 'uninstall', plugin: parts[1] }
 66  
 67      case 'enable':
 68        return { type: 'enable', plugin: parts[1] }
 69  
 70      case 'disable':
 71        return { type: 'disable', plugin: parts[1] }
 72  
 73      case 'validate': {
 74        const target = parts.slice(1).join(' ').trim()
 75        return { type: 'validate', path: target || undefined }
 76      }
 77  
 78      case 'marketplace':
 79      case 'market': {
 80        const action = parts[1]?.toLowerCase()
 81        const target = parts.slice(2).join(' ')
 82  
 83        switch (action) {
 84          case 'add':
 85            return { type: 'marketplace', action: 'add', target }
 86          case 'remove':
 87          case 'rm':
 88            return { type: 'marketplace', action: 'remove', target }
 89          case 'update':
 90            return { type: 'marketplace', action: 'update', target }
 91          case 'list':
 92            return { type: 'marketplace', action: 'list' }
 93          default:
 94            // No action specified, show marketplace menu
 95            return { type: 'marketplace' }
 96        }
 97      }
 98  
 99      default:
100        // Unknown command, show menu
101        return { type: 'menu' }
102    }
103  }