|
| 1 | +use clap::Args; |
| 2 | +use clap::Parser; |
| 3 | +use codex_utils_cli::CliConfigOverrides; |
| 4 | + |
| 5 | +#[derive(Parser, Debug, Default)] |
| 6 | +#[command(version)] |
| 7 | +pub struct Cli { |
| 8 | + #[clap(skip)] |
| 9 | + pub config_overrides: CliConfigOverrides, |
| 10 | + |
| 11 | + #[command(subcommand)] |
| 12 | + pub command: Option<Command>, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Debug, clap::Subcommand)] |
| 16 | +pub enum Command { |
| 17 | + /// Submit a new Codex Cloud task without launching the TUI. |
| 18 | + Exec(ExecCommand), |
| 19 | + /// Show the status of a Codex Cloud task. |
| 20 | + Status(StatusCommand), |
| 21 | + /// List Codex Cloud tasks. |
| 22 | + List(ListCommand), |
| 23 | + /// Apply the diff for a Codex Cloud task locally. |
| 24 | + Apply(ApplyCommand), |
| 25 | + /// Show the unified diff for a Codex Cloud task. |
| 26 | + Diff(DiffCommand), |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Args)] |
| 30 | +pub struct ExecCommand { |
| 31 | + /// Task prompt to run in Codex Cloud. |
| 32 | + #[arg(value_name = "QUERY")] |
| 33 | + pub query: Option<String>, |
| 34 | + |
| 35 | + /// Target environment identifier (see `codex cloud` to browse). |
| 36 | + #[arg(long = "env", value_name = "ENV_ID")] |
| 37 | + pub environment: String, |
| 38 | + |
| 39 | + /// Number of assistant attempts (best-of-N). |
| 40 | + #[arg( |
| 41 | + long = "attempts", |
| 42 | + default_value_t = 1usize, |
| 43 | + value_parser = parse_attempts |
| 44 | + )] |
| 45 | + pub attempts: usize, |
| 46 | + |
| 47 | + /// Git branch to run in Codex Cloud (defaults to current branch). |
| 48 | + #[arg(long = "branch", value_name = "BRANCH")] |
| 49 | + pub branch: Option<String>, |
| 50 | +} |
| 51 | + |
| 52 | +fn parse_attempts(input: &str) -> Result<usize, String> { |
| 53 | + let value: usize = input |
| 54 | + .parse() |
| 55 | + .map_err(|_| "attempts must be an integer between 1 and 4".to_string())?; |
| 56 | + if (1..=4).contains(&value) { |
| 57 | + Ok(value) |
| 58 | + } else { |
| 59 | + Err("attempts must be between 1 and 4".to_string()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn parse_limit(input: &str) -> Result<i64, String> { |
| 64 | + let value: i64 = input |
| 65 | + .parse() |
| 66 | + .map_err(|_| "limit must be an integer between 1 and 20".to_string())?; |
| 67 | + if (1..=20).contains(&value) { |
| 68 | + Ok(value) |
| 69 | + } else { |
| 70 | + Err("limit must be between 1 and 20".to_string()) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[derive(Debug, Args)] |
| 75 | +pub struct StatusCommand { |
| 76 | + /// Codex Cloud task identifier to inspect. |
| 77 | + #[arg(value_name = "TASK_ID")] |
| 78 | + pub task_id: String, |
| 79 | +} |
| 80 | + |
| 81 | +#[derive(Debug, Args)] |
| 82 | +pub struct ListCommand { |
| 83 | + /// Filter tasks by environment identifier. |
| 84 | + #[arg(long = "env", value_name = "ENV_ID")] |
| 85 | + pub environment: Option<String>, |
| 86 | + |
| 87 | + /// Maximum number of tasks to return (1-20). |
| 88 | + #[arg(long = "limit", default_value_t = 20, value_parser = parse_limit, value_name = "N")] |
| 89 | + pub limit: i64, |
| 90 | + |
| 91 | + /// Pagination cursor returned by a previous call. |
| 92 | + #[arg(long = "cursor", value_name = "CURSOR")] |
| 93 | + pub cursor: Option<String>, |
| 94 | + |
| 95 | + /// Emit JSON instead of plain text. |
| 96 | + #[arg(long = "json", default_value_t = false)] |
| 97 | + pub json: bool, |
| 98 | +} |
| 99 | + |
| 100 | +#[derive(Debug, Args)] |
| 101 | +pub struct ApplyCommand { |
| 102 | + /// Codex Cloud task identifier to apply. |
| 103 | + #[arg(value_name = "TASK_ID")] |
| 104 | + pub task_id: String, |
| 105 | + |
| 106 | + /// Attempt number to apply (1-based). |
| 107 | + #[arg(long = "attempt", value_parser = parse_attempts, value_name = "N")] |
| 108 | + pub attempt: Option<usize>, |
| 109 | +} |
| 110 | + |
| 111 | +#[derive(Debug, Args)] |
| 112 | +pub struct DiffCommand { |
| 113 | + /// Codex Cloud task identifier to display. |
| 114 | + #[arg(value_name = "TASK_ID")] |
| 115 | + pub task_id: String, |
| 116 | + |
| 117 | + /// Attempt number to display (1-based). |
| 118 | + #[arg(long = "attempt", value_parser = parse_attempts, value_name = "N")] |
| 119 | + pub attempt: Option<usize>, |
| 120 | +} |
0 commit comments