Skip to content

Commit b5dda6b

Browse files
refactor(mkdir): Simplify mode argument parsing
1 parent fb4d611 commit b5dda6b

File tree

1 file changed

+9
-46
lines changed

1 file changed

+9
-46
lines changed

src/uu/mkdir/src/mkdir.rs

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ pub struct Config<'a> {
5050
}
5151

5252
#[cfg(windows)]
53-
fn get_mode(_matches: &ArgMatches, _mode_had_minus_prefix: bool) -> Result<u32, String> {
53+
fn get_mode(_matches: &ArgMatches) -> Result<u32, String> {
5454
Ok(DEFAULT_PERM)
5555
}
5656

5757
#[cfg(not(windows))]
58-
fn get_mode(matches: &ArgMatches, mode_had_minus_prefix: bool) -> Result<u32, String> {
58+
fn get_mode(matches: &ArgMatches) -> Result<u32, String> {
5959
// Not tested on Windows
6060
let mut new_mode = DEFAULT_PERM;
6161

@@ -64,13 +64,7 @@ fn get_mode(matches: &ArgMatches, mode_had_minus_prefix: bool) -> Result<u32, St
6464
if mode.chars().any(|c| c.is_ascii_digit()) {
6565
new_mode = mode::parse_numeric(new_mode, m, true)?;
6666
} else {
67-
let cmode = if mode_had_minus_prefix {
68-
// clap parsing is finished, now put prefix back
69-
format!("-{mode}")
70-
} else {
71-
mode.to_string()
72-
};
73-
new_mode = mode::parse_symbolic(new_mode, &cmode, mode::get_umask(), true)?;
67+
new_mode = mode::parse_symbolic(new_mode, mode, mode::get_umask(), true)?;
7468
}
7569
}
7670
Ok(new_mode)
@@ -80,48 +74,14 @@ fn get_mode(matches: &ArgMatches, mode_had_minus_prefix: bool) -> Result<u32, St
8074
}
8175
}
8276

83-
#[cfg(windows)]
84-
fn strip_minus_from_mode(_args: &mut [OsString]) -> UResult<bool> {
85-
Ok(false)
86-
}
87-
88-
// Iterate 'args' and delete the first occurrence
89-
// of a prefix '-' if it's associated with MODE
90-
// e.g. "chmod -v -xw -R FILE" -> "chmod -v xw -R FILE"
91-
#[cfg(not(windows))]
92-
fn strip_minus_from_mode(args: &mut Vec<OsString>) -> UResult<bool> {
93-
for arg in args {
94-
if arg == "--" {
95-
break;
96-
}
97-
let bytes = uucore::os_str_as_bytes(arg)?;
98-
if let Some(b'-') = bytes.first() {
99-
if let Some(
100-
b'r' | b'w' | b'x' | b'X' | b's' | b't' | b'u' | b'g' | b'o' | b'0'..=b'7',
101-
) = bytes.get(1)
102-
{
103-
*arg = uucore::os_str_from_bytes(&bytes[1..])?.into_owned();
104-
return Ok(true);
105-
}
106-
}
107-
}
108-
Ok(false)
109-
}
110-
11177
#[uucore::main]
11278
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
113-
let mut args: Vec<OsString> = args.collect();
114-
115-
// Before we can parse 'args' with clap (and previously getopts),
116-
// a possible MODE prefix '-' needs to be removed (e.g. "chmod -x FILE").
117-
let mode_had_minus_prefix = strip_minus_from_mode(&mut args)?;
118-
11979
// Linux-specific options, not implemented
12080
// opts.optflag("Z", "context", "set SELinux security context" +
12181
// " of each created directory to CTX"),
12282
let matches = uu_app()
12383
.after_help(get_message("mkdir-after-help"))
124-
.try_get_matches_from(args)?;
84+
.try_get_matches_from(args)?; // Directly use 'args'
12585

12686
let dirs = matches
12787
.get_many::<OsString>(options::DIRS)
@@ -133,7 +93,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
13393
let set_selinux_context = matches.get_flag(options::SELINUX);
13494
let context = matches.get_one::<String>(options::CONTEXT);
13595

136-
match get_mode(&matches, mode_had_minus_prefix) {
96+
// Corrected call to get_mode
97+
match get_mode(&matches) {
13798
Ok(mode) => {
13899
let config = Config {
139100
recursive,
@@ -158,7 +119,9 @@ pub fn uu_app() -> Command {
158119
Arg::new(options::MODE)
159120
.short('m')
160121
.long(options::MODE)
161-
.help(get_message("mkdir-help-mode")),
122+
.help(get_message("mkdir-help-mode"))
123+
.allow_hyphen_values(true)
124+
.num_args(1),
162125
)
163126
.arg(
164127
Arg::new(options::PARENTS)

0 commit comments

Comments
 (0)