Skip to content

Commit 71ba5f1

Browse files
fix: make q settings delete functionality user friendly (#3042)
1 parent d40ac31 commit 71ba5f1

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

crates/chat-cli/src/cli/settings.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use clap::{
66
Args,
77
Subcommand,
88
};
9+
use crossterm::style::Stylize;
910
use eyre::{
1011
Result,
1112
WrapErr,
@@ -37,15 +38,15 @@ pub enum SettingsSubcommands {
3738
#[derive(Clone, Debug, Args, PartialEq, Eq)]
3839
#[command(subcommand_negates_reqs = true)]
3940
#[command(args_conflicts_with_subcommands = true)]
40-
#[command(group(ArgGroup::new("vals").requires("key").args(&["value", "delete", "format"])))]
41+
#[command(group(ArgGroup::new("vals").requires("key").args(&["value", "format"])))]
4142
pub struct SettingsArgs {
4243
#[command(subcommand)]
4344
cmd: Option<SettingsSubcommands>,
4445
/// key
4546
key: Option<String>,
4647
/// value
4748
value: Option<String>,
48-
/// Delete a value
49+
/// Delete a key (No value needed)
4950
#[arg(long, short)]
5051
delete: bool,
5152
/// Format of the output
@@ -87,11 +88,26 @@ impl SettingsArgs {
8788
},
8889
None => {
8990
let Some(key) = &self.key else {
91+
if self.delete {
92+
return Err(eyre::eyre!(
93+
"the argument {} requires a {}\n Usage: q settings {} {}",
94+
"'--delete'".yellow(),
95+
"<KEY>".green(),
96+
"--delete".yellow(),
97+
"<KEY>".green()
98+
));
99+
}
90100
return Ok(ExitCode::SUCCESS);
91101
};
92102

93103
let key = Setting::try_from(key.as_str())?;
94104
match (&self.value, self.delete) {
105+
(Some(_), true) => Err(eyre::eyre!(
106+
"the argument {} cannot be used with {}\n Usage: q settings {} {key}",
107+
"'--delete'".yellow(),
108+
"'[VALUE]'".yellow(),
109+
"--delete".yellow()
110+
)),
95111
(None, false) => match os.database.settings.get(key) {
96112
Some(value) => {
97113
match self.format {
@@ -147,9 +163,34 @@ impl SettingsArgs {
147163

148164
Ok(ExitCode::SUCCESS)
149165
},
150-
_ => Ok(ExitCode::SUCCESS),
151166
}
152167
},
153168
}
154169
}
155170
}
171+
172+
#[cfg(test)]
173+
mod tests {
174+
use super::*;
175+
176+
#[tokio::test]
177+
async fn test_delete_with_value_error() {
178+
let mut os = Os::new().await.unwrap();
179+
180+
let settings_args = SettingsArgs {
181+
cmd: None,
182+
key: Some("chat.defaultAgent".to_string()),
183+
value: Some("test_value".to_string()),
184+
delete: true,
185+
format: OutputFormat::Plain,
186+
};
187+
188+
let result = settings_args.execute(&mut os).await;
189+
190+
assert!(result.is_err());
191+
let error_msg = result.unwrap_err().to_string();
192+
assert!(error_msg.contains("the argument"));
193+
assert!(error_msg.contains("--delete"));
194+
assert!(error_msg.contains("Usage:"));
195+
}
196+
}

0 commit comments

Comments
 (0)