nestrs-cli-rs 0.1.0

Rust port of the Nest CLI for the nestrs organization.
Documentation
//! Upstream source: `../nest-cli/lib/utils/extra-args-warning.ts`.

use crate::ui::ERROR_PREFIX;

pub fn exit_if_extra_args(
    command_name: &str,
    parent_args: &[String],
    expected_arg_count: usize,
) -> Result<(), ExtraArgsError> {
    if parent_args.len() <= expected_arg_count {
        Ok(())
    } else {
        Err(ExtraArgsError {
            command_name: command_name.to_string(),
            extra_args: parent_args[expected_arg_count..].to_vec(),
        })
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ExtraArgsError {
    pub command_name: String,
    pub extra_args: Vec<String>,
}

impl ExtraArgsError {
    pub fn usage_message(&self) -> String {
        format!(
            "Run \"nest {} --help\" for usage information.\n",
            self.command_name
        )
    }
}

impl std::fmt::Display for ExtraArgsError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            formatter,
            "{ERROR_PREFIX} Too many arguments. Unexpected extra argument(s): {}",
            self.extra_args.join(", ")
        )
    }
}

impl std::error::Error for ExtraArgsError {}