1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// Copyright (C) 2024 - 2025 Automated Design Corp. All Rights Reserved.
//
//! Standard CLI argument parsing for external modules.
use super::error::IpcError;
/// Parsed command-line arguments common to all external modules.
pub struct ModuleArgs {
/// IPC server address (from `--ipc-address`, default `"127.0.0.1:9100"`)
pub ipc_address: String,
/// Module instance name (from `--module-name`, required)
pub module_name: String,
/// Optional JSON configuration string (from `--config` / `-c`)
pub config_json: Option<String>,
}
impl ModuleArgs {
/// Parse standard module arguments from `std::env::args()`.
///
/// Recognised flags:
/// - `--ipc-address <addr>` (default `127.0.0.1:9100`)
/// - `--module-name <name>` (required)
/// - `--config <json>` or `-c <json>` (optional)
pub fn from_env() -> Result<Self, IpcError> {
let args: Vec<String> = std::env::args().collect();
let mut ipc_address = "127.0.0.1:9100".to_string();
let mut module_name: Option<String> = None;
let mut config_json: Option<String> = None;
let mut i = 1; // skip argv[0]
while i < args.len() {
match args[i].as_str() {
"--ipc-address" => {
i += 1;
if i >= args.len() {
return Err(IpcError::InvalidMessage("--ipc-address requires a value".into()));
}
ipc_address = args[i].clone();
}
"--module-name" => {
i += 1;
if i >= args.len() {
return Err(IpcError::InvalidMessage("--module-name requires a value".into()));
}
module_name = Some(args[i].clone());
}
"--config" | "-c" => {
i += 1;
if i >= args.len() {
return Err(IpcError::InvalidMessage("--config requires a value".into()));
}
config_json = Some(args[i].clone());
}
_ => {
// Ignore unknown args for forward compatibility
}
}
i += 1;
}
let module_name = module_name.ok_or_else(|| {
IpcError::InvalidMessage("--module-name is required".into())
})?;
Ok(Self {
ipc_address,
module_name,
config_json,
})
}
}