mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Merge pull request #3059 from jfinkels/df-options-parsing
df: create Options::from() function to contain the code for parsing ArgMatches
This commit is contained in:
commit
edce04e8ee
1 changed files with 37 additions and 60 deletions
|
@ -12,17 +12,17 @@ use uucore::error::UResult;
|
|||
use uucore::fsext::statfs_fn;
|
||||
use uucore::fsext::{read_fs_list, FsUsage, MountInfo};
|
||||
|
||||
use clap::{crate_version, App, AppSettings, Arg};
|
||||
use clap::{crate_version, App, AppSettings, Arg, ArgMatches};
|
||||
|
||||
use number_prefix::NumberPrefix;
|
||||
use std::cell::Cell;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use std::error::Error;
|
||||
#[cfg(unix)]
|
||||
use std::ffi::CString;
|
||||
use std::fmt::Display;
|
||||
use std::iter::FromIterator;
|
||||
#[cfg(unix)]
|
||||
use std::mem;
|
||||
|
||||
|
@ -69,6 +69,27 @@ struct Options {
|
|||
fs_selector: FsSelector,
|
||||
}
|
||||
|
||||
impl Options {
|
||||
/// Convert command-line arguments into [`Options`].
|
||||
fn from(matches: &ArgMatches) -> Self {
|
||||
Self {
|
||||
show_local_fs: matches.is_present(OPT_LOCAL),
|
||||
show_all_fs: matches.is_present(OPT_ALL),
|
||||
show_listed_fs: false,
|
||||
show_fs_type: matches.is_present(OPT_PRINT_TYPE),
|
||||
show_inode_instead: matches.is_present(OPT_INODES),
|
||||
human_readable_base: if matches.is_present(OPT_HUMAN_READABLE) {
|
||||
1024
|
||||
} else if matches.is_present(OPT_HUMAN_READABLE_2) {
|
||||
1000
|
||||
} else {
|
||||
-1
|
||||
},
|
||||
fs_selector: FsSelector::from(matches),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Filesystem {
|
||||
mount_info: MountInfo,
|
||||
|
@ -80,18 +101,19 @@ fn usage() -> String {
|
|||
}
|
||||
|
||||
impl FsSelector {
|
||||
fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn include(&mut self, fs_type: String) {
|
||||
self.include.insert(fs_type);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn exclude(&mut self, fs_type: String) {
|
||||
self.exclude.insert(fs_type);
|
||||
/// Convert command-line arguments into a [`FsSelector`].
|
||||
///
|
||||
/// This function reads the include and exclude sets from
|
||||
/// [`ArgMatches`] and returns the corresponding [`FsSelector`]
|
||||
/// instance.
|
||||
fn from(matches: &ArgMatches) -> Self {
|
||||
let include = HashSet::from_iter(matches.values_of_lossy(OPT_TYPE).unwrap_or_default());
|
||||
let exclude = HashSet::from_iter(
|
||||
matches
|
||||
.values_of_lossy(OPT_EXCLUDE_TYPE)
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
Self { include, exclude }
|
||||
}
|
||||
|
||||
fn should_select(&self, fs_type: &str) -> bool {
|
||||
|
@ -102,24 +124,6 @@ impl FsSelector {
|
|||
}
|
||||
}
|
||||
|
||||
impl Options {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
show_local_fs: false,
|
||||
show_all_fs: false,
|
||||
show_listed_fs: false,
|
||||
show_fs_type: false,
|
||||
show_inode_instead: false,
|
||||
// block_size: match env::var("BLOCKSIZE") {
|
||||
// Ok(size) => size.parse().unwrap(),
|
||||
// Err(_) => 512,
|
||||
// },
|
||||
human_readable_base: -1,
|
||||
fs_selector: FsSelector::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Filesystem {
|
||||
// TODO: resolve uuid in `mount_info.dev_name` if exists
|
||||
fn new(mount_info: MountInfo) -> Option<Self> {
|
||||
|
@ -293,34 +297,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
}
|
||||
}
|
||||
|
||||
let mut opt = Options::new();
|
||||
if matches.is_present(OPT_LOCAL) {
|
||||
opt.show_local_fs = true;
|
||||
}
|
||||
if matches.is_present(OPT_ALL) {
|
||||
opt.show_all_fs = true;
|
||||
}
|
||||
if matches.is_present(OPT_INODES) {
|
||||
opt.show_inode_instead = true;
|
||||
}
|
||||
if matches.is_present(OPT_PRINT_TYPE) {
|
||||
opt.show_fs_type = true;
|
||||
}
|
||||
if matches.is_present(OPT_HUMAN_READABLE) {
|
||||
opt.human_readable_base = 1024;
|
||||
}
|
||||
if matches.is_present(OPT_HUMAN_READABLE_2) {
|
||||
opt.human_readable_base = 1000;
|
||||
}
|
||||
for fs_type in matches.values_of_lossy(OPT_TYPE).unwrap_or_default() {
|
||||
opt.fs_selector.include(fs_type.to_owned());
|
||||
}
|
||||
for fs_type in matches
|
||||
.values_of_lossy(OPT_EXCLUDE_TYPE)
|
||||
.unwrap_or_default()
|
||||
{
|
||||
opt.fs_selector.exclude(fs_type.to_owned());
|
||||
}
|
||||
let opt = Options::from(&matches);
|
||||
|
||||
let fs_list = filter_mount_list(read_fs_list(), &paths, &opt)
|
||||
.into_iter()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue