Skip to content

Commit 449d552

Browse files
committed
Disable colored help and error output in commands
1 parent e1ba6da commit 449d552

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/zulip.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub mod api;
22
pub mod client;
3-
mod commands;
3+
pub mod commands;
44

55
use crate::db::notifications::add_metadata;
66
use crate::db::notifications::{self, delete_ping, move_indices, record_ping, Identifier};
@@ -14,9 +14,10 @@ use crate::team_data::{people, teams};
1414
use crate::utils::pluralize;
1515
use crate::zulip::api::{MessageApiResponse, Recipient};
1616
use crate::zulip::client::ZulipClient;
17-
use crate::zulip::commands::{ChatCommand, LookupCmd, StreamCommand, WorkqueueCmd, WorkqueueLimit};
17+
use crate::zulip::commands::{
18+
parse_no_color, ChatCommand, LookupCmd, StreamCommand, WorkqueueCmd, WorkqueueLimit,
19+
};
1820
use anyhow::{format_err, Context as _};
19-
use clap::Parser;
2021
use rust_team_data::v1::TeamKind;
2122
use std::fmt::Write as _;
2223
use subtle::ConstantTimeEq;
@@ -192,7 +193,7 @@ fn handle_command<'a>(
192193

193194
// Missing stream means that this is a direct message
194195
if message_data.stream_id.is_none() {
195-
let cmd = ChatCommand::try_parse_from(words)?;
196+
let cmd = parse_no_color::<ChatCommand, _>(words.into_iter())?;
196197
match cmd {
197198
ChatCommand::Acknowledge { identifier } => {
198199
acknowledge(&ctx, gh_id, (&identifier).into()).await
@@ -218,7 +219,7 @@ fn handle_command<'a>(
218219
if cmd_index >= words.len() {
219220
return Ok(Some("Unknown command".to_string()));
220221
}
221-
let cmd = StreamCommand::try_parse_from(&words[cmd_index..])?;
222+
let cmd = parse_no_color::<StreamCommand, _>(words[cmd_index..].into_iter().copied())?;
222223
match cmd {
223224
StreamCommand::EndTopic => {
224225
post_waiter(&ctx, message_data, WaitingMessage::end_topic())

src/zulip/commands.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::db::notifications::Identifier;
22
use crate::db::review_prefs::RotationMode;
3+
use clap::{ColorChoice, Parser};
34
use std::num::NonZeroU32;
45
use std::str::FromStr;
56

67
/// Command sent in a DM with triagebot on Zulip.
78
#[derive(clap::Parser, Debug)]
8-
#[clap(override_usage("<command>"))]
9+
#[clap(override_usage("<command>"), disable_colored_help(true))]
910
pub enum ChatCommand {
1011
/// Acknowledge a notification
1112
#[clap(alias = "ack")]
@@ -138,7 +139,7 @@ impl FromStr for IdentifierCli {
138139

139140
/// Command sent in a Zulip stream after `@**triagebot**`.
140141
#[derive(clap::Parser, Debug)]
141-
#[clap(override_usage = "`@triagebot <command>`")]
142+
#[clap(override_usage("`@triagebot <command>`"), disable_colored_help(true))]
142143
pub enum StreamCommand {
143144
/// End the current topic.
144145
#[clap(alias = "await")]
@@ -157,3 +158,12 @@ pub enum StreamCommand {
157158
/// Update docs
158159
DocsUpdate,
159160
}
161+
162+
/// Helper function to parse CLI arguments without any colored help or error output.
163+
pub fn parse_no_color<'a, T: Parser, I: Iterator<Item = &'a str>>(input: I) -> anyhow::Result<T> {
164+
let matches = T::command()
165+
.color(ColorChoice::Never)
166+
.try_get_matches_from(input)?;
167+
let value = T::from_arg_matches(&matches)?;
168+
Ok(value)
169+
}

0 commit comments

Comments
 (0)