#!/usr/bin/env bun /** * @fileoverview Command-line argument parsing for loq * @module cli/args */ /** * Parsed command-line arguments structure */ export interface ParsedArgs { /** Log files to process */ files: string[]; /** Query tokens (e.g., ['where', 'level=error']) */ query: string[]; /** CLI options */ options: { /** Enable tail mode (-f) */ follow: boolean; /** Force specific log format */ format: 'auto' | 'json' | 'apache' | 'syslog' | 'clf'; /** Output format */ output: 'color' | 'table' | 'json' | 'csv'; /** Limit number of results */ limit: number | null; /** Show help */ help: boolean; /** Show version */ version: boolean; }; } /** * Parse command-line arguments into structured format * @param args - Raw argument array (typically Bun.argv.slice(2)) * @returns Parsed arguments object * @example * ```ts * const args = parseArgs(['app.log', 'where', 'level=error']); * // { files: ['app.log'], query: ['where', 'level=error'], options: {...} } * ``` */ export function parseArgs(args: string[]): ParsedArgs { const result: ParsedArgs = { files: [], query: [], options: { follow: false, format: 'auto', output: 'color', limit: null, help: false, version: false, }, }; for (let i = 0; i < args.length; i++) { const arg = args[i]!; // Check for SQL-style query (starts with SELECT) if (arg.toUpperCase().startsWith('SELECT ')) { result.query = ['sql', arg]; continue; } // Options if (arg === '-f' || arg === '--follow') { result.options.follow = true; } else if (arg === '-h' || arg === '--help') { result.options.help = true; } else if (arg === '-v' || arg === '--version') { result.options.version = true; } else if (arg === '--format' && args[i + 1]) { result.options.format = args[++i] as ParsedArgs['options']['format']; } else if ((arg === '-o' || arg === '--output') && args[i + 1]) { result.options.output = args[++i] as ParsedArgs['options']['output']; } else if ((arg === '--limit' || arg === '-n') && args[i + 1]) { result.options.limit = parseInt(args[++i]!, 10); } else if (arg === 'where' || arg === 'count' || arg === 'between' || arg === 'after' || arg === 'before') { // Query keywords - collect rest as query result.query = args.slice(i); break; } else if (arg === 'limit' && result.query.length > 0) { result.query.push(arg); } else if (!arg.startsWith('-')) { // Assume it's a file result.files.push(arg); } } return result; } /** * Print help message to console */ export function printHelp(): void { console.log(` loq - Fast, friendly CLI log analyzer USAGE: loq [query] loq where loq "SELECT * FROM WHERE " cat logs.txt | loq where QUERY SYNTAX: where level=error Simple equality where status>=400 Comparison operators where level=error and status>=500 Boolean logic where message contains "timeout" Text search where message matches "error.*" Regex matching where path contains "/api" Field contains between "10:00" and "11:00" today Time range after yesterday Relative time count by level Aggregation limit 10 Limit results SQL SYNTAX: "SELECT * FROM app.log WHERE level='error'" "SELECT level, COUNT(*) FROM app.log GROUP BY level" OPTIONS: -f, --follow Tail mode (like tail -f) -o, --output Output format: color, table, json, csv --format Log format: auto, json, apache, syslog, clf -n, --limit Limit number of results -h, --help Show this help -v, --version Show version EXAMPLES: loq app.log where level=error loq access.log where "status>=400" # Quote comparisons with > or < loq app.log where level=error and path contains "/api" loq app.log count by level loq -f app.log where level=error cat *.log | loq where message contains "failed" NOTE: Quote expressions with > or < to prevent shell interpretation `); } /** * Print version information to console */ export function printVersion(): void { console.log('loq v0.1.0'); }