diff --git a/crates/nu-cli/src/completions/completion_common.rs b/crates/nu-cli/src/completions/completion_common.rs index c8ff69246e..93629d74b1 100644 --- a/crates/nu-cli/src/completions/completion_common.rs +++ b/crates/nu-cli/src/completions/completion_common.rs @@ -152,7 +152,9 @@ pub fn complete_item( pub fn escape_path(path: String, dir: bool) -> String { let filename_contaminated = !dir && path.contains(['\'', '"', ' ', '#', '(', ')']); let dirname_contaminated = dir && path.contains(['\'', '"', ' ', '#']); - if filename_contaminated || dirname_contaminated || path.parse::().is_ok() { + let maybe_flag = path.starts_with('-'); + let maybe_number = path.parse::().is_ok(); + if filename_contaminated || dirname_contaminated || maybe_flag || maybe_number { format!("`{path}`") } else { path diff --git a/crates/nu-cli/tests/completions.rs b/crates/nu-cli/tests/completions.rs index c70c10b16b..7147099371 100644 --- a/crates/nu-cli/tests/completions.rs +++ b/crates/nu-cli/tests/completions.rs @@ -527,6 +527,10 @@ fn file_completion_quoted() { let suggestions = completer.complete(target_dir, target_dir.len()); let expected_paths: Vec = vec![ + "`--help`".to_string(), + "`-42`".to_string(), + "`-inf`".to_string(), + "`4.2`".to_string(), "`te st.txt`".to_string(), "`te#st.txt`".to_string(), "`te'st.txt`".to_string(), diff --git a/tests/fixtures/quoted_completions/--help b/tests/fixtures/quoted_completions/--help new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures/quoted_completions/-42 b/tests/fixtures/quoted_completions/-42 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures/quoted_completions/-inf b/tests/fixtures/quoted_completions/-inf new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures/quoted_completions/4.2 b/tests/fixtures/quoted_completions/4.2 new file mode 100644 index 0000000000..e69de29bb2