Elffuss BraidHub

kikocis/continue public

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