Allow working directory to be nonexistent if search path is given
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #1072
authored by Sijmen · committed by David Peter · parent ac934bd · Browse files · 1 file·+28 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+28 added−29 removed
- other:+28 added−29 removed
Provenance
- Commit
fdcbb2f- Tree
51b30c1- Author
Sijmen <me@sijman.nl>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
ac934bd
1 file changed+28 added−29 removed
modified:src/main.rs+28 added-29 removed
68 unmodified lines69 fn run() -> Result<ExitCode> {70 let matches = app::build_app().get_matches_from(env::args_os());71 72 set_working_dir(&matches)?;73- let current_directory = Path::new(".");74- ensure_current_directory_exists(current_directory)?;75- let search_paths = extract_search_paths(&matches, current_directory)?;73+ let search_paths = extract_search_paths(&matches)?;76-74 let pattern = extract_search_pattern(&matches)?;75 ensure_search_pattern_is_not_a_path(&matches, pattern)?;76 let pattern_regex = build_pattern_regex(&matches, pattern)?;77 43 unmodified lines121 .unwrap_or("");122 Ok(pattern)123 }124 128-fn extract_search_paths(129- matches: &clap::ArgMatches,130- current_directory: &Path,131-) -> Result<Vec<PathBuf>> {132- let mut search_paths = matches125+fn extract_search_paths(matches: &clap::ArgMatches) -> Result<Vec<PathBuf>> {126+ let current_directory = Path::new(".");127+128+ let parameter_paths = matches129 .values_of_os("path")134- .or_else(|| matches.values_of_os("search-path"))135- .map_or_else(136- || vec![current_directory.to_path_buf()],137- |paths| {138- paths139- .filter_map(|path| {140- let path_buffer = PathBuf::from(path);141- if filesystem::is_existing_directory(&path_buffer) {142- Some(path_buffer)143- } else {144- print_error(format!(145- "Search path '{}' is not a directory.",146- path_buffer.to_string_lossy()147- ));148- None149- }150- })151- .collect()152- },153- );130+ .or_else(|| matches.values_of_os("search-path"));131+132+ let mut search_paths = match parameter_paths {133+ Some(paths) => paths134+ .filter_map(|path| {135+ let path_buffer = PathBuf::from(path);136+ if filesystem::is_existing_directory(&path_buffer) {137+ Some(path_buffer)138+ } else {139+ print_error(format!(140+ "Search path '{}' is not a directory.",141+ path_buffer.to_string_lossy(),142+ ));143+ None144+ }145+ })146+ .collect(),147+ None => {148+ ensure_current_directory_exists(current_directory)?;149+ vec![current_directory.to_path_buf()]150+ }151+ };152+153 if search_paths.is_empty() {154 return Err(anyhow!("No valid search paths given."));155 }156 if matches.is_present("absolute-path") {412 unmodified lines