|
| 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, ValueHint}; |
| 7 | +use std::env; |
| 8 | +use std::ffi::OsString; |
| 9 | +pub use uucore::{format_usage, translate}; |
| 10 | + |
| 11 | +pub const ARG_INPUT: &str = "input"; |
| 12 | +pub const ARG_PREFIX: &str = "prefix"; |
| 13 | + |
| 14 | +pub mod options { |
| 15 | + pub const BYTES: &str = "bytes"; |
| 16 | + pub const LINE_BYTES: &str = "line-bytes"; |
| 17 | + pub const LINES: &str = "lines"; |
| 18 | + pub const ADDITIONAL_SUFFIX: &str = "additional-suffix"; |
| 19 | + pub const FILTER: &str = "filter"; |
| 20 | + pub const NUMBER: &str = "number"; |
| 21 | + pub const NUMERIC_SUFFIXES: &str = "numeric-suffixes"; |
| 22 | + pub const NUMERIC_SUFFIXES_SHORT: &str = "-d"; |
| 23 | + pub const HEX_SUFFIXES: &str = "hex-suffixes"; |
| 24 | + pub const HEX_SUFFIXES_SHORT: &str = "-x"; |
| 25 | + pub const SUFFIX_LENGTH: &str = "suffix-length"; |
| 26 | + pub const VERBOSE: &str = "verbose"; |
| 27 | + pub const SEPARATOR: &str = "separator"; |
| 28 | + pub const ELIDE_EMPTY_FILES: &str = "elide-empty-files"; |
| 29 | + pub const IO_BLKSIZE: &str = "-io-blksize"; |
| 30 | +} |
| 31 | + |
| 32 | +pub fn uu_app() -> Command { |
| 33 | + Command::new("split") |
| 34 | + .version(uucore::crate_version!()) |
| 35 | + .help_template(uucore::localized_help_template(uucore::util_name())) |
| 36 | + .about(translate!("split-about")) |
| 37 | + .after_help(translate!("split-after-help")) |
| 38 | + .override_usage(format_usage(&translate!("split-usage"))) |
| 39 | + .infer_long_args(true) |
| 40 | + // strategy (mutually exclusive) |
| 41 | + .arg( |
| 42 | + Arg::new(options::BYTES) |
| 43 | + .short('b') |
| 44 | + .long(options::BYTES) |
| 45 | + .allow_hyphen_values(true) |
| 46 | + .value_name("SIZE") |
| 47 | + .help(translate!("split-help-bytes")), |
| 48 | + ) |
| 49 | + .arg( |
| 50 | + Arg::new(options::LINE_BYTES) |
| 51 | + .short('C') |
| 52 | + .long(options::LINE_BYTES) |
| 53 | + .allow_hyphen_values(true) |
| 54 | + .value_name("SIZE") |
| 55 | + .help(translate!("split-help-line-bytes")), |
| 56 | + ) |
| 57 | + .arg( |
| 58 | + Arg::new(options::LINES) |
| 59 | + .short('l') |
| 60 | + .long(options::LINES) |
| 61 | + .allow_hyphen_values(true) |
| 62 | + .value_name("NUMBER") |
| 63 | + .default_value("1000") |
| 64 | + .help(translate!("split-help-lines")), |
| 65 | + ) |
| 66 | + .arg( |
| 67 | + Arg::new(options::NUMBER) |
| 68 | + .short('n') |
| 69 | + .long(options::NUMBER) |
| 70 | + .allow_hyphen_values(true) |
| 71 | + .value_name("CHUNKS") |
| 72 | + .help(translate!("split-help-number")), |
| 73 | + ) |
| 74 | + // rest of the arguments |
| 75 | + .arg( |
| 76 | + Arg::new(options::ADDITIONAL_SUFFIX) |
| 77 | + .long(options::ADDITIONAL_SUFFIX) |
| 78 | + .allow_hyphen_values(true) |
| 79 | + .value_name("SUFFIX") |
| 80 | + .default_value("") |
| 81 | + .value_parser(clap::value_parser!(OsString)) |
| 82 | + .help(translate!("split-help-additional-suffix")), |
| 83 | + ) |
| 84 | + .arg( |
| 85 | + Arg::new(options::FILTER) |
| 86 | + .long(options::FILTER) |
| 87 | + .allow_hyphen_values(true) |
| 88 | + .value_name("COMMAND") |
| 89 | + .value_hint(ValueHint::CommandName) |
| 90 | + .help(translate!("split-help-filter")), |
| 91 | + ) |
| 92 | + .arg( |
| 93 | + Arg::new(options::ELIDE_EMPTY_FILES) |
| 94 | + .long(options::ELIDE_EMPTY_FILES) |
| 95 | + .short('e') |
| 96 | + .help(translate!("split-help-elide-empty-files")) |
| 97 | + .action(ArgAction::SetTrue), |
| 98 | + ) |
| 99 | + .arg( |
| 100 | + Arg::new(options::NUMERIC_SUFFIXES_SHORT) |
| 101 | + .short('d') |
| 102 | + .action(ArgAction::SetTrue) |
| 103 | + .overrides_with_all([ |
| 104 | + options::NUMERIC_SUFFIXES, |
| 105 | + options::NUMERIC_SUFFIXES_SHORT, |
| 106 | + options::HEX_SUFFIXES, |
| 107 | + options::HEX_SUFFIXES_SHORT, |
| 108 | + ]) |
| 109 | + .help(translate!("split-help-numeric-suffixes-short")), |
| 110 | + ) |
| 111 | + .arg( |
| 112 | + Arg::new(options::NUMERIC_SUFFIXES) |
| 113 | + .long(options::NUMERIC_SUFFIXES) |
| 114 | + .require_equals(true) |
| 115 | + .num_args(0..=1) |
| 116 | + .overrides_with_all([ |
| 117 | + options::NUMERIC_SUFFIXES, |
| 118 | + options::NUMERIC_SUFFIXES_SHORT, |
| 119 | + options::HEX_SUFFIXES, |
| 120 | + options::HEX_SUFFIXES_SHORT, |
| 121 | + ]) |
| 122 | + .value_name("FROM") |
| 123 | + .help(translate!("split-help-numeric-suffixes")), |
| 124 | + ) |
| 125 | + .arg( |
| 126 | + Arg::new(options::HEX_SUFFIXES_SHORT) |
| 127 | + .short('x') |
| 128 | + .action(ArgAction::SetTrue) |
| 129 | + .overrides_with_all([ |
| 130 | + options::NUMERIC_SUFFIXES, |
| 131 | + options::NUMERIC_SUFFIXES_SHORT, |
| 132 | + options::HEX_SUFFIXES, |
| 133 | + options::HEX_SUFFIXES_SHORT, |
| 134 | + ]) |
| 135 | + .help(translate!("split-help-hex-suffixes-short")), |
| 136 | + ) |
| 137 | + .arg( |
| 138 | + Arg::new(options::HEX_SUFFIXES) |
| 139 | + .long(options::HEX_SUFFIXES) |
| 140 | + .require_equals(true) |
| 141 | + .num_args(0..=1) |
| 142 | + .overrides_with_all([ |
| 143 | + options::NUMERIC_SUFFIXES, |
| 144 | + options::NUMERIC_SUFFIXES_SHORT, |
| 145 | + options::HEX_SUFFIXES, |
| 146 | + options::HEX_SUFFIXES_SHORT, |
| 147 | + ]) |
| 148 | + .value_name("FROM") |
| 149 | + .help(translate!("split-help-hex-suffixes")), |
| 150 | + ) |
| 151 | + .arg( |
| 152 | + Arg::new(options::SUFFIX_LENGTH) |
| 153 | + .short('a') |
| 154 | + .long(options::SUFFIX_LENGTH) |
| 155 | + .allow_hyphen_values(true) |
| 156 | + .value_name("N") |
| 157 | + .help(translate!("split-help-suffix-length")), |
| 158 | + ) |
| 159 | + .arg( |
| 160 | + Arg::new(options::VERBOSE) |
| 161 | + .long(options::VERBOSE) |
| 162 | + .help(translate!("split-help-verbose")) |
| 163 | + .action(ArgAction::SetTrue), |
| 164 | + ) |
| 165 | + .arg( |
| 166 | + Arg::new(options::SEPARATOR) |
| 167 | + .short('t') |
| 168 | + .long(options::SEPARATOR) |
| 169 | + .allow_hyphen_values(true) |
| 170 | + .value_name("SEP") |
| 171 | + .action(ArgAction::Append) |
| 172 | + .help(translate!("split-help-separator")), |
| 173 | + ) |
| 174 | + .arg( |
| 175 | + Arg::new(options::IO_BLKSIZE) |
| 176 | + .long("io-blksize") |
| 177 | + .alias(options::IO_BLKSIZE) |
| 178 | + .hide(true), |
| 179 | + ) |
| 180 | + .arg( |
| 181 | + Arg::new(ARG_INPUT) |
| 182 | + .default_value("-") |
| 183 | + .value_hint(ValueHint::FilePath) |
| 184 | + .value_parser(clap::value_parser!(OsString)), |
| 185 | + ) |
| 186 | + .arg( |
| 187 | + Arg::new(ARG_PREFIX) |
| 188 | + .default_value("x") |
| 189 | + .value_parser(clap::value_parser!(OsString)), |
| 190 | + ) |
| 191 | +} |
0 commit comments