Skip to main content

systemprompt_cli/runner/args/
assemble.rs

1//! Assembling the runtime config and the forwarded argument vector.
2//!
3//! Split from the clap definitions next door because these are the only pieces
4//! with behaviour rather than shape: the verbosity/output precedence, and the
5//! reconstruction that decides what a profile-routed subprocess actually
6//! receives.
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11use super::{Cli, Commands};
12use crate::cli_settings::{CliConfig, ColorMode, OutputFormat, VerbosityLevel};
13use crate::env_overrides::EnvOverrides;
14
15pub fn build_cli_config(cli: &Cli, env: &EnvOverrides) -> CliConfig {
16    let mut cfg = CliConfig::resolve(env);
17
18    if cli.verbosity.debug {
19        cfg = cfg.with_verbosity(VerbosityLevel::Debug);
20    } else if cli.verbosity.verbose {
21        cfg = cfg.with_verbosity(VerbosityLevel::Verbose);
22    } else if cli.verbosity.quiet {
23        cfg = cfg.with_verbosity(VerbosityLevel::Quiet);
24    }
25
26    if cli.output.json {
27        cfg = cfg.with_output_format(OutputFormat::Json);
28    } else if cli.output.yaml {
29        cfg = cfg.with_output_format(OutputFormat::Yaml);
30    }
31
32    if cli.display.no_color {
33        cfg = cfg.with_color_mode(ColorMode::Never);
34    }
35
36    if cli.display.non_interactive {
37        cfg = cfg.with_interactive(false);
38    }
39
40    cfg = cfg.with_profile_override(cli.profile_opts.profile.clone());
41
42    cfg
43}
44
45pub fn reconstruct_args(cli: &Cli) -> Vec<String> {
46    let original: Vec<String> = std::env::args().skip(1).collect();
47    reconstruct_args_from(cli, &original)
48}
49
50pub fn reconstruct_args_from(cli: &Cli, original_args: &[String]) -> Vec<String> {
51    let mut args = Vec::new();
52
53    if cli.verbosity.debug {
54        args.push("--debug".to_owned());
55    } else if cli.verbosity.verbose {
56        args.push("--verbose".to_owned());
57    } else if cli.verbosity.quiet {
58        args.push("--quiet".to_owned());
59    }
60
61    if cli.output.json {
62        args.push("--json".to_owned());
63    } else if cli.output.yaml {
64        args.push("--yaml".to_owned());
65    }
66
67    if cli.display.no_color {
68        args.push("--no-color".to_owned());
69    }
70
71    if cli.display.non_interactive {
72        args.push("--non-interactive".to_owned());
73    }
74
75    if let Some(ref profile) = cli.profile_opts.profile {
76        args.push("--profile".to_owned());
77        args.push(profile.clone());
78    }
79
80    let mut skip_next = false;
81    for arg in original_args {
82        if skip_next {
83            skip_next = false;
84            continue;
85        }
86        if arg == "--profile" {
87            skip_next = true;
88            continue;
89        }
90        if arg.starts_with("--profile=") {
91            continue;
92        }
93        if !args.contains(arg)
94            && !matches!(
95                arg.as_str(),
96                "--debug"
97                    | "--verbose"
98                    | "-v"
99                    | "--quiet"
100                    | "-q"
101                    | "--json"
102                    | "--yaml"
103                    | "--no-color"
104                    | "--non-interactive"
105            )
106        {
107            args.push(arg.clone());
108        }
109    }
110
111    args
112}
113
114pub fn has_local_export_flag(command: Option<&Commands>) -> bool {
115    let args: Vec<String> = std::env::args().collect();
116    has_local_export_flag_in(command, &args)
117}
118
119pub fn has_local_export_flag_in(command: Option<&Commands>, args: &[String]) -> bool {
120    if !matches!(command, Some(Commands::Analytics(_))) {
121        return false;
122    }
123    args.iter()
124        .any(|arg| arg == "--export" || arg.starts_with("--export="))
125}