1 //! CLI related functions, uses the clap argparsing definitions from `opts.rs`.
11 use once_cell::sync::OnceCell;
13 use crate::{utils::FileVisibilityPolicy, Opts, QuestionPolicy, Subcommand};
15 /// Whether to enable accessible output (removes info output and reduces other
16 /// output, removes visual markers like '[' and ']').
17 /// Removes th progress bar as well
18 pub static ACCESSIBLE: OnceCell<bool> = OnceCell::new();
21 /// A helper method that calls `clap::Parser::parse`.
24 /// 1. Make paths absolute.
25 /// 2. Checks the QuestionPolicy.
26 pub fn parse_args() -> crate::Result<(Self, QuestionPolicy, FileVisibilityPolicy)> {
27 let mut opts = Self::parse();
29 ACCESSIBLE.set(opts.accessible).unwrap();
31 let (Subcommand::Compress { files, .. }
32 | Subcommand::Decompress { files, .. }
33 | Subcommand::List { archives: files, .. }) = &mut opts.cmd;
34 *files = canonicalize_files(files)?;
36 let skip_questions_positively = match (opts.yes, opts.no) {
37 (false, false) => QuestionPolicy::Ask,
38 (true, false) => QuestionPolicy::AlwaysYes,
39 (false, true) => QuestionPolicy::AlwaysNo,
40 (true, true) => unreachable!(),
43 // TODO: change this to be just a single function call?
44 let file_visibility_policy = FileVisibilityPolicy::new()
45 .read_git_exclude(opts.gitignore)
46 .read_ignore(opts.gitignore)
47 .read_git_ignore(opts.gitignore)
48 .read_hidden(opts.hidden);
50 Ok((opts, skip_questions_positively, file_visibility_policy))
54 fn canonicalize_files(files: &[impl AsRef<Path>]) -> io::Result<Vec<PathBuf>> {
55 files.iter().map(fs::canonicalize).collect()