Skip to content

Commit fcb4c5d

Browse files
committed
refactor(cli): share gateway argv prefix scan
1 parent 49869c2 commit fcb4c5d

1 file changed

Lines changed: 36 additions & 41 deletions

File tree

src/cli/gateway-run-argv.ts

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,41 @@ export function consumeGatewayFastPathRootOptionToken(
8585
return 0;
8686
}
8787

88-
/** Resolve the gateway command path from raw argv for catalog/policy lookups. */
89-
export function resolveGatewayCatalogCommandPath(argv: string[]): string[] | null {
88+
function resolveGatewayCommandStart(argv: string[]): {
89+
args: string[];
90+
startIndex: number;
91+
} | null {
9092
const args = argv.slice(2);
91-
let sawGateway = false;
92-
9393
for (let index = 0; index < args.length; index += 1) {
9494
const arg = args[index];
9595
if (!arg || arg === "--") {
96-
break;
96+
return null;
9797
}
98-
if (!sawGateway) {
99-
const consumed = consumeRootOptionToken(args, index);
100-
if (consumed > 0) {
101-
index += consumed - 1;
102-
continue;
103-
}
104-
if (arg.startsWith("-")) {
105-
continue;
106-
}
107-
if (arg !== "gateway") {
108-
return null;
109-
}
110-
sawGateway = true;
98+
const consumed = consumeRootOptionToken(args, index);
99+
if (consumed > 0) {
100+
index += consumed - 1;
111101
continue;
112102
}
103+
if (arg.startsWith("-")) {
104+
continue;
105+
}
106+
return arg === "gateway" ? { args, startIndex: index + 1 } : null;
107+
}
108+
return null;
109+
}
113110

114-
const consumed = consumeGatewayRunOptionToken(args, index);
111+
/** Resolve the gateway command path from raw argv for catalog/policy lookups. */
112+
export function resolveGatewayCatalogCommandPath(argv: string[]): string[] | null {
113+
const gateway = resolveGatewayCommandStart(argv);
114+
if (!gateway) {
115+
return null;
116+
}
117+
for (let index = gateway.startIndex; index < gateway.args.length; index += 1) {
118+
const arg = gateway.args[index];
119+
if (!arg || arg === "--") {
120+
break;
121+
}
122+
const consumed = consumeGatewayRunOptionToken(gateway.args, index);
115123
if (consumed > 0) {
116124
index += consumed - 1;
117125
continue;
@@ -122,44 +130,31 @@ export function resolveGatewayCatalogCommandPath(argv: string[]): string[] | nul
122130
return ["gateway", arg];
123131
}
124132

125-
return sawGateway ? ["gateway"] : null;
133+
return ["gateway"];
126134
}
127135

128136
/** Resolve destructive gateway-run flags before Commander registration. */
129137
export function resolveGatewayRunPreBootstrapOptions(
130138
argv: string[],
131139
): { force: boolean; reset: boolean } | null {
132-
const args = argv.slice(2);
140+
const gateway = resolveGatewayCommandStart(argv);
141+
if (!gateway) {
142+
return null;
143+
}
133144
let force = false;
134145
let reset = false;
135-
let sawGateway = false;
136146
let sawRun = false;
137147

138-
for (let index = 0; index < args.length; index += 1) {
139-
const arg = args[index];
148+
for (let index = gateway.startIndex; index < gateway.args.length; index += 1) {
149+
const arg = gateway.args[index];
140150
if (!arg || arg === "--") {
141151
break;
142152
}
143-
if (!sawGateway) {
144-
const consumed = consumeRootOptionToken(args, index);
145-
if (consumed > 0) {
146-
index += consumed - 1;
147-
continue;
148-
}
149-
if (arg.startsWith("-")) {
150-
continue;
151-
}
152-
if (arg !== "gateway") {
153-
return null;
154-
}
155-
sawGateway = true;
156-
continue;
157-
}
158153
if (!sawRun && arg === "run") {
159154
sawRun = true;
160155
continue;
161156
}
162-
const consumed = consumeGatewayRunPreBootstrapOptionToken(args, index);
157+
const consumed = consumeGatewayRunPreBootstrapOptionToken(gateway.args, index);
163158
if (consumed > 0) {
164159
if (arg === "--force") {
165160
force = true;
@@ -179,5 +174,5 @@ export function resolveGatewayRunPreBootstrapOptions(
179174
}
180175
}
181176

182-
return sawGateway ? { force, reset } : null;
177+
return { force, reset };
183178
}

0 commit comments

Comments
 (0)