1 //! CLI related functions, uses the clap argparsing definitions from `opts.rs`.
12 use crate::{accessible::set_accessible, utils::FileVisibilityPolicy, Opts, QuestionPolicy, Subcommand};
15 /// A helper method that calls `clap::Parser::parse`.
18 /// 1. Make paths absolute.
19 /// 2. Checks the QuestionPolicy.
20 pub fn parse_args() -> crate::Result<(Self, QuestionPolicy, FileVisibilityPolicy)> {
21 let mut opts = Self::parse();
23 set_accessible(opts.accessible);
25 let (Subcommand::Compress { files, .. }
26 | Subcommand::Decompress { files, .. }
27 | Subcommand::List { archives: files, .. }) = &mut opts.cmd;
28 *files = canonicalize_files(files)?;
30 let skip_questions_positively = match (opts.yes, opts.no) {
31 (false, false) => QuestionPolicy::Ask,
32 (true, false) => QuestionPolicy::AlwaysYes,
33 (false, true) => QuestionPolicy::AlwaysNo,
34 (true, true) => unreachable!(),
37 // TODO: change this to be just a single function call?
38 let file_visibility_policy = FileVisibilityPolicy::new()
39 .read_git_exclude(opts.gitignore)
40 .read_ignore(opts.gitignore)
41 .read_git_ignore(opts.gitignore)
42 .read_hidden(opts.hidden);
44 Ok((opts, skip_questions_positively, file_visibility_policy))
48 fn canonicalize_files(files: &[impl AsRef<Path>]) -> io::Result<Vec<PathBuf>> {
49 files.iter().map(fs::canonicalize).collect()