Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 191 additions & 0 deletions src/uu/split/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use clap::{Arg, ArgAction, Command, ValueHint};
use std::env;
use std::ffi::OsString;
pub use uucore::{format_usage, translate};

pub const ARG_INPUT: &str = "input";
pub const ARG_PREFIX: &str = "prefix";

pub mod options {
pub const BYTES: &str = "bytes";
pub const LINE_BYTES: &str = "line-bytes";
pub const LINES: &str = "lines";
pub const ADDITIONAL_SUFFIX: &str = "additional-suffix";
pub const FILTER: &str = "filter";
pub const NUMBER: &str = "number";
pub const NUMERIC_SUFFIXES: &str = "numeric-suffixes";
pub const NUMERIC_SUFFIXES_SHORT: &str = "-d";
pub const HEX_SUFFIXES: &str = "hex-suffixes";
pub const HEX_SUFFIXES_SHORT: &str = "-x";
pub const SUFFIX_LENGTH: &str = "suffix-length";
pub const VERBOSE: &str = "verbose";
pub const SEPARATOR: &str = "separator";
pub const ELIDE_EMPTY_FILES: &str = "elide-empty-files";
pub const IO_BLKSIZE: &str = "-io-blksize";
}

pub fn uu_app() -> Command {
Command::new("split")
.version(uucore::crate_version!())
.help_template(uucore::localized_help_template(uucore::util_name()))
.about(translate!("split-about"))
.after_help(translate!("split-after-help"))
.override_usage(format_usage(&translate!("split-usage")))
.infer_long_args(true)
// strategy (mutually exclusive)
.arg(
Arg::new(options::BYTES)
.short('b')
.long(options::BYTES)
.allow_hyphen_values(true)
.value_name("SIZE")
.help(translate!("split-help-bytes")),
)
.arg(
Arg::new(options::LINE_BYTES)
.short('C')
.long(options::LINE_BYTES)
.allow_hyphen_values(true)
.value_name("SIZE")
.help(translate!("split-help-line-bytes")),
)
.arg(
Arg::new(options::LINES)
.short('l')
.long(options::LINES)
.allow_hyphen_values(true)
.value_name("NUMBER")
.default_value("1000")
.help(translate!("split-help-lines")),
)
.arg(
Arg::new(options::NUMBER)
.short('n')
.long(options::NUMBER)
.allow_hyphen_values(true)
.value_name("CHUNKS")
.help(translate!("split-help-number")),
)
// rest of the arguments
.arg(
Arg::new(options::ADDITIONAL_SUFFIX)
.long(options::ADDITIONAL_SUFFIX)
.allow_hyphen_values(true)
.value_name("SUFFIX")
.default_value("")
.value_parser(clap::value_parser!(OsString))
.help(translate!("split-help-additional-suffix")),
)
.arg(
Arg::new(options::FILTER)
.long(options::FILTER)
.allow_hyphen_values(true)
.value_name("COMMAND")
.value_hint(ValueHint::CommandName)
.help(translate!("split-help-filter")),
)
.arg(
Arg::new(options::ELIDE_EMPTY_FILES)
.long(options::ELIDE_EMPTY_FILES)
.short('e')
.help(translate!("split-help-elide-empty-files"))
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::NUMERIC_SUFFIXES_SHORT)
.short('d')
.action(ArgAction::SetTrue)
.overrides_with_all([
options::NUMERIC_SUFFIXES,
options::NUMERIC_SUFFIXES_SHORT,
options::HEX_SUFFIXES,
options::HEX_SUFFIXES_SHORT,
])
.help(translate!("split-help-numeric-suffixes-short")),
)
.arg(
Arg::new(options::NUMERIC_SUFFIXES)
.long(options::NUMERIC_SUFFIXES)
.require_equals(true)
.num_args(0..=1)
.overrides_with_all([
options::NUMERIC_SUFFIXES,
options::NUMERIC_SUFFIXES_SHORT,
options::HEX_SUFFIXES,
options::HEX_SUFFIXES_SHORT,
])
.value_name("FROM")
.help(translate!("split-help-numeric-suffixes")),
)
.arg(
Arg::new(options::HEX_SUFFIXES_SHORT)
.short('x')
.action(ArgAction::SetTrue)
.overrides_with_all([
options::NUMERIC_SUFFIXES,
options::NUMERIC_SUFFIXES_SHORT,
options::HEX_SUFFIXES,
options::HEX_SUFFIXES_SHORT,
])
.help(translate!("split-help-hex-suffixes-short")),
)
.arg(
Arg::new(options::HEX_SUFFIXES)
.long(options::HEX_SUFFIXES)
.require_equals(true)
.num_args(0..=1)
.overrides_with_all([
options::NUMERIC_SUFFIXES,
options::NUMERIC_SUFFIXES_SHORT,
options::HEX_SUFFIXES,
options::HEX_SUFFIXES_SHORT,
])
.value_name("FROM")
.help(translate!("split-help-hex-suffixes")),
)
.arg(
Arg::new(options::SUFFIX_LENGTH)
.short('a')
.long(options::SUFFIX_LENGTH)
.allow_hyphen_values(true)
.value_name("N")
.help(translate!("split-help-suffix-length")),
)
.arg(
Arg::new(options::VERBOSE)
.long(options::VERBOSE)
.help(translate!("split-help-verbose"))
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::SEPARATOR)
.short('t')
.long(options::SEPARATOR)
.allow_hyphen_values(true)
.value_name("SEP")
.action(ArgAction::Append)
.help(translate!("split-help-separator")),
)
.arg(
Arg::new(options::IO_BLKSIZE)
.long("io-blksize")
.alias(options::IO_BLKSIZE)
.hide(true),
)
.arg(
Arg::new(ARG_INPUT)
.default_value("-")
.value_hint(ValueHint::FilePath)
.value_parser(clap::value_parser!(OsString)),
)
.arg(
Arg::new(ARG_PREFIX)
.default_value("x")
.value_parser(clap::value_parser!(OsString)),
)
}
21 changes: 9 additions & 12 deletions src/uu/split/src/filenames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@
//! assert_eq!(it.next().unwrap(), "chunk_ac.txt");
//! ```

