-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathcli.rs
40 lines (32 loc) · 1.03 KB
/
cli.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//! CLI related functions, uses the clap argparsing definitions from `opts.rs`.
use std::{
io,
path::{Path, PathBuf},
vec::Vec,
};
use clap::Parser;
use fs_err as fs;
use crate::{Opts, QuestionPolicy, Subcommand};
impl Opts {
/// A helper method that calls `clap::Parser::parse`.
///
/// And:
/// 1. Make paths absolute.
/// 2. Checks the QuestionPolicy.
pub fn parse_args() -> crate::Result<(Self, QuestionPolicy)> {
let mut opts = Self::parse();
let (Subcommand::Compress { files, .. } | Subcommand::Decompress { files, .. }) = &mut opts.cmd;
*files = canonicalize_files(files)?;
let skip_questions_positively = if opts.yes {
QuestionPolicy::AlwaysYes
} else if opts.no {
QuestionPolicy::AlwaysNo
} else {
QuestionPolicy::Ask
};
Ok((opts, skip_questions_positively))
}
}
fn canonicalize_files(files: &[impl AsRef<Path>]) -> io::Result<Vec<PathBuf>> {
files.iter().map(fs::canonicalize).collect()
}