Skip to main content

cli/lib/utils/
extra_args_warning.rs

1//! Upstream source: `../nest-cli/lib/utils/extra-args-warning.ts`.
2
3use crate::ui::ERROR_PREFIX;
4
5pub fn exit_if_extra_args(
6    command_name: &str,
7    parent_args: &[String],
8    expected_arg_count: usize,
9) -> Result<(), ExtraArgsError> {
10    if parent_args.len() <= expected_arg_count {
11        Ok(())
12    } else {
13        Err(ExtraArgsError {
14            command_name: command_name.to_string(),
15            extra_args: parent_args[expected_arg_count..].to_vec(),
16        })
17    }
18}
19
20#[derive(Clone, Debug, PartialEq, Eq)]
21pub struct ExtraArgsError {
22    pub command_name: String,
23    pub extra_args: Vec<String>,
24}
25
26impl ExtraArgsError {
27    pub fn usage_message(&self) -> String {
28        format!(
29            "Run \"nest {} --help\" for usage information.\n",
30            self.command_name
31        )
32    }
33}
34
35impl std::fmt::Display for ExtraArgsError {
36    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(
38            formatter,
39            "{ERROR_PREFIX} Too many arguments. Unexpected extra argument(s): {}",
40            self.extra_args.join(", ")
41        )
42    }
43}
44
45impl std::error::Error for ExtraArgsError {}