Skip to content

Commit dddbc17

Browse files
authored
Merge pull request #7017 from sylvestre/chmod-L
ch*: Remove duplicate declarations + start to plug it to chmod too
2 parents 438c3c9 + 2969421 commit dddbc17

4 files changed

Lines changed: 56 additions & 77 deletions

File tree

src/uu/chgrp/src/chgrp.rs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn uu_app() -> Command {
7373
Arg::new(options::HELP)
7474
.long(options::HELP)
7575
.help("Print help information.")
76-
.action(ArgAction::Help)
76+
.action(ArgAction::Help),
7777
)
7878
.arg(
7979
Arg::new(options::verbosity::CHANGES)
@@ -101,20 +101,6 @@ pub fn uu_app() -> Command {
101101
.help("output a diagnostic for every file processed")
102102
.action(ArgAction::SetTrue),
103103
)
104-
.arg(
105-
Arg::new(options::dereference::DEREFERENCE)
106-
.long(options::dereference::DEREFERENCE)
107-
.action(ArgAction::SetTrue),
108-
)
109-
.arg(
110-
Arg::new(options::dereference::NO_DEREFERENCE)
111-
.short('h')
112-
.long(options::dereference::NO_DEREFERENCE)
113-
.help(
114-
"affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)",
115-
)
116-
.action(ArgAction::SetTrue),
117-
)
118104
.arg(
119105
Arg::new(options::preserve_root::PRESERVE)
120106
.long(options::preserve_root::PRESERVE)
@@ -141,23 +127,6 @@ pub fn uu_app() -> Command {
141127
.help("operate on files and directories recursively")
142128
.action(ArgAction::SetTrue),
143129
)
144-
.arg(
145-
Arg::new(options::traverse::TRAVERSE)
146-
.short(options::traverse::TRAVERSE.chars().next().unwrap())
147-
.help("if a command line argument is a symbolic link to a directory, traverse it")
148-
.action(ArgAction::SetTrue),
149-
)
150-
.arg(
151-
Arg::new(options::traverse::NO_TRAVERSE)
152-
.short(options::traverse::NO_TRAVERSE.chars().next().unwrap())
153-
.help("do not traverse any symbolic links (default)")
154-
.overrides_with_all([options::traverse::TRAVERSE, options::traverse::EVERY])
155-
.action(ArgAction::SetTrue),
156-
)
157-
.arg(
158-
Arg::new(options::traverse::EVERY)
159-
.short(options::traverse::EVERY.chars().next().unwrap())
160-
.help("traverse every symbolic link to a directory encountered")
161-
.action(ArgAction::SetTrue),
162-
)
130+
// Add common arguments with chgrp, chown & chmod
131+
.args(uucore::perms::common_args())
163132
}

src/uu/chmod/src/chmod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const USAGE: &str = help_usage!("chmod.md");
2323
const LONG_USAGE: &str = help_section!("after help", "chmod.md");
2424

2525
mod options {
26+
pub const HELP: &str = "help";
2627
pub const CHANGES: &str = "changes";
2728
pub const QUIET: &str = "quiet"; // visible_alias("silent")
2829
pub const VERBOSE: &str = "verbose";
@@ -158,6 +159,13 @@ pub fn uu_app() -> Command {
158159
.args_override_self(true)
159160
.infer_long_args(true)
160161
.no_binary_name(true)
162+
.disable_help_flag(true)
163+
.arg(
164+
Arg::new(options::HELP)
165+
.long(options::HELP)
166+
.help("Print help information.")
167+
.action(ArgAction::Help),
168+
)
161169
.arg(
162170
Arg::new(options::CHANGES)
163171
.long(options::CHANGES)
@@ -206,16 +214,19 @@ pub fn uu_app() -> Command {
206214
.help("use RFILE's mode instead of MODE values"),
207215
)
208216
.arg(
209-
Arg::new(options::MODE).required_unless_present(options::REFERENCE), // It would be nice if clap could parse with delimiter, e.g. "g-x,u+x",
210-
// however .multiple_occurrences(true) cannot be used here because FILE already needs that.
211-
// Only one positional argument with .multiple_occurrences(true) set is allowed per command
217+
Arg::new(options::MODE).required_unless_present(options::REFERENCE),
218+
// It would be nice if clap could parse with delimiter, e.g. "g-x,u+x",
219+
// however .multiple_occurrences(true) cannot be used here because FILE already needs that.
220+
// Only one positional argument with .multiple_occurrences(true) set is allowed per command
212221
)
213222
.arg(
214223
Arg::new(options::FILE)
215224
.required_unless_present(options::MODE)
216225
.action(ArgAction::Append)
217226
.value_hint(clap::ValueHint::AnyPath),
218227
)
228+
// Add common arguments with chgrp, chown & chmod
229+
.args(uucore::perms::common_args())
219230
}
220231

221232
struct Chmoder {

src/uu/chown/src/chown.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,6 @@ pub fn uu_app() -> Command {
9696
.help("like verbose but report only when a change is made")
9797
.action(ArgAction::SetTrue),
9898
)
99-
.arg(
100-
Arg::new(options::dereference::DEREFERENCE)
101-
.long(options::dereference::DEREFERENCE)
102-
.help(
103-
"affect the referent of each symbolic link (this is the default), \
104-
rather than the symbolic link itself",
105-
)
106-
.action(ArgAction::SetTrue),
107-
)
108-
.arg(
109-
Arg::new(options::dereference::NO_DEREFERENCE)
110-
.short('h')
111-
.long(options::dereference::NO_DEREFERENCE)
112-
.help(
113-
"affect symbolic links instead of any referenced file \
114-
(useful only on systems that can change the ownership of a symlink)",
115-
)
116-
.action(ArgAction::SetTrue),
117-
)
11899
.arg(
119100
Arg::new(options::FROM)
120101
.long(options::FROM)
@@ -165,34 +146,15 @@ pub fn uu_app() -> Command {
165146
.long(options::verbosity::SILENT)
166147
.action(ArgAction::SetTrue),
167148
)
168-
.arg(
169-
Arg::new(options::traverse::TRAVERSE)
170-
.short(options::traverse::TRAVERSE.chars().next().unwrap())
171-
.help("if a command line argument is a symbolic link to a directory, traverse it")
172-
.overrides_with_all([options::traverse::EVERY, options::traverse::NO_TRAVERSE])
173-
.action(ArgAction::SetTrue),
174-
)
175-
.arg(
176-
Arg::new(options::traverse::EVERY)
177-
.short(options::traverse::EVERY.chars().next().unwrap())
178-
.help("traverse every symbolic link to a directory encountered")
179-
.overrides_with_all([options::traverse::TRAVERSE, options::traverse::NO_TRAVERSE])
180-
.action(ArgAction::SetTrue),
181-
)
182-
.arg(
183-
Arg::new(options::traverse::NO_TRAVERSE)
184-
.short(options::traverse::NO_TRAVERSE.chars().next().unwrap())
185-
.help("do not traverse any symbolic links (default)")
186-
.overrides_with_all([options::traverse::TRAVERSE, options::traverse::EVERY])
187-
.action(ArgAction::SetTrue),
188-
)
189149
.arg(
190150
Arg::new(options::verbosity::VERBOSE)
191151
.long(options::verbosity::VERBOSE)
192152
.short('v')
193153
.help("output a diagnostic for every file processed")
194154
.action(ArgAction::SetTrue),
195155
)
156+
// Add common arguments with chgrp, chown & chmod
157+
.args(uucore::perms::common_args())
196158
}
197159

198160
/// Parses the user string to extract the UID.

src/uucore/src/lib/features/perms.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use crate::features::entries;
1313
use crate::show_error;
1414
use clap::{Arg, ArgMatches, Command};
1515
use libc::{gid_t, uid_t};
16+
use options::traverse;
1617
use walkdir::WalkDir;
1718

1819
use std::io::Error as IOError;
@@ -33,6 +34,7 @@ pub enum VerbosityLevel {
3334
Verbose,
3435
Normal,
3536
}
37+
3638
#[derive(PartialEq, Eq, Clone, Debug)]
3739
pub struct Verbosity {
3840
pub groups_only: bool,
@@ -634,6 +636,41 @@ pub fn chown_base(
634636
executor.exec()
635637
}
636638

639+
pub fn common_args() -> Vec<Arg> {
640+
vec![
641+
Arg::new(traverse::TRAVERSE)
642+
.short(traverse::TRAVERSE.chars().next().unwrap())
643+
.help("if a command line argument is a symbolic link to a directory, traverse it")
644+
.overrides_with_all([traverse::EVERY, traverse::NO_TRAVERSE])
645+
.action(clap::ArgAction::SetTrue),
646+
Arg::new(traverse::EVERY)
647+
.short(traverse::EVERY.chars().next().unwrap())
648+
.help("traverse every symbolic link to a directory encountered")
649+
.overrides_with_all([traverse::TRAVERSE, traverse::NO_TRAVERSE])
650+
.action(clap::ArgAction::SetTrue),
651+
Arg::new(traverse::NO_TRAVERSE)
652+
.short(traverse::NO_TRAVERSE.chars().next().unwrap())
653+
.help("do not traverse any symbolic links (default)")
654+
.overrides_with_all([traverse::TRAVERSE, traverse::EVERY])
655+
.action(clap::ArgAction::SetTrue),
656+
Arg::new(options::dereference::DEREFERENCE)
657+
.long(options::dereference::DEREFERENCE)
658+
.help(
659+
"affect the referent of each symbolic link (this is the default), \
660+
rather than the symbolic link itself",
661+
)
662+
.action(clap::ArgAction::SetTrue),
663+
Arg::new(options::dereference::NO_DEREFERENCE)
664+
.short('h')
665+
.long(options::dereference::NO_DEREFERENCE)
666+
.help(
667+
"affect symbolic links instead of any referenced file \
668+
(useful only on systems that can change the ownership of a symlink)",
669+
)
670+
.action(clap::ArgAction::SetTrue),
671+
]
672+
}
673+
637674
#[cfg(test)]
638675
mod tests {
639676
// Note this useful idiom: importing names from outer (for mod tests) scope.

0 commit comments

Comments
 (0)