Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions crates/chat-cli/src/cli/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::{
Args,
Subcommand,
};
use crossterm::style::Stylize;
use eyre::{
Result,
WrapErr,
Expand Down Expand Up @@ -37,15 +38,15 @@ pub enum SettingsSubcommands {
#[derive(Clone, Debug, Args, PartialEq, Eq)]
#[command(subcommand_negates_reqs = true)]
#[command(args_conflicts_with_subcommands = true)]
#[command(group(ArgGroup::new("vals").requires("key").args(&["value", "delete", "format"])))]
#[command(group(ArgGroup::new("vals").requires("key").args(&["value", "format"])))]
pub struct SettingsArgs {
#[command(subcommand)]
cmd: Option<SettingsSubcommands>,
/// key
key: Option<String>,
/// value
value: Option<String>,
/// Delete a value
/// Delete a key (No value needed)
#[arg(long, short)]
delete: bool,
/// Format of the output
Expand Down Expand Up @@ -87,11 +88,26 @@ impl SettingsArgs {
},
None => {
let Some(key) = &self.key else {
if self.delete {
return Err(eyre::eyre!(
"the argument {} requires a {}\n Usage: q settings {} {}",
"'--delete'".yellow(),
"<KEY>".green(),
"--delete".yellow(),
"<KEY>".green()
));
}
return Ok(ExitCode::SUCCESS);
};

let key = Setting::try_from(key.as_str())?;
match (&self.value, self.delete) {
(Some(_), true) => Err(eyre::eyre!(
"the argument {} cannot be used with {}\n Usage: q settings {} {key}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, whats the behavior when you run q settings -d (without providing a key)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kkashilk that's a good catch, when we run the same without my code Clap handles it, but it is still giving output which can be confusing, where it says [VALUE] is needed. I can handle this scenario for delete in next revision

error: the following required arguments were not provided:
  <KEY>

Usage: q settings --delete <KEY> [VALUE]

"'--delete'".yellow(),
"'[VALUE]'".yellow(),
"--delete".yellow()
)),
(None, false) => match os.database.settings.get(key) {
Some(value) => {
match self.format {
Expand Down Expand Up @@ -147,9 +163,34 @@ impl SettingsArgs {

Ok(ExitCode::SUCCESS)
},
_ => Ok(ExitCode::SUCCESS),
}
},
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_delete_with_value_error() {
let mut os = Os::new().await.unwrap();

let settings_args = SettingsArgs {
cmd: None,
key: Some("chat.defaultAgent".to_string()),
value: Some("test_value".to_string()),
delete: true,
format: OutputFormat::Plain,
};

let result = settings_args.execute(&mut os).await;

assert!(result.is_err());
let error_msg = result.unwrap_err().to_string();
assert!(error_msg.contains("the argument"));
assert!(error_msg.contains("--delete"));
assert!(error_msg.contains("Usage:"));
}
}