Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147/** Parses CLI arguments for `xdg-terminal-exec`. Supports `--app-id=VALUE`, `--title=VALUE`, `--dir=VALUE`, `--hold`, and `--` or `-e` to delimit the command to execute. @module */import { tagged, } from '@monochromatic-dev/module-logger/ts';
import type { UserOptions, } from './build-command.ts';
/** Logger root for terminal-exec after removing the package log shim. @example ```ts const rl = tagged({ tag: someFunction.name, l: parentLogger, },); ``` */const parentLogger = tagged({ tag: 'terminal-exec', },);
/** Tagged logger for this module. */const l = tagged({ tag: 'cli', l: parentLogger,},);
/** Parses process arguments into structured user options. Arguments before `--` or `-e` are xdg-terminal-exec options; arguments after are the command to execute in the terminal. @param argv - Raw process arguments (excluding `bun` and script path). @returns Parsed user options. @mutates argv - `JSON.stringify` may invoke array accessors or proxy traps. @example ```ts parseArgs({ argv: ['--title=My Shell', '--', 'bash', '-l'] }) // { title: 'My Shell', command: ['bash', '-l'], appId: '', dir: '', hold: false } ``` */export function parseArgs({ argv, }: { argv: readonly string[]; },): UserOptions { /** Option accumulator mutated by the parse loop below; let because reassigned conditionally. */ let appId = ''; /** Same shape as appId; the parse loop populates each option in turn. */ let title = ''; /** Same shape as appId; the parse loop populates each option in turn. */ let dir = ''; /** Same shape as appId; the parse loop populates each option in turn. */ let hold = false; /** Cursor into argv; advanced as options are consumed and used to slice the command remainder. */ let i = 0;
while (i < argv .length) { /** Current argv slot, scoped to the loop iteration. */ const arg = argv[i]; if (arg === undefined) break;
if ((arg === '--') || (arg === '-e')) { l.debug(`found delimiter '${arg}'`,); i++; break; }
if (arg.startsWith('--app-id=',)) { appId = arg.slice('--app-id='.length,); l.debug(`app-id='${appId}'`,); i++; continue; }
if (arg.startsWith('--title=',)) { title = arg.slice('--title='.length,); l.debug(`title='${title}'`,); i++; continue; }
if (arg.startsWith('--dir=',)) { dir = arg.slice('--dir='.length,); l.debug(`dir='${dir}'`,); i++; continue; }
if (arg === '--hold') { hold = true; l.debug('hold=true',); i++; continue; }
if (arg.startsWith('-',)) { l.debug(`ignoring unknown option '${arg}'`,); i++; continue; }
/** First non-option argument starts the command. */ l.debug(`found non-option '${arg}', treating as command start`,); break; }
/** Remainder after the delimiter is the command to execute. */ const command = argv.slice(i,);
l.debug( `parsed: appId='${appId}', title='${title}', dir='${dir}', hold=${ String(hold,) }, command=${JSON.stringify(command,)}`, ); /** Bound to a local before return so the helper-shape allowlist accepts the function-root `let` declarations above. */ const result: UserOptions = { appId, title, dir, hold, command, }; return result;}