use crate::cli::options;
use crate::number::DynamicWidthNumber;
use crate::number::FixedWidthNumber;
use crate::number::Number;
use crate::strategy::Strategy;
use crate::{
OPT_ADDITIONAL_SUFFIX, OPT_HEX_SUFFIXES, OPT_HEX_SUFFIXES_SHORT, OPT_NUMERIC_SUFFIXES,
OPT_NUMERIC_SUFFIXES_SHORT, OPT_SUFFIX_LENGTH,
};
use clap::ArgMatches;
use std::ffi::{OsStr, OsString};
use std::path::is_separator;
Expand Down Expand Up @@ -148,15 +145,15 @@ impl Suffix {
// Since all suffixes are setup with 'overrides_with_all()' against themselves and each other,
// last one wins, all others are ignored
match (
matches.contains_id(OPT_NUMERIC_SUFFIXES),
matches.contains_id(OPT_HEX_SUFFIXES),
matches.get_flag(OPT_NUMERIC_SUFFIXES_SHORT),
matches.get_flag(OPT_HEX_SUFFIXES_SHORT),
matches.contains_id(options::NUMERIC_SUFFIXES),
matches.contains_id(options::HEX_SUFFIXES),
matches.get_flag(options::NUMERIC_SUFFIXES_SHORT),
matches.get_flag(options::HEX_SUFFIXES_SHORT),
) {
(true, _, _, _) => {
stype = SuffixType::Decimal;
// if option was specified, but without value - this will return None as there is no default value
if let Some(opt) = matches.get_one::<String>(OPT_NUMERIC_SUFFIXES) {
if let Some(opt) = matches.get_one::<String>(options::NUMERIC_SUFFIXES) {
start = opt
.parse::<usize>()
.map_err(|_| SuffixError::NotParsable(opt.to_owned()))?;
Expand All @@ -166,7 +163,7 @@ impl Suffix {
(_, true, _, _) => {
stype = SuffixType::Hexadecimal;
// if option was specified, but without value - this will return None as there is no default value
if let Some(opt) = matches.get_one::<String>(OPT_HEX_SUFFIXES) {
if let Some(opt) = matches.get_one::<String>(options::HEX_SUFFIXES) {
start = usize::from_str_radix(opt, 16)
.map_err(|_| SuffixError::NotParsable(opt.to_owned()))?;
auto_widening = false;
Expand All @@ -179,7 +176,7 @@ impl Suffix {

// Get suffix length and a flag to indicate if it was specified with command line option
let (mut length, is_length_cmd_opt) =
if let Some(v) = matches.get_one::<String>(OPT_SUFFIX_LENGTH) {
if let Some(v) = matches.get_one::<String>(options::SUFFIX_LENGTH) {
// suffix length was specified in command line
let parsed_length = v
.parse::<usize>()
Expand Down Expand Up @@ -228,7 +225,7 @@ impl Suffix {
}

let additional = matches
.get_one::<OsString>(OPT_ADDITIONAL_SUFFIX)
.get_one::<OsString>(options::ADDITIONAL_SUFFIX)
.unwrap()
.clone();
if additional.to_string_lossy().chars().any(is_separator) {
Expand Down
Loading
Loading