Elffuss BraidHub

kikocis/continue public

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