sandbox_runtime/
cli.rs

1//! CLI parsing and execution.
2
3use std::path::PathBuf;
4
5use clap::Parser;
6
7/// Sandbox Runtime - OS-level sandboxing tool
8#[derive(Parser, Debug)]
9#[command(name = "srt")]
10#[command(about = "Sandbox Runtime - enforce filesystem and network restrictions on processes")]
11#[command(version)]
12pub struct Cli {
13    /// Enable debug logging
14    #[arg(short = 'd', long = "debug")]
15    pub debug: bool,
16
17    /// Path to settings file (default: ~/.srt-settings.json)
18    #[arg(short = 's', long = "settings")]
19    pub settings: Option<PathBuf>,
20
21    /// Run command string directly (sh -c mode)
22    #[arg(short = 'c')]
23    pub command: Option<String>,
24
25    /// Command and arguments to run
26    #[arg(trailing_var_arg = true)]
27    pub args: Vec<String>,
28}
29
30impl Cli {
31    /// Parse CLI arguments.
32    pub fn parse_args() -> Self {
33        Cli::parse()
34    }
35
36    /// Get the command to execute.
37    /// Returns (command_string, shell_mode)
38    /// - shell_mode = true when using -c flag
39    /// - shell_mode = false when using positional args
40    pub fn get_command(&self) -> Option<(String, bool)> {
41        if let Some(ref cmd) = self.command {
42            Some((cmd.clone(), true))
43        } else if !self.args.is_empty() {
44            // Join args with proper quoting
45            let cmd = crate::utils::join_args(&self.args);
46            Some((cmd, false))
47        } else {
48            None
49        }
50    }
51
52    /// Get the settings file path.
53    pub fn get_settings_path(&self) -> Option<PathBuf> {
54        self.settings.clone().or_else(crate::config::default_settings_path)
55    }
56}