Skip to main content

start_command/
launch_identity_options.rs

1//! Launch options about the identity of a new execution:
2//!
3//! - `--append-attempt`: run as the next attempt of the tracked execution
4//!   named by `--session-id` instead of being refused (issue #229)
5//! - `--continues <id>`: record that this execution continues an earlier one,
6//!   which `--status` and `--upload-log` follow (issue #231)
7//!
8//! Both only apply when a command is launched, not to the query modes.
9use crate::args_parser::WrapperOptions;
10use crate::args_parser_queries::active_query_modes;
11
12const CONTINUES_ERROR: &str = "Option --continues requires a UUID or session name argument";
13
14/// Parse one launch identity option; `Ok(0)` when `args[index]` is not one.
15pub fn parse(args: &[String], index: usize, options: &mut WrapperOptions) -> Result<usize, String> {
16    let arg = args[index].as_str();
17    if arg == "--append-attempt" {
18        options.append_attempt = true;
19        return Ok(1);
20    }
21    if arg == "--continues" {
22        return match args.get(index + 1).filter(|next| !next.starts_with('-')) {
23            Some(next) => {
24                options.continues = Some(next.clone());
25                Ok(2)
26            }
27            None => Err(CONTINUES_ERROR.into()),
28        };
29    }
30    if let Some(value) = arg.strip_prefix("--continues=") {
31        if value.is_empty() {
32            return Err(CONTINUES_ERROR.into());
33        }
34        options.continues = Some(value.to_string());
35        return Ok(1);
36    }
37    Ok(0)
38}
39
40/// Validate the launch identity options.
41pub fn validate(options: &WrapperOptions) -> Result<(), String> {
42    let mut used = Vec::new();
43    if options.append_attempt {
44        used.push("--append-attempt");
45    }
46    if options.continues.is_some() {
47        used.push("--continues");
48    }
49    let Some(first) = used.first() else {
50        return Ok(());
51    };
52    if !active_query_modes(options).is_empty() {
53        return Err(format!(
54            "{} option is only valid when launching a command",
55            first
56        ));
57    }
58    if options.append_attempt && options.session_id.is_none() {
59        return Err("--append-attempt option requires --session-id <uuid>".into());
60    }
61    Ok(())
62}