(argv: string[])
| 15 | } |
| 16 | |
| 17 | export function parse(argv: string[]): ParsedArgs { |
| 18 | const positionals: string[] = [] |
| 19 | const flags = new Map<string, string[]>() |
| 20 | |
| 21 | for (let i = 0; i < argv.length; i++) { |
| 22 | const token = argv[i] |
| 23 | if (token === '--') { |
| 24 | // Everything after `--` is positional. Useful for note bodies that |
| 25 | // happen to start with `--`. |
| 26 | for (let j = i + 1; j < argv.length; j++) positionals.push(argv[j]) |
| 27 | break |
| 28 | } |
| 29 | if (token.startsWith('--')) { |
| 30 | const eq = token.indexOf('=') |
| 31 | if (eq >= 0) { |
| 32 | const name = token.slice(2, eq) |
| 33 | const value = token.slice(eq + 1) |
| 34 | push(flags, name, value) |
| 35 | continue |
| 36 | } |
| 37 | const name = token.slice(2) |
| 38 | const next = argv[i + 1] |
| 39 | if (next != null && !next.startsWith('--')) { |
| 40 | push(flags, name, next) |
| 41 | i += 1 |
| 42 | } else { |
| 43 | push(flags, name, 'true') |
| 44 | } |
| 45 | continue |
| 46 | } |
| 47 | if (/^-[A-Za-z][\w-]*$/.test(token)) { |
| 48 | // Short flag (e.g. `-h`). Only a dash followed by a letter counts as a |
| 49 | // flag; text that merely starts with `-` — a markdown task `- [ ] …`, a |
| 50 | // list item, or a negative number — stays positional so |
| 51 | // `zn capture "- [ ] task"` works. Boolean only (no value / `-abc` |
| 52 | // combining — unnecessary for our surface). |
| 53 | push(flags, token.slice(1), 'true') |
| 54 | continue |
| 55 | } |
| 56 | positionals.push(token) |
| 57 | } |
| 58 | return { positionals, flags } |
| 59 | } |
| 60 | |
| 61 | function push(flags: Map<string, string[]>, name: string, value: string): void { |
| 62 | const existing = flags.get(name) |
no test coverage detected