notalawyer_clap/
lib.rs

1pub use notalawyer::include_notice;
2
3pub trait ParseExt: clap::Parser {
4    fn parse_with_license_notice(notice: &str) -> Self {
5        fn patch_command(cmd: clap::Command) -> clap::Command {
6            cmd.arg(clap::arg!(--"license-notice" "Show license notices"))
7        }
8
9        let matches = patch_command(Self::command_for_update()).get_matches();
10        if matches.get_flag("license-notice") {
11            print!("{notice}");
12            std::process::exit(0);
13        }
14        // For better error message, we need to use build Command twice.
15        let mut command = patch_command(Self::command());
16        let mut matches = command.clone().get_matches();
17        Self::from_arg_matches_mut(&mut matches)
18            .map_err(|err| err.format(&mut command))
19            .unwrap_or_else(|err| err.exit())
20    }
21}
22
23impl<T: clap::Parser> ParseExt for T {}