Merge pull request #239 from Crypto-Spartan/fix-zip-warning
[ouch.git] / src / cli.rs
blobf8578e64bd293441c29c9008d7e073b905d351db
1 //! CLI related functions, uses the clap argparsing definitions from `opts.rs`.
3 use std::{
4     io,
5     path::{Path, PathBuf},
6     vec::Vec,
7 };
9 use clap::Parser;
10 use fs_err as fs;
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();
20 impl Opts {
21     /// A helper method that calls `clap::Parser::parse`.
22     ///
23     /// And:
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!(),
41         };
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))
51     }
54 fn canonicalize_files(files: &[impl AsRef<Path>]) -> io::Result<Vec<PathBuf>> {
55     files.iter().map(fs::canonicalize).collect()