|
| 1 | +// This file is part of the uutils coreutils package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +use clap::{Arg, ArgAction, Command}; |
| 7 | +use uucore::{checksum::SUPPORTED_ALGORITHMS, translate}; |
| 8 | + |
| 9 | +/// List of all options that can be encountered in checksum utils |
| 10 | +pub mod options { |
| 11 | + // cksum-specific |
| 12 | + pub const ALGORITHM: &str = "algorithm"; |
| 13 | + pub const DEBUG: &str = "debug"; |
| 14 | + |
| 15 | + // positional arg |
| 16 | + pub const FILE: &str = "file"; |
| 17 | + |
| 18 | + pub const UNTAGGED: &str = "untagged"; |
| 19 | + pub const TAG: &str = "tag"; |
| 20 | + pub const LENGTH: &str = "length"; |
| 21 | + pub const RAW: &str = "raw"; |
| 22 | + pub const BASE64: &str = "base64"; |
| 23 | + pub const CHECK: &str = "check"; |
| 24 | + pub const TEXT: &str = "text"; |
| 25 | + pub const BINARY: &str = "binary"; |
| 26 | + pub const ZERO: &str = "zero"; |
| 27 | + |
| 28 | + // check-specific |
| 29 | + pub const STRICT: &str = "strict"; |
| 30 | + pub const STATUS: &str = "status"; |
| 31 | + pub const WARN: &str = "warn"; |
| 32 | + pub const IGNORE_MISSING: &str = "ignore-missing"; |
| 33 | + pub const QUIET: &str = "quiet"; |
| 34 | +} |
| 35 | + |
| 36 | +/// `ChecksumCommand` is a convenience trait to more easily declare checksum |
| 37 | +/// CLI interfaces with |
| 38 | +pub trait ChecksumCommand { |
| 39 | + fn with_algo(self) -> Self; |
| 40 | + |
| 41 | + fn with_length(self) -> Self; |
| 42 | + |
| 43 | + fn with_check_and_opts(self) -> Self; |
| 44 | + |
| 45 | + fn with_binary(self) -> Self; |
| 46 | + |
| 47 | + fn with_text(self, is_default: bool) -> Self; |
| 48 | + |
| 49 | + fn with_tag(self, is_default: bool) -> Self; |
| 50 | + |
| 51 | + fn with_untagged(self) -> Self; |
| 52 | + |
| 53 | + fn with_raw(self) -> Self; |
| 54 | + |
| 55 | + fn with_base64(self) -> Self; |
| 56 | + |
| 57 | + fn with_zero(self) -> Self; |
| 58 | + |
| 59 | + fn with_debug(self) -> Self; |
| 60 | +} |
| 61 | + |
| 62 | +impl ChecksumCommand for Command { |
| 63 | + fn with_algo(self) -> Self { |
| 64 | + self.arg( |
| 65 | + Arg::new(options::ALGORITHM) |
| 66 | + .long(options::ALGORITHM) |
| 67 | + .short('a') |
| 68 | + .help(translate!("ck-common-help-algorithm")) |
| 69 | + .value_name("ALGORITHM") |
| 70 | + .value_parser(SUPPORTED_ALGORITHMS), |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + fn with_length(self) -> Self { |
| 75 | + self.arg( |
| 76 | + Arg::new(options::LENGTH) |
| 77 | + .long(options::LENGTH) |
| 78 | + .short('l') |
| 79 | + .help(translate!("ck-common-help-length")) |
| 80 | + .action(ArgAction::Set), |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + fn with_check_and_opts(self) -> Self { |
| 85 | + self.arg( |
| 86 | + Arg::new(options::CHECK) |
| 87 | + .short('c') |
| 88 | + .long(options::CHECK) |
| 89 | + .help(translate!("ck-common-help-check")) |
| 90 | + .action(ArgAction::SetTrue), |
| 91 | + ) |
| 92 | + .arg( |
| 93 | + Arg::new(options::WARN) |
| 94 | + .short('w') |
| 95 | + .long("warn") |
| 96 | + .help(translate!("ck-common-help-warn")) |
| 97 | + .action(ArgAction::SetTrue) |
| 98 | + .overrides_with_all([options::STATUS, options::QUIET]), |
| 99 | + ) |
| 100 | + .arg( |
| 101 | + Arg::new(options::STATUS) |
| 102 | + .long("status") |
| 103 | + .help(translate!("ck-common-help-status")) |
| 104 | + .action(ArgAction::SetTrue) |
| 105 | + .overrides_with_all([options::WARN, options::QUIET]), |
| 106 | + ) |
| 107 | + .arg( |
| 108 | + Arg::new(options::QUIET) |
| 109 | + .long(options::QUIET) |
| 110 | + .help(translate!("ck-common-help-quiet")) |
| 111 | + .action(ArgAction::SetTrue) |
| 112 | + .overrides_with_all([options::STATUS, options::WARN]), |
| 113 | + ) |
| 114 | + .arg( |
| 115 | + Arg::new(options::IGNORE_MISSING) |
| 116 | + .long(options::IGNORE_MISSING) |
| 117 | + .help(translate!("ck-common-help-ignore-missing")) |
| 118 | + .action(ArgAction::SetTrue), |
| 119 | + ) |
| 120 | + .arg( |
| 121 | + Arg::new(options::STRICT) |
| 122 | + .long(options::STRICT) |
| 123 | + .help(translate!("ck-common-help-strict")) |
| 124 | + .action(ArgAction::SetTrue), |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + fn with_binary(self) -> Self { |
| 129 | + self.arg( |
| 130 | + Arg::new(options::BINARY) |
| 131 | + .long(options::BINARY) |
| 132 | + .short('b') |
| 133 | + .hide(true) |
| 134 | + .action(ArgAction::SetTrue), |
| 135 | + ) |
| 136 | + } |
| 137 | + |
| 138 | + fn with_text(self, is_default: bool) -> Self { |
| 139 | + let mut arg = Arg::new(options::TEXT) |
| 140 | + .long(options::TEXT) |
| 141 | + .short('t') |
| 142 | + .action(ArgAction::SetTrue); |
| 143 | + |
| 144 | + arg = if is_default { |
| 145 | + arg.help(translate!("ck-common-help-text")) |
| 146 | + } else { |
| 147 | + arg.hide(true) |
| 148 | + }; |
| 149 | + |
| 150 | + self.arg(arg) |
| 151 | + } |
| 152 | + |
| 153 | + fn with_tag(self, default: bool) -> Self { |
| 154 | + let mut arg = Arg::new(options::TAG) |
| 155 | + .long(options::TAG) |
| 156 | + .action(ArgAction::SetTrue); |
| 157 | + |
| 158 | + arg = if default { |
| 159 | + arg.help(translate!("ck-common-help-tag-default")) |
| 160 | + } else { |
| 161 | + arg.help(translate!("ck-common-help-tag")) |
| 162 | + }; |
| 163 | + |
| 164 | + self.arg(arg) |
| 165 | + } |
| 166 | + |
| 167 | + fn with_untagged(self) -> Self { |
| 168 | + self.arg( |
| 169 | + Arg::new(options::UNTAGGED) |
| 170 | + .long(options::UNTAGGED) |
| 171 | + .help(translate!("ck-common-help-untagged")) |
| 172 | + .action(ArgAction::SetTrue), |
| 173 | + ) |
| 174 | + } |
| 175 | + |
| 176 | + fn with_raw(self) -> Self { |
| 177 | + self.arg( |
| 178 | + Arg::new(options::RAW) |
| 179 | + .long(options::RAW) |
| 180 | + .help(translate!("ck-common-help-raw")) |
| 181 | + .action(ArgAction::SetTrue), |
| 182 | + ) |
| 183 | + } |
| 184 | + |
| 185 | + fn with_base64(self) -> Self { |
| 186 | + self.arg( |
| 187 | + Arg::new(options::BASE64) |
| 188 | + .long(options::BASE64) |
| 189 | + .help(translate!("ck-common-help-base64")) |
| 190 | + .action(ArgAction::SetTrue) |
| 191 | + // Even though this could easily just override an earlier '--raw', |
| 192 | + // GNU cksum does not permit these flags to be combined: |
| 193 | + .conflicts_with(options::RAW), |
| 194 | + ) |
| 195 | + } |
| 196 | + |
| 197 | + fn with_zero(self) -> Self { |
| 198 | + self.arg( |
| 199 | + Arg::new(options::ZERO) |
| 200 | + .long(options::ZERO) |
| 201 | + .short('z') |
| 202 | + .help(translate!("ck-common-help-zero")) |
| 203 | + .action(ArgAction::SetTrue), |
| 204 | + ) |
| 205 | + } |
| 206 | + |
| 207 | + fn with_debug(self) -> Self { |
| 208 | + self.arg( |
| 209 | + Arg::new(options::DEBUG) |
| 210 | + .long(options::DEBUG) |
| 211 | + .help(translate!("ck-common-help-debug")) |
| 212 | + .action(ArgAction::SetTrue), |
| 213 | + ) |
| 214 | + } |
| 215 | +} |
0 commit comments