source dump of claude code
at main 60 lines 1.4 kB view raw
1/** 2 * Centralized utilities for parsing slash commands 3 */ 4 5export type ParsedSlashCommand = { 6 commandName: string 7 args: string 8 isMcp: boolean 9} 10 11/** 12 * Parses a slash command input string into its component parts 13 * 14 * @param input - The raw input string (should start with '/') 15 * @returns Parsed command name, args, and MCP flag, or null if invalid 16 * 17 * @example 18 * parseSlashCommand('/search foo bar') 19 * // => { commandName: 'search', args: 'foo bar', isMcp: false } 20 * 21 * @example 22 * parseSlashCommand('/mcp:tool (MCP) arg1 arg2') 23 * // => { commandName: 'mcp:tool (MCP)', args: 'arg1 arg2', isMcp: true } 24 */ 25export function parseSlashCommand(input: string): ParsedSlashCommand | null { 26 const trimmedInput = input.trim() 27 28 // Check if input starts with '/' 29 if (!trimmedInput.startsWith('/')) { 30 return null 31 } 32 33 // Remove the leading '/' and split by spaces 34 const withoutSlash = trimmedInput.slice(1) 35 const words = withoutSlash.split(' ') 36 37 if (!words[0]) { 38 return null 39 } 40 41 let commandName = words[0] 42 let isMcp = false 43 let argsStartIndex = 1 44 45 // Check for MCP commands (second word is '(MCP)') 46 if (words.length > 1 && words[1] === '(MCP)') { 47 commandName = commandName + ' (MCP)' 48 isMcp = true 49 argsStartIndex = 2 50 } 51 52 // Extract arguments (everything after command name) 53 const args = words.slice(argsStartIndex).join(' ') 54 55 return { 56 commandName, 57 args, 58 isMcp, 59 } 60}