UNPKG

agentsqripts

Version:

Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems

64 lines (56 loc) 1.71 kB
/** * @file Enhanced command line argument parser * @description Provides better argument parsing that handles paths correctly */ /** * Parse command line arguments with improved handling * @param {Array} args - Raw command line arguments * @param {Object} config - Configuration for parsing * @returns {Object} Parsed result with options, targetPath, and potential error */ function parseArgs(args = [], config = {}) { const { defaults = {}, flags = {} } = config; // Initialize with defaults const options = { ...defaults }; let targetPath = '.'; let error = null; // Process arguments let i = 0; while (i < args.length) { const arg = args[i]; // Check if it's a flag if (arg.startsWith('--')) { const flagName = arg.substring(2); const flagConfig = Object.entries(flags).find(([key]) => key === flagName || key.replace(/([A-Z])/g, '-$1').toLowerCase() === flagName ); if (flagConfig) { const [optionKey, optionConfig] = flagConfig; // Boolean flag if (optionConfig.type === 'boolean') { options[optionKey] = true; i++; } // Value flag else if (i + 1 < args.length && !args[i + 1].startsWith('--')) { options[optionKey] = args[i + 1]; i += 2; } else { error = `Flag ${arg} requires a value`; break; } } else { // Unknown flag error = `Unknown flag: ${arg}`; break; } } // Not a flag, must be the target path else { targetPath = arg; i++; } } return { options, targetPath, error }; } module.exports = { parseArgs };