mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
df: refactor function for parsing CLI args
Add a `Options::from()` function to collect the code for parsing an `Options` object from the `clap::ArgMatches` object.
This commit is contained in:
parent
e60b581c38
commit
639971e520
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::statfs_fn;
|
||||||
use uucore::fsext::{read_fs_list, FsUsage, MountInfo};
|
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 number_prefix::NumberPrefix;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
use std::iter::FromIterator;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
|
@ -69,6 +69,27 @@ struct Options {
|
||||||
fs_selector: FsSelector,
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
struct Filesystem {
|
struct Filesystem {
|
||||||
mount_info: MountInfo,
|
mount_info: MountInfo,
|
||||||
|
@ -80,18 +101,19 @@ fn usage() -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FsSelector {
|
impl FsSelector {
|
||||||
fn new() -> Self {
|
/// Convert command-line arguments into a [`FsSelector`].
|
||||||
Self::default()
|
///
|
||||||
}
|
/// This function reads the include and exclude sets from
|
||||||
|
/// [`ArgMatches`] and returns the corresponding [`FsSelector`]
|
||||||
#[inline(always)]
|
/// instance.
|
||||||
fn include(&mut self, fs_type: String) {
|
fn from(matches: &ArgMatches) -> Self {
|
||||||
self.include.insert(fs_type);
|
let include = HashSet::from_iter(matches.values_of_lossy(OPT_TYPE).unwrap_or_default());
|
||||||
}
|
let exclude = HashSet::from_iter(
|
||||||
|
matches
|
||||||
#[inline(always)]
|
.values_of_lossy(OPT_EXCLUDE_TYPE)
|
||||||
fn exclude(&mut self, fs_type: String) {
|
.unwrap_or_default(),
|
||||||
self.exclude.insert(fs_type);
|
);
|
||||||
|
Self { include, exclude }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn should_select(&self, fs_type: &str) -> bool {
|
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 {
|
impl Filesystem {
|
||||||
// TODO: resolve uuid in `mount_info.dev_name` if exists
|
// TODO: resolve uuid in `mount_info.dev_name` if exists
|
||||||
fn new(mount_info: MountInfo) -> Option<Self> {
|
fn new(mount_info: MountInfo) -> Option<Self> {
|
||||||
|
@ -293,34 +297,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut opt = Options::new();
|
let opt = Options::from(&matches);
|
||||||
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 fs_list = filter_mount_list(read_fs_list(), &paths, &opt)
|
let fs_list = filter_mount_list(read_fs_list(), &paths, &opt)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue