|
| 1 | +use std::{collections::VecDeque, ffi::OsString, fmt, sync::OnceLock}; |
| 2 | + |
| 3 | +#[derive(Debug)] |
| 4 | +pub enum ArgParseError { |
| 5 | + StdoutMessage(String), |
| 6 | + StderrMessage(String), |
| 7 | +} |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +pub struct ZipCli { |
| 11 | + pub verbose: bool, |
| 12 | + pub command: ZipCommand, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Debug)] |
| 16 | +enum SubcommandName { |
| 17 | + Compress, |
| 18 | + Info, |
| 19 | + Extract, |
| 20 | +} |
| 21 | + |
| 22 | +static PARSED_EXE_NAME: OnceLock<String> = OnceLock::new(); |
| 23 | + |
| 24 | +impl ZipCli { |
| 25 | + const VERSION: &'static str = env!("CARGO_PKG_VERSION"); |
| 26 | + const DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION"); |
| 27 | + |
| 28 | + pub const INTERNAL_ERROR_EXIT_CODE: i32 = 3; |
| 29 | + pub const ARGV_PARSE_FAILED_EXIT_CODE: i32 = 2; |
| 30 | + pub const NON_FAILURE_EXIT_CODE: i32 = 0; |
| 31 | + |
| 32 | + pub fn binary_name() -> &'static str { |
| 33 | + PARSED_EXE_NAME.get().expect("binary name was not set yet") |
| 34 | + } |
| 35 | + |
| 36 | + fn generate_version_text() -> String { |
| 37 | + format!("{} {}\n", Self::binary_name(), Self::VERSION) |
| 38 | + } |
| 39 | + |
| 40 | + fn generate_usage_line() -> String { |
| 41 | + format!("Usage: {} [OPTIONS] <COMMAND>", Self::binary_name()) |
| 42 | + } |
| 43 | + |
| 44 | + fn generate_full_help_text() -> String { |
| 45 | + format!( |
| 46 | + "\ |
| 47 | +{} |
| 48 | +
|
| 49 | +{} |
| 50 | +
|
| 51 | +Commands: |
| 52 | + {}{}{} |
| 53 | + {}{}{} |
| 54 | + {}{}{} |
| 55 | +
|
| 56 | +Options: |
| 57 | + -v, --verbose Write information logs to stderr |
| 58 | + -h, --help Print help |
| 59 | + -V, --version Print version |
| 60 | +", |
| 61 | + Self::DESCRIPTION, |
| 62 | + Self::generate_usage_line(), |
| 63 | + compress::Compress::COMMAND_NAME, |
| 64 | + compress::Compress::COMMAND_TABS, |
| 65 | + compress::Compress::COMMAND_DESCRIPTION, |
| 66 | + info::Info::COMMAND_NAME, |
| 67 | + info::Info::COMMAND_TABS, |
| 68 | + info::Info::COMMAND_DESCRIPTION, |
| 69 | + extract::Extract::COMMAND_NAME, |
| 70 | + extract::Extract::COMMAND_TABS, |
| 71 | + extract::Extract::COMMAND_DESCRIPTION, |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + fn generate_brief_help_text(context: &str) -> String { |
| 76 | + format!( |
| 77 | + "\ |
| 78 | +error: {context} |
| 79 | +
|
| 80 | +{} |
| 81 | +
|
| 82 | +For more information, try '--help'. |
| 83 | +", |
| 84 | + Self::generate_usage_line() |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + fn parse_up_to_subcommand_name( |
| 89 | + argv: &mut VecDeque<OsString>, |
| 90 | + ) -> Result<(bool, SubcommandName), ArgParseError> { |
| 91 | + let mut verbose: bool = false; |
| 92 | + let mut subcommand_name: Option<SubcommandName> = None; |
| 93 | + while subcommand_name.is_none() { |
| 94 | + match argv.pop_front() { |
| 95 | + None => { |
| 96 | + let help_text = Self::generate_full_help_text(); |
| 97 | + return Err(ArgParseError::StderrMessage(help_text)); |
| 98 | + } |
| 99 | + Some(arg) => match arg.as_encoded_bytes() { |
| 100 | + b"-v" | b"--verbose" => verbose = true, |
| 101 | + b"-V" | b"--version" => { |
| 102 | + let version_text = Self::generate_version_text(); |
| 103 | + return Err(ArgParseError::StdoutMessage(version_text)); |
| 104 | + } |
| 105 | + b"-h" | b"--help" => { |
| 106 | + let help_text = Self::generate_full_help_text(); |
| 107 | + return Err(ArgParseError::StdoutMessage(help_text)); |
| 108 | + } |
| 109 | + b"compress" => subcommand_name = Some(SubcommandName::Compress), |
| 110 | + b"info" => subcommand_name = Some(SubcommandName::Info), |
| 111 | + b"extract" => subcommand_name = Some(SubcommandName::Extract), |
| 112 | + arg_bytes => { |
| 113 | + let context = if arg_bytes.starts_with(b"-") { |
| 114 | + format!("unrecognized global flag {arg:?}") |
| 115 | + } else { |
| 116 | + format!("unrecognized subcommand name {arg:?}") |
| 117 | + }; |
| 118 | + let help_text = Self::generate_brief_help_text(&context); |
| 119 | + return Err(ArgParseError::StderrMessage(help_text)); |
| 120 | + } |
| 121 | + }, |
| 122 | + } |
| 123 | + } |
| 124 | + Ok((verbose, subcommand_name.unwrap())) |
| 125 | + } |
| 126 | + |
| 127 | + pub fn parse_argv(argv: impl IntoIterator<Item = OsString>) -> Result<Self, ArgParseError> { |
| 128 | + let mut argv: VecDeque<OsString> = argv.into_iter().collect(); |
| 129 | + let exe_name: String = argv |
| 130 | + .pop_front() |
| 131 | + .expect("exe name not on command line") |
| 132 | + .into_string() |
| 133 | + .expect("exe name not valid unicode"); |
| 134 | + PARSED_EXE_NAME |
| 135 | + .set(exe_name) |
| 136 | + .expect("exe name already written"); |
| 137 | + let (verbose, subcommand_name) = Self::parse_up_to_subcommand_name(&mut argv)?; |
| 138 | + let command = match subcommand_name { |
| 139 | + SubcommandName::Info => ZipCommand::Info(info::Info::parse_argv(argv)?), |
| 140 | + SubcommandName::Extract => ZipCommand::Extract(extract::Extract::parse_argv(argv)?), |
| 141 | + SubcommandName::Compress => ZipCommand::Compress(compress::Compress::parse_argv(argv)?), |
| 142 | + }; |
| 143 | + Ok(Self { verbose, command }) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +#[derive(Debug)] |
| 148 | +pub enum ZipCommand { |
| 149 | + Compress(compress::Compress), |
| 150 | + Info(info::Info), |
| 151 | + Extract(extract::Extract), |
| 152 | +} |
| 153 | + |
| 154 | +pub trait CommandFormat: fmt::Debug { |
| 155 | + const COMMAND_NAME: &'static str; |
| 156 | + const COMMAND_TABS: &'static str; |
| 157 | + const COMMAND_DESCRIPTION: &'static str; |
| 158 | + |
| 159 | + const USAGE_LINE: &'static str; |
| 160 | + |
| 161 | + fn generate_usage_line() -> String { |
| 162 | + format!( |
| 163 | + "Usage: {} {} {}", |
| 164 | + ZipCli::binary_name(), |
| 165 | + Self::COMMAND_NAME, |
| 166 | + Self::USAGE_LINE, |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | + fn generate_help() -> String; |
| 171 | + |
| 172 | + fn generate_full_help_text() -> String { |
| 173 | + format!( |
| 174 | + "\ |
| 175 | +{} |
| 176 | +
|
| 177 | +{} |
| 178 | +{}", |
| 179 | + Self::COMMAND_DESCRIPTION, |
| 180 | + Self::generate_usage_line(), |
| 181 | + Self::generate_help(), |
| 182 | + ) |
| 183 | + } |
| 184 | + |
| 185 | + fn generate_brief_help_text(context: &str) -> String { |
| 186 | + format!( |
| 187 | + "\ |
| 188 | +error: {context} |
| 189 | +
|
| 190 | +{} |
| 191 | +", |
| 192 | + Self::generate_usage_line() |
| 193 | + ) |
| 194 | + } |
| 195 | + |
| 196 | + fn exit_arg_invalid(context: &str) -> ArgParseError { |
| 197 | + let message = Self::generate_brief_help_text(context); |
| 198 | + ArgParseError::StderrMessage(message) |
| 199 | + } |
| 200 | + |
| 201 | + fn parse_argv(argv: VecDeque<OsString>) -> Result<Self, ArgParseError> |
| 202 | + where |
| 203 | + Self: Sized; |
| 204 | +} |
| 205 | + |
| 206 | +pub mod compress; |
| 207 | +pub mod extract; |
| 208 | +pub mod info; |
0 commit comments