Elffuss BraidHub

kikocis/continue public

57d99ced5e24 · 3.8 KiBRawPermalink
1export interface CommandLineArgs {
2 configPath?: string;
3 organizationSlug?: string; // Organization slug to use for this session
4 prompt?: string; // Optional prompt argument
5 resume?: boolean; // Resume from last session
6 readonly?: boolean; // Start in plan mode (backward compatibility)
7 rules?: string[]; // Array of rule specifications
8 mcps?: string[]; // Array of MCP server slugs from the hub
9 models?: string[]; // Array of model slugs from the hub
10 prompts?: string[]; // Array of prompt slugs from the hub
11 format?: "json"; // Output format for headless mode
12}
13
14// Re-export hub loader functions for backward compatibility
15export { loadMcpFromHub, processRule } from "./hubLoader.js";
16// Alias processRule as processPromptOrRule for backward compatibility
17export { processRule as processPromptOrRule } from "./hubLoader.js";
18
19/**
20 * Extract values for a specific flag that can appear multiple times
21 */
22function extractMultipleFlags(args: string[], flagName: string): string[] {
23 const indices: number[] = [];
24 for (let i = 0; i < args.length; i++) {
25 if (args[i] === flagName) {
26 indices.push(i);
27 }
28 }
29
30 const values: string[] = [];
31 for (const index of indices) {
32 if (index + 1 < args.length && !args[index + 1].startsWith("--")) {
33 values.push(args[index + 1]);
34 }
35 }
36 return values;
37}
38
39/**
40 * Extract value for a single-value flag
41 */
42function extractSingleFlag(
43 args: string[],
44 flagName: string,
45): string | undefined {
46 const index = args.indexOf(flagName);
47 if (index !== -1 && index + 1 < args.length && !args[index + 1].startsWith("--")) {
48 return args[index + 1];
49 }
50 return undefined;
51}
52
53/**
54 * Parse command line arguments for non-permission related flags
55 * @deprecated Most functionality has been moved to services. This is only kept for backward compatibility.
56 * @returns Parsed command line arguments
57 */
58export function parseArgs(): CommandLineArgs {
59 const args = process.argv.slice(2);
60 const result: CommandLineArgs = {};
61
62 // Parse boolean flags
63 if (args.includes("--resume")) {
64 result.resume = true;
65 }
66 if (args.includes("--readonly")) {
67 result.readonly = true;
68 }
69
70 // Parse single-value flags
71 const formatValue = extractSingleFlag(args, "--format");
72 if (formatValue === "json") {
73 result.format = "json";
74 }
75
76 result.configPath = extractSingleFlag(args, "--config");
77
78 const orgValue = extractSingleFlag(args, "--org");
79 if (orgValue) {
80 result.organizationSlug =
81 orgValue.toLowerCase() === "personal" ? undefined : orgValue;
82 }
83
84 // Parse multi-value flags
85 const rules = extractMultipleFlags(args, "--rule");
86 if (rules.length > 0) {
87 result.rules = rules;
88 } else if (args.includes("--rule")) {
89 result.rules = [];
90 }
91
92 const mcps = extractMultipleFlags(args, "--mcp");
93 if (mcps.length > 0) {
94 result.mcps = mcps;
95 } else if (args.includes("--mcp")) {
96 // Ensure we have an empty array if --mcp flag is present but has no values
97 result.mcps = [];
98 }
99
100 const models = extractMultipleFlags(args, "--model");
101 if (models.length > 0) {
102 result.models = models;
103 } else if (args.includes("--model")) {
104 result.models = [];
105 }
106
107 const prompts = extractMultipleFlags(args, "--prompt");
108 if (prompts.length > 0) {
109 result.prompts = prompts;
110 } else if (args.includes("--prompt")) {
111 result.prompts = [];
112 }
113
114 // Extract prompt from non-flag arguments
115 const flagsWithValues = [
116 "--config",
117 "--org",
118 "--rule",
119 "--mcp",
120 "--model",
121 "--prompt",
122 "--format",
123 ];
124 const nonFlagArgs = args.filter((arg, index) => {
125 if (arg.startsWith("--") || arg === "-p") return false;
126 const prevArg = index > 0 ? args[index - 1] : "";
127 return !flagsWithValues.includes(prevArg);
128 });
129
130 if (nonFlagArgs.length > 0) {
131 result.prompt = nonFlagArgs[nonFlagArgs.length - 1];
132 }
133
134 return result;
135}