fix: escape quotes when string has whitespace

This commit is contained in:
nibon7 2022-11-02 11:59:33 +08:00
parent c6fe49aba5
commit 50aefdb9b3
No known key found for this signature in database
GPG key ID: 281CE465D8EEC03B
3 changed files with 11 additions and 9 deletions

View file

@ -13,7 +13,7 @@ Generates [Nushell](https://github.com/nushell/nushell) completions for [`clap`]
### myapp.rs ### myapp.rs
```rust ```rust
use clap::{Arg, ArgAction, Command}; use clap::{builder::PossibleValue, Arg, ArgAction, Command, ValueHint};
use clap_complete::generate; use clap_complete::generate;
use clap_complete_nushell::Nushell; use clap_complete_nushell::Nushell;
use std::io; use std::io;
@ -25,7 +25,7 @@ fn main() {
.about("Tests completions") .about("Tests completions")
.arg( .arg(
Arg::new("file") Arg::new("file")
.value_hint(clap::ValueHint::FilePath) .value_hint(ValueHint::FilePath)
.help("some input file"), .help("some input file"),
) )
.arg( .arg(
@ -54,9 +54,7 @@ fn main() {
Arg::new("config") Arg::new("config")
.long("config") .long("config")
.action(ArgAction::Set) .action(ArgAction::Set)
.value_parser([clap::builder::PossibleValue::new( .value_parser([PossibleValue::new("Lest quotes aren't escaped.")])
"Lest quotes aren't escaped.",
)])
.help("the other case to test"), .help("the other case to test"),
), ),
), ),
@ -66,7 +64,6 @@ fn main() {
} }
``` ```
### myapp.nu ### myapp.nu
```nu ```nu
@ -98,7 +95,7 @@ module completions {
] ]
def "nu-complete myapp some_cmd sub_cmd config" [] { def "nu-complete myapp some_cmd sub_cmd config" [] {
[ "Lest quotes aren't escaped." ] [ "\"Lest quotes aren't escaped.\"" ]
} }
# sub-subcommand # sub-subcommand

View file

@ -123,7 +123,12 @@ impl<'a, 'b> Argument<'a, 'b> {
s.push_str("\n ["); s.push_str("\n [");
for value in &possible_values { for value in &possible_values {
s.push_str(format!(r#" "{}""#, value.get_name()).as_str()); let name = value.get_name();
if name.contains(|c: char| c.is_whitespace()) {
s.push_str(format!(r#" "\"{}\"""#, name).as_str());
} else {
s.push_str(format!(r#" "{}""#, name).as_str());
}
} }
s.push_str(" ]\n }\n\n"); s.push_str(" ]\n }\n\n");

View file

@ -26,7 +26,7 @@ module completions {
] ]
def "nu-complete my-app some_cmd sub_cmd config" [] { def "nu-complete my-app some_cmd sub_cmd config" [] {
[ "Lest quotes, aren't escaped." "Second to trigger display of options" ] [ "\"Lest quotes, aren't escaped.\"" "\"Second to trigger display of options\"" ]
} }
# sub-subcommand # sub-subcommand