MCPcopy Create free account
hub / github.com/cli/cli / parseArgv

Function parseArgv

evals/src/core/calls.ts:56–85  ·  view source on GitHub ↗
(args: string[], spec: ArgvSpec = {})

Source from the content-addressed store, hash-verified

54
55/** Parses gh argv into positional words, flag values, and `gh api` fields. */
56export function parseArgv(args: string[], spec: ArgvSpec = {}): ParsedArgv {
57 const valued = new Set<string>([...commonValuedFlags, ...spec.valued ?? []]);
58 const joined = new Set(spec.joined ?? []);
59 const words: string[] = [];
60 const options = new Map<string, string>();
61 const fields: string[] = [];
62 const set = (flag: string, value: string) => {
63 flag = spec.aliases?.[flag] ?? flag;
64 const previous = options.get(flag);
65 options.set(flag, joined.has(flag) && previous !== undefined ? `${previous},${value}` : value);
66 if (fieldFlags.has(flag)) fields.push(value);
67 };
68 let help = false;
69 for (let index = 0; index < args.length; index++) {
70 const arg = args[index]!;
71 if (arg === "--") { words.push(...args.slice(index + 1)); break; }
72 if (arg === "--help" || arg === "-h") help = true;
73 if (!arg.startsWith("-")) { words.push(arg); continue; }
74 const short = arg.slice(0, 2);
75 if (!arg.startsWith("--") && arg.length > 2 && valued.has(short)) {
76 set(short, arg.slice(arg[2] === "=" ? 3 : 2));
77 continue;
78 }
79 const equals = arg.indexOf("=");
80 if (equals > 0) { set(arg.slice(0, equals), arg.slice(equals + 1)); continue; }
81 if (valued.has(arg)) set(arg, args[++index] ?? "");
82 else options.set(arg, "true");
83 }
84 return { words, options, fields, help };
85}

Callers 1

commandFunction · 0.85

Calls 2

setFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected