mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
feat(nu): 'nushell' completion support
This commit is contained in:
commit
cc57615764
20 changed files with 4581 additions and 94 deletions
3015
Cargo.lock
generated
3015
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -7,6 +7,7 @@ members = [
|
|||
"clap_lex",
|
||||
"clap_complete",
|
||||
"clap_complete_fig",
|
||||
"clap_complete_nushell",
|
||||
"clap_mangen",
|
||||
]
|
||||
|
||||
|
|
11
clap_complete_nushell/CHANGELOG.md
Normal file
11
clap_complete_nushell/CHANGELOG.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
# Change Log
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
<!-- next-header -->
|
||||
## [Unreleased] - ReleaseDate
|
||||
|
||||
<!-- next-url -->
|
||||
[Unreleased]: https://github.com/clap-rs/clap/compare/bd163098e122bb65e053a61b773931c8f36d2e7d...HEAD
|
45
clap_complete_nushell/Cargo.toml
Normal file
45
clap_complete_nushell/Cargo.toml
Normal file
|
@ -0,0 +1,45 @@
|
|||
[package]
|
||||
name = "clap_complete_nushell"
|
||||
version = "0.1.11"
|
||||
description = "A generator library used with clap for Nushell completion scripts"
|
||||
repository = "https://github.com/clap-rs/clap/tree/master/clap_complete_nushell"
|
||||
categories = ["command-line-interface"]
|
||||
keywords = [
|
||||
"clap",
|
||||
"cli",
|
||||
"completion",
|
||||
"nushell"
|
||||
]
|
||||
license.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
include.workspace = true
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[package.metadata.release]
|
||||
pre-release-replacements = [
|
||||
{file="CHANGELOG.md", search="Unreleased", replace="{{version}}", min=1},
|
||||
{file="CHANGELOG.md", search="\\.\\.\\.HEAD", replace="...{{tag_name}}", exactly=1},
|
||||
{file="CHANGELOG.md", search="ReleaseDate", replace="{{date}}", min=1},
|
||||
{file="CHANGELOG.md", search="<!-- next-header -->", replace="<!-- next-header -->\n## [Unreleased] - ReleaseDate\n", exactly=1},
|
||||
{file="CHANGELOG.md", search="<!-- next-url -->", replace="<!-- next-url -->\n[Unreleased]: https://github.com/clap-rs/clap/compare/{{tag_name}}...HEAD", exactly=1},
|
||||
]
|
||||
|
||||
[lib]
|
||||
bench = false
|
||||
|
||||
[dependencies]
|
||||
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std"] }
|
||||
clap_complete = { path = "../clap_complete", version = "4.0.0" }
|
||||
|
||||
[dev-dependencies]
|
||||
snapbox = { version = "0.4.10", features = ["diff"] }
|
||||
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "help"] }
|
||||
nu-cli = "0.78.0"
|
||||
nu-command = "0.78.0"
|
||||
nu-parser = "0.78.0"
|
||||
nu-protocol = "0.78.0"
|
||||
nu-test-support = "0.78.0"
|
||||
reedline = "0.18.0"
|
201
clap_complete_nushell/LICENSE-APACHE
Normal file
201
clap_complete_nushell/LICENSE-APACHE
Normal file
|
@ -0,0 +1,201 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
21
clap_complete_nushell/LICENSE-MIT
Normal file
21
clap_complete_nushell/LICENSE-MIT
Normal file
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2022 nibon7 <nibon7@163.com> and Clap Contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
114
clap_complete_nushell/README.md
Normal file
114
clap_complete_nushell/README.md
Normal file
|
@ -0,0 +1,114 @@
|
|||
# clap_complete_nushell
|
||||
|
||||
Generates [Nushell](https://github.com/nushell/nushell) completions for [`clap`](https://github.com/clap-rs/clap) based CLIs
|
||||
|
||||
[![Crates.io](https://img.shields.io/crates/v/clap_complete_nushell)](https://crates.io/crates/clap_complete_nushell)
|
||||
[![Crates.io](https://img.shields.io/crates/d/clap_complete_nushell)](https://crates.io/crates/clap_complete_nushell)
|
||||
[![License](https://img.shields.io/badge/license-Apache%202.0-blue)](LICENSE-APACHE)
|
||||
[![License](https://img.shields.io/badge/license-MIT-blue)](LICENSE-MIT)
|
||||
[![docs.rs](https://img.shields.io/docsrs/clap_complete_nushell)](https://docs.rs/clap_complete_nushell)
|
||||
|
||||
- [Questions & Discussions](https://github.com/clap-rs/clap/discussions)
|
||||
- [CONTRIBUTING](../CONTRIBUTING.md)
|
||||
|
||||
## Examples
|
||||
|
||||
### myapp.rs
|
||||
|
||||
```rust
|
||||
use clap::{builder::PossibleValue, Arg, ArgAction, Command, ValueHint};
|
||||
use clap_complete::generate;
|
||||
use clap_complete_nushell::Nushell;
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
let mut cmd = Command::new("myapp")
|
||||
.version("3.0")
|
||||
.propagate_version(true)
|
||||
.about("Tests completions")
|
||||
.arg(
|
||||
Arg::new("file")
|
||||
.value_hint(ValueHint::FilePath)
|
||||
.help("some input file"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("config")
|
||||
.action(ArgAction::Count)
|
||||
.help("some config file")
|
||||
.short('c')
|
||||
.visible_short_alias('C')
|
||||
.long("config")
|
||||
.visible_alias("conf"),
|
||||
)
|
||||
.arg(Arg::new("choice").value_parser(["first", "second"]))
|
||||
.subcommand(
|
||||
Command::new("test").about("tests things").arg(
|
||||
Arg::new("case")
|
||||
.long("case")
|
||||
.action(ArgAction::Set)
|
||||
.help("the case to test"),
|
||||
),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("some_cmd")
|
||||
.about("top level subcommand")
|
||||
.subcommand(
|
||||
Command::new("sub_cmd").about("sub-subcommand").arg(
|
||||
Arg::new("config")
|
||||
.long("config")
|
||||
.action(ArgAction::Set)
|
||||
.value_parser([PossibleValue::new("Lest quotes aren't escaped.")])
|
||||
.help("the other case to test"),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
generate(Nushell, &mut cmd, "myapp", &mut io::stdout());
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### myapp.nu
|
||||
|
||||
```nu
|
||||
module completions {
|
||||
|
||||
def "nu-complete myapp choice" [] {
|
||||
[ "first" "second" ]
|
||||
}
|
||||
|
||||
# Tests completions
|
||||
export extern myapp [
|
||||
file?: string # some input file
|
||||
--config(-c) # some config file
|
||||
--conf # some config file
|
||||
-C # some config file
|
||||
choice?: string@"nu-complete myapp choice"
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# tests things
|
||||
export extern "myapp test" [
|
||||
--case: string # the case to test
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# top level subcommand
|
||||
export extern "myapp some_cmd" [
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
def "nu-complete myapp some_cmd sub_cmd config" [] {
|
||||
[ "\"Lest quotes aren't escaped.\"" ]
|
||||
}
|
||||
|
||||
# sub-subcommand
|
||||
export extern "myapp some_cmd sub_cmd" [
|
||||
--config: string@"nu-complete myapp some_cmd sub_cmd config" # the other case to test
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
use completions *
|
||||
```
|
12
clap_complete_nushell/examples/nushell_completion.rs
Normal file
12
clap_complete_nushell/examples/nushell_completion.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
use clap::Command;
|
||||
use clap_complete::generate;
|
||||
use clap_complete_nushell::Nushell;
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
let mut cmd = Command::new("myapp")
|
||||
.subcommand(Command::new("test").subcommand(Command::new("config")))
|
||||
.subcommand(Command::new("hello"));
|
||||
|
||||
generate(Nushell, &mut cmd, "myapp", &mut io::stdout());
|
||||
}
|
49
clap_complete_nushell/examples/sub_subcommands.rs
Normal file
49
clap_complete_nushell/examples/sub_subcommands.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
use clap::{builder::PossibleValue, Arg, ArgAction, Command, ValueHint};
|
||||
use clap_complete::generate;
|
||||
use clap_complete_nushell::Nushell;
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
let mut cmd = Command::new("myapp")
|
||||
.version("3.0")
|
||||
.propagate_version(true)
|
||||
.about("Tests completions")
|
||||
.arg(
|
||||
Arg::new("file")
|
||||
.value_hint(ValueHint::FilePath)
|
||||
.help("some input file"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("config")
|
||||
.action(ArgAction::Count)
|
||||
.help("some config file")
|
||||
.short('c')
|
||||
.visible_short_alias('C')
|
||||
.long("config")
|
||||
.visible_alias("conf"),
|
||||
)
|
||||
.arg(Arg::new("choice").value_parser(["first", "second"]))
|
||||
.subcommand(
|
||||
Command::new("test").about("tests things").arg(
|
||||
Arg::new("case")
|
||||
.long("case")
|
||||
.action(ArgAction::Set)
|
||||
.help("the case to test"),
|
||||
),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("some_cmd")
|
||||
.about("top level subcommand")
|
||||
.subcommand(
|
||||
Command::new("sub_cmd").about("sub-subcommand").arg(
|
||||
Arg::new("config")
|
||||
.long("config")
|
||||
.action(ArgAction::Set)
|
||||
.value_parser([PossibleValue::new("Lest quotes aren't escaped.")])
|
||||
.help("the other case to test"),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
generate(Nushell, &mut cmd, "myapp", &mut io::stdout());
|
||||
}
|
203
clap_complete_nushell/src/lib.rs
Normal file
203
clap_complete_nushell/src/lib.rs
Normal file
|
@ -0,0 +1,203 @@
|
|||
//! Generates [Nushell](https://github.com/nushell/nushell) completions for [`clap`](https://github.com/clap-rs/clap) based CLIs
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
//! ```
|
||||
//! use clap::Command;
|
||||
//! use clap_complete::generate;
|
||||
//! use clap_complete_nushell::Nushell;
|
||||
//! use std::io;
|
||||
//!
|
||||
//! let mut cmd = Command::new("myapp")
|
||||
//! .subcommand(Command::new("test").subcommand(Command::new("config")))
|
||||
//! .subcommand(Command::new("hello"));
|
||||
//!
|
||||
//! generate(Nushell, &mut cmd, "myapp", &mut io::stdout());
|
||||
//! ```
|
||||
|
||||
#![doc(html_logo_url = "https://raw.githubusercontent.com/clap-rs/clap/master/assets/clap.png")]
|
||||
#![warn(missing_docs, trivial_casts, unused_allocation, trivial_numeric_casts)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use clap::{builder::PossibleValue, Arg, ArgAction, Command};
|
||||
use clap_complete::Generator;
|
||||
|
||||
/// Generate Nushell complete file
|
||||
pub struct Nushell;
|
||||
|
||||
impl Generator for Nushell {
|
||||
fn file_name(&self, name: &str) -> String {
|
||||
format!("{name}.nu")
|
||||
}
|
||||
|
||||
fn generate(&self, cmd: &Command, buf: &mut dyn std::io::Write) {
|
||||
let mut completions = String::new();
|
||||
|
||||
completions.push_str("module completions {\n\n");
|
||||
|
||||
generate_completion(&mut completions, cmd, false);
|
||||
|
||||
for sub in cmd.get_subcommands() {
|
||||
generate_completion(&mut completions, sub, true);
|
||||
}
|
||||
|
||||
completions.push_str("}\n\n");
|
||||
completions.push_str("use completions *\n");
|
||||
|
||||
buf.write_all(completions.as_bytes())
|
||||
.expect("Failed to write to generated file")
|
||||
}
|
||||
}
|
||||
|
||||
fn append_value_completion_and_help(
|
||||
arg: &Arg,
|
||||
name: &str,
|
||||
possible_values: &Vec<PossibleValue>,
|
||||
s: &mut String,
|
||||
) {
|
||||
let takes_values = arg
|
||||
.get_num_args()
|
||||
.map(|r| r.takes_values())
|
||||
.unwrap_or(false);
|
||||
|
||||
if takes_values {
|
||||
s.push_str(": string");
|
||||
|
||||
if !possible_values.is_empty() {
|
||||
s.push_str(format!(r#"@"nu-complete {} {}""#, name, arg.get_id()).as_str())
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(help) = arg.get_help() {
|
||||
let indent: usize = 30;
|
||||
let width = match s.lines().last() {
|
||||
Some(line) => indent.saturating_sub(line.len()),
|
||||
None => 0,
|
||||
};
|
||||
|
||||
s.push_str(format!("{:>width$}# {}", ' ', help).as_str());
|
||||
}
|
||||
|
||||
s.push('\n');
|
||||
}
|
||||
|
||||
fn append_value_completion_defs(arg: &Arg, name: &str, s: &mut String) {
|
||||
let possible_values = arg.get_possible_values();
|
||||
if possible_values.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
s.push_str(format!(r#" def "nu-complete {} {}" [] {{"#, name, arg.get_id()).as_str());
|
||||
s.push_str("\n [");
|
||||
|
||||
for value in possible_values {
|
||||
let vname = value.get_name();
|
||||
if vname.contains(|c: char| c.is_whitespace()) {
|
||||
s.push_str(format!(r#" "\"{vname}\"""#).as_str());
|
||||
} else {
|
||||
s.push_str(format!(r#" "{vname}""#).as_str());
|
||||
}
|
||||
}
|
||||
|
||||
s.push_str(" ]\n }\n\n");
|
||||
}
|
||||
|
||||
fn append_argument(arg: &Arg, name: &str, s: &mut String) {
|
||||
let possible_values = arg.get_possible_values();
|
||||
|
||||
if arg.is_positional() {
|
||||
// rest arguments
|
||||
if matches!(arg.get_action(), ArgAction::Append) {
|
||||
s.push_str(format!(" ...{}", arg.get_id()).as_str());
|
||||
} else {
|
||||
s.push_str(format!(" {}", arg.get_id()).as_str());
|
||||
|
||||
if !arg.is_required_set() {
|
||||
s.push('?');
|
||||
}
|
||||
}
|
||||
|
||||
append_value_completion_and_help(arg, name, &possible_values, s);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let shorts = arg.get_short_and_visible_aliases();
|
||||
let longs = arg.get_long_and_visible_aliases();
|
||||
|
||||
match shorts {
|
||||
Some(shorts) => match longs {
|
||||
Some(longs) => {
|
||||
// short options and long options
|
||||
s.push_str(
|
||||
format!(
|
||||
" --{}(-{})",
|
||||
longs.first().expect("At least one long option expected"),
|
||||
shorts.first().expect("At lease one short option expected")
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
append_value_completion_and_help(arg, name, &possible_values, s);
|
||||
|
||||
// long alias
|
||||
for long in longs.iter().skip(1) {
|
||||
s.push_str(format!(" --{long}").as_str());
|
||||
append_value_completion_and_help(arg, name, &possible_values, s);
|
||||
}
|
||||
|
||||
// short alias
|
||||
for short in shorts.iter().skip(1) {
|
||||
s.push_str(format!(" -{short}").as_str());
|
||||
append_value_completion_and_help(arg, name, &possible_values, s);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// short options only
|
||||
for short in shorts {
|
||||
s.push_str(format!(" -{short}").as_str());
|
||||
append_value_completion_and_help(arg, name, &possible_values, s);
|
||||
}
|
||||
}
|
||||
},
|
||||
None => match longs {
|
||||
Some(longs) => {
|
||||
// long options only
|
||||
for long in longs {
|
||||
s.push_str(format!(" --{long}").as_str());
|
||||
append_value_completion_and_help(arg, name, &possible_values, s);
|
||||
}
|
||||
}
|
||||
None => unreachable!("No short or long options found"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_completion(completions: &mut String, cmd: &Command, is_subcommand: bool) {
|
||||
let name = cmd.get_bin_name().expect("Failed to get bin name");
|
||||
|
||||
for arg in cmd.get_arguments() {
|
||||
append_value_completion_defs(arg, name, completions);
|
||||
}
|
||||
|
||||
if let Some(about) = cmd.get_about() {
|
||||
completions.push_str(format!(" # {about}\n").as_str());
|
||||
}
|
||||
|
||||
if is_subcommand {
|
||||
completions.push_str(format!(" export extern \"{name}\" [\n").as_str());
|
||||
} else {
|
||||
completions.push_str(format!(" export extern {name} [\n").as_str());
|
||||
}
|
||||
|
||||
for arg in cmd.get_arguments() {
|
||||
append_argument(arg, name, completions);
|
||||
}
|
||||
|
||||
completions.push_str(" ]\n\n");
|
||||
|
||||
if is_subcommand {
|
||||
for sub in cmd.get_subcommands() {
|
||||
generate_completion(completions, sub, true);
|
||||
}
|
||||
}
|
||||
}
|
257
clap_complete_nushell/tests/common.rs
Normal file
257
clap_complete_nushell/tests/common.rs
Normal file
|
@ -0,0 +1,257 @@
|
|||
use clap::{builder::PossibleValue, Arg, ArgAction, Command, ValueHint};
|
||||
|
||||
pub fn basic_command(name: &'static str) -> Command {
|
||||
Command::new(name)
|
||||
.arg(
|
||||
Arg::new("config")
|
||||
.short('c')
|
||||
.global(true)
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("v")
|
||||
.short('v')
|
||||
.conflicts_with("config")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("test")
|
||||
.about("Subcommand")
|
||||
.arg(Arg::new("debug").short('d').action(ArgAction::Count)),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn feature_sample_command(name: &'static str) -> Command {
|
||||
Command::new(name)
|
||||
.version("3.0")
|
||||
.propagate_version(true)
|
||||
.about("Tests completions")
|
||||
.arg(
|
||||
Arg::new("file")
|
||||
.value_hint(ValueHint::FilePath)
|
||||
.help("some input file"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("config")
|
||||
.action(ArgAction::Count)
|
||||
.help("some config file")
|
||||
.short('c')
|
||||
.visible_short_alias('C')
|
||||
.long("config")
|
||||
.visible_alias("conf"),
|
||||
)
|
||||
.arg(Arg::new("choice").value_parser(["first", "second"]))
|
||||
.subcommand(
|
||||
Command::new("test").about("tests things").arg(
|
||||
Arg::new("case")
|
||||
.long("case")
|
||||
.action(ArgAction::Set)
|
||||
.help("the case to test"),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn special_commands_command(name: &'static str) -> Command {
|
||||
feature_sample_command(name)
|
||||
.subcommand(
|
||||
Command::new("some_cmd")
|
||||
.about("tests other things")
|
||||
.arg(
|
||||
Arg::new("config")
|
||||
.long("config")
|
||||
.hide(true)
|
||||
.action(ArgAction::Set)
|
||||
.require_equals(true)
|
||||
.help("the other case to test"),
|
||||
)
|
||||
.arg(Arg::new("path").num_args(1..)),
|
||||
)
|
||||
.subcommand(Command::new("some-cmd-with-hyphens").alias("hyphen"))
|
||||
.subcommand(Command::new("some-hidden-cmd").hide(true))
|
||||
}
|
||||
|
||||
pub fn quoting_command(name: &'static str) -> Command {
|
||||
Command::new(name)
|
||||
.version("3.0")
|
||||
.arg(
|
||||
Arg::new("single-quotes")
|
||||
.long("single-quotes")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Can be 'always', 'auto', or 'never'"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("double-quotes")
|
||||
.long("double-quotes")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Can be \"always\", \"auto\", or \"never\""),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("backticks")
|
||||
.long("backticks")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("For more information see `echo test`"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("backslash")
|
||||
.long("backslash")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Avoid '\\n'"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("brackets")
|
||||
.long("brackets")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("List packages [filter]"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("expansions")
|
||||
.long("expansions")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Execute the shell command with $SHELL"),
|
||||
)
|
||||
.subcommands([
|
||||
Command::new("cmd-single-quotes").about("Can be 'always', 'auto', or 'never'"),
|
||||
Command::new("cmd-double-quotes").about("Can be \"always\", \"auto\", or \"never\""),
|
||||
Command::new("cmd-backticks").about("For more information see `echo test`"),
|
||||
Command::new("cmd-backslash").about("Avoid '\\n'"),
|
||||
Command::new("cmd-brackets").about("List packages [filter]"),
|
||||
Command::new("cmd-expansions").about("Execute the shell command with $SHELL"),
|
||||
])
|
||||
}
|
||||
|
||||
pub fn aliases_command(name: &'static str) -> Command {
|
||||
Command::new(name)
|
||||
.version("3.0")
|
||||
.about("testing nushell completions")
|
||||
.arg(
|
||||
Arg::new("flag")
|
||||
.short('f')
|
||||
.visible_short_alias('F')
|
||||
.long("flag")
|
||||
.action(ArgAction::SetTrue)
|
||||
.visible_alias("flg")
|
||||
.help("cmd flag"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("option")
|
||||
.short('o')
|
||||
.visible_short_alias('O')
|
||||
.long("option")
|
||||
.visible_alias("opt")
|
||||
.help("cmd option")
|
||||
.action(ArgAction::Set),
|
||||
)
|
||||
.arg(Arg::new("positional"))
|
||||
}
|
||||
|
||||
pub fn sub_subcommands_command(name: &'static str) -> Command {
|
||||
feature_sample_command(name).subcommand(
|
||||
Command::new("some_cmd")
|
||||
.about("top level subcommand")
|
||||
.visible_alias("some_cmd_alias")
|
||||
.subcommand(
|
||||
Command::new("sub_cmd").about("sub-subcommand").arg(
|
||||
Arg::new("config")
|
||||
.long("config")
|
||||
.action(ArgAction::Set)
|
||||
.value_parser([
|
||||
PossibleValue::new("Lest quotes, aren't escaped.")
|
||||
.help("help,with,comma"),
|
||||
PossibleValue::new("Second to trigger display of options"),
|
||||
])
|
||||
.help("the other case to test"),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn value_hint_command(name: &'static str) -> Command {
|
||||
Command::new(name)
|
||||
.arg(
|
||||
Arg::new("choice")
|
||||
.long("choice")
|
||||
.action(ArgAction::Set)
|
||||
.value_parser(["bash", "fish", "zsh"]),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("unknown")
|
||||
.long("unknown")
|
||||
.value_hint(ValueHint::Unknown),
|
||||
)
|
||||
.arg(Arg::new("other").long("other").value_hint(ValueHint::Other))
|
||||
.arg(
|
||||
Arg::new("path")
|
||||
.long("path")
|
||||
.short('p')
|
||||
.value_hint(ValueHint::AnyPath),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("file")
|
||||
.long("file")
|
||||
.short('f')
|
||||
.value_hint(ValueHint::FilePath),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("dir")
|
||||
.long("dir")
|
||||
.short('d')
|
||||
.value_hint(ValueHint::DirPath),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("exe")
|
||||
.long("exe")
|
||||
.short('e')
|
||||
.value_hint(ValueHint::ExecutablePath),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("cmd_name")
|
||||
.long("cmd-name")
|
||||
.value_hint(ValueHint::CommandName),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("cmd")
|
||||
.long("cmd")
|
||||
.short('c')
|
||||
.value_hint(ValueHint::CommandString),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("command_with_args")
|
||||
.action(ArgAction::Set)
|
||||
.num_args(1..)
|
||||
.trailing_var_arg(true)
|
||||
.value_hint(ValueHint::CommandWithArguments),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("user")
|
||||
.short('u')
|
||||
.long("user")
|
||||
.value_hint(ValueHint::Username),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("host")
|
||||
.short('H')
|
||||
.long("host")
|
||||
.value_hint(ValueHint::Hostname),
|
||||
)
|
||||
.arg(Arg::new("url").long("url").value_hint(ValueHint::Url))
|
||||
.arg(
|
||||
Arg::new("email")
|
||||
.long("email")
|
||||
.value_hint(ValueHint::EmailAddress),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn assert_matches_path(
|
||||
expected_path: impl AsRef<std::path::Path>,
|
||||
gen: impl clap_complete::Generator,
|
||||
mut cmd: Command,
|
||||
name: &'static str,
|
||||
) {
|
||||
let mut buf = vec![];
|
||||
clap_complete::generate(gen, &mut cmd, name, &mut buf);
|
||||
|
||||
snapbox::Assert::new()
|
||||
.action_env("SNAPSHOTS")
|
||||
.normalize_paths(false)
|
||||
.matches_path(expected_path, buf);
|
||||
}
|
326
clap_complete_nushell/tests/completion.rs
Normal file
326
clap_complete_nushell/tests/completion.rs
Normal file
|
@ -0,0 +1,326 @@
|
|||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use nu_cli::NuCompleter;
|
||||
use nu_command::create_default_context;
|
||||
use nu_parser::parse;
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack, StateWorkingSet},
|
||||
Value,
|
||||
};
|
||||
|
||||
use reedline::{Completer as _, Suggestion};
|
||||
|
||||
// creates a new engine with the current path into the completions fixtures folder
|
||||
fn new_engine() -> (PathBuf, EngineState, Stack) {
|
||||
// Target folder inside assets
|
||||
let mut dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests");
|
||||
dir.push("snapshots");
|
||||
|
||||
let mut dir_str = dir
|
||||
.clone()
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.unwrap_or_default();
|
||||
dir_str.push(std::path::MAIN_SEPARATOR);
|
||||
|
||||
// Create a new engine with default context
|
||||
let mut engine_state = create_default_context();
|
||||
|
||||
// New stack
|
||||
let mut stack = Stack::new();
|
||||
|
||||
// Add pwd as env var
|
||||
stack.add_env_var(
|
||||
"PWD".to_string(),
|
||||
Value::String {
|
||||
val: dir_str.clone(),
|
||||
span: nu_protocol::Span::new(0, dir_str.len()),
|
||||
},
|
||||
);
|
||||
|
||||
#[cfg(windows)]
|
||||
stack.add_env_var(
|
||||
"Path".to_string(),
|
||||
Value::String {
|
||||
val: "c:\\some\\path;c:\\some\\other\\path".to_string(),
|
||||
span: nu_protocol::Span::new(0, dir_str.len()),
|
||||
},
|
||||
);
|
||||
|
||||
#[cfg(not(windows))]
|
||||
stack.add_env_var(
|
||||
"PATH".to_string(),
|
||||
Value::String {
|
||||
val: "/some/path:/some/other/path".to_string(),
|
||||
span: nu_protocol::Span::new(0, dir_str.len()),
|
||||
},
|
||||
);
|
||||
|
||||
// Merge environment into the permanent state
|
||||
let merge_result = engine_state.merge_env(&mut stack, &dir);
|
||||
assert!(merge_result.is_ok(), "{}", merge_result.unwrap_err());
|
||||
|
||||
(dir, engine_state, stack)
|
||||
}
|
||||
|
||||
fn external_completion(file_name: &str) -> NuCompleter {
|
||||
// Create a new engine
|
||||
let (dir, mut engine_state, mut stack) = new_engine();
|
||||
|
||||
let path = dir.join(file_name);
|
||||
let mut buf = Vec::new();
|
||||
let mut file =
|
||||
File::open(&path).unwrap_or_else(|_| panic!("Failed to open {}", path.display()));
|
||||
file.read_to_end(&mut buf)
|
||||
.unwrap_or_else(|_| panic!("Failed to open {}", path.display()));
|
||||
|
||||
let (_, delta) = {
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
let (block, err) = parse(&mut working_set, None, &buf, false, &[]);
|
||||
assert!(err.is_none(), "{:?}", err.unwrap());
|
||||
|
||||
(block, working_set.render())
|
||||
};
|
||||
|
||||
assert!(engine_state.merge_delta(delta).is_ok());
|
||||
|
||||
// Merge environment into the permanent state
|
||||
assert!(engine_state.merge_env(&mut stack, &dir).is_ok());
|
||||
|
||||
let latest_block_id = engine_state.num_blocks() - 1;
|
||||
|
||||
// Change config adding the external completer
|
||||
let mut config = engine_state.get_config().clone();
|
||||
config.external_completer = Some(latest_block_id);
|
||||
engine_state.set_config(&config);
|
||||
|
||||
// Instantiate a new completer
|
||||
NuCompleter::new(Arc::new(engine_state), stack)
|
||||
}
|
||||
|
||||
// match a list of suggestions with the expected values
|
||||
#[track_caller]
|
||||
fn assert_suggestions(expected: &[&str], suggestions: Vec<Suggestion>) {
|
||||
let expected = expected
|
||||
.iter()
|
||||
.map(|s| (*s).to_owned())
|
||||
.collect::<Vec<String>>();
|
||||
let suggestions = suggestions
|
||||
.into_iter()
|
||||
.map(|s| s.value)
|
||||
.collect::<Vec<String>>();
|
||||
assert_eq!(expected, suggestions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_basic() {
|
||||
let mut completer = external_completion("basic.nu");
|
||||
|
||||
let input = "my-app -";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &["--help", "-c", "-h", "-v"];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app test -";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &["--help", "-c", "-d", "-h"];
|
||||
assert_suggestions(expected, suggestions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_feature_sample() {
|
||||
let mut completer = external_completion("feature_sample.nu");
|
||||
|
||||
let input = "my-app test --";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &["--case", "--help", "--version"];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app choice ";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &["first", "second"];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app -";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &[
|
||||
"--conf",
|
||||
"--config",
|
||||
"--help",
|
||||
"--version",
|
||||
"-C",
|
||||
"-V",
|
||||
"-c",
|
||||
"-h",
|
||||
];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app --";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &["--conf", "--config", "--help", "--version"];
|
||||
assert_suggestions(expected, suggestions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_special_commands() {
|
||||
let mut completer = external_completion("special_commands.nu");
|
||||
|
||||
let input = "my-app some";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &[
|
||||
"my-app some_cmd",
|
||||
"my-app some-hidden-cmd",
|
||||
"my-app some-cmd-with-hyphens",
|
||||
];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app choice ";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &["first", "second"];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app -";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &[
|
||||
"--conf",
|
||||
"--config",
|
||||
"--help",
|
||||
"--version",
|
||||
"-C",
|
||||
"-V",
|
||||
"-c",
|
||||
"-h",
|
||||
];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app --";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &["--conf", "--config", "--help", "--version"];
|
||||
assert_suggestions(expected, suggestions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_quoting() {
|
||||
let mut completer = external_completion("quoting.nu");
|
||||
|
||||
let input = "my-app cmd-s";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &["my-app cmd-single-quotes"];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app --";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &[
|
||||
"--backslash",
|
||||
"--backticks",
|
||||
"--brackets",
|
||||
"--double-quotes",
|
||||
"--expansions",
|
||||
"--help",
|
||||
"--single-quotes",
|
||||
"--version",
|
||||
];
|
||||
assert_suggestions(expected, suggestions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_aliases() {
|
||||
let mut completer = external_completion("aliases.nu");
|
||||
|
||||
let input = "my-app -";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &[
|
||||
"--flag",
|
||||
"--flg",
|
||||
"--help",
|
||||
"--opt",
|
||||
"--option",
|
||||
"--version",
|
||||
"-F",
|
||||
"-O",
|
||||
"-V",
|
||||
"-f",
|
||||
"-h",
|
||||
"-o",
|
||||
];
|
||||
assert_suggestions(expected, suggestions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_sub_subcommands() {
|
||||
let mut completer = external_completion("sub_subcommands.nu");
|
||||
|
||||
let input = "my-app";
|
||||
let mut suggestions = completer.complete(input, input.len());
|
||||
suggestions.sort_by_key(|s| s.value.clone());
|
||||
let expected = &[
|
||||
"my-app",
|
||||
"my-app help",
|
||||
"my-app help help",
|
||||
"my-app help some_cmd",
|
||||
"my-app help some_cmd sub_cmd",
|
||||
"my-app help test",
|
||||
"my-app some_cmd",
|
||||
"my-app some_cmd help",
|
||||
"my-app some_cmd help help",
|
||||
"my-app some_cmd help sub_cmd",
|
||||
"my-app some_cmd sub_cmd",
|
||||
"my-app test",
|
||||
];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app some_cmd sub_cmd -";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &["--config", "--help", "--version", "-V", "-h"];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app some_cmd sub_cmd --config ";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &[
|
||||
"\"Lest quotes, aren't escaped.\"",
|
||||
"\"Second to trigger display of options\"",
|
||||
];
|
||||
assert_suggestions(expected, suggestions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_value_hint() {
|
||||
let mut completer = external_completion("value_hint.nu");
|
||||
|
||||
let input = "my-app -";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &[
|
||||
"--choice",
|
||||
"--cmd",
|
||||
"--cmd-name",
|
||||
"--dir",
|
||||
"--email",
|
||||
"--exe",
|
||||
"--file",
|
||||
"--help",
|
||||
"--host",
|
||||
"--other",
|
||||
"--path",
|
||||
"--unknown",
|
||||
"--url",
|
||||
"--user",
|
||||
"-H",
|
||||
"-c",
|
||||
"-d",
|
||||
"-e",
|
||||
"-f",
|
||||
"-h",
|
||||
"-p",
|
||||
"-u",
|
||||
];
|
||||
assert_suggestions(expected, suggestions);
|
||||
|
||||
let input = "my-app --choice ";
|
||||
let suggestions = completer.complete(input, input.len());
|
||||
let expected = &["bash", "fish", "zsh"];
|
||||
assert_suggestions(expected, suggestions);
|
||||
}
|
85
clap_complete_nushell/tests/nushell.rs
Normal file
85
clap_complete_nushell/tests/nushell.rs
Normal file
|
@ -0,0 +1,85 @@
|
|||
mod common;
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let name = "my-app";
|
||||
let cmd = common::basic_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/basic.nu",
|
||||
clap_complete_nushell::Nushell,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn feature_sample() {
|
||||
let name = "my-app";
|
||||
let cmd = common::feature_sample_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/feature_sample.nu",
|
||||
clap_complete_nushell::Nushell,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn special_commands() {
|
||||
let name = "my-app";
|
||||
let cmd = common::special_commands_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/special_commands.nu",
|
||||
clap_complete_nushell::Nushell,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quoting() {
|
||||
let name = "my-app";
|
||||
let cmd = common::quoting_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/quoting.nu",
|
||||
clap_complete_nushell::Nushell,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aliases() {
|
||||
let name = "my-app";
|
||||
let cmd = common::aliases_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/aliases.nu",
|
||||
clap_complete_nushell::Nushell,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sub_subcommands() {
|
||||
let name = "my-app";
|
||||
let cmd = common::sub_subcommands_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/sub_subcommands.nu",
|
||||
clap_complete_nushell::Nushell,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_hint() {
|
||||
let name = "my-app";
|
||||
let cmd = common::value_hint_command(name);
|
||||
common::assert_matches_path(
|
||||
"tests/snapshots/value_hint.nu",
|
||||
clap_complete_nushell::Nushell,
|
||||
cmd,
|
||||
name,
|
||||
);
|
||||
}
|
18
clap_complete_nushell/tests/snapshots/aliases.nu
Normal file
18
clap_complete_nushell/tests/snapshots/aliases.nu
Normal file
|
@ -0,0 +1,18 @@
|
|||
module completions {
|
||||
|
||||
# testing nushell completions
|
||||
export extern my-app [
|
||||
--flag(-f) # cmd flag
|
||||
--flg # cmd flag
|
||||
-F # cmd flag
|
||||
--option(-o): string # cmd option
|
||||
--opt: string # cmd option
|
||||
-O: string # cmd option
|
||||
positional?: string
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
use completions *
|
30
clap_complete_nushell/tests/snapshots/basic.nu
Normal file
30
clap_complete_nushell/tests/snapshots/basic.nu
Normal file
|
@ -0,0 +1,30 @@
|
|||
module completions {
|
||||
|
||||
export extern my-app [
|
||||
-c
|
||||
-v
|
||||
--help(-h) # Print help
|
||||
]
|
||||
|
||||
# Subcommand
|
||||
export extern "my-app test" [
|
||||
-d
|
||||
-c
|
||||
--help(-h) # Print help
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app help" [
|
||||
]
|
||||
|
||||
# Subcommand
|
||||
export extern "my-app help test" [
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app help help" [
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
use completions *
|
39
clap_complete_nushell/tests/snapshots/feature_sample.nu
Normal file
39
clap_complete_nushell/tests/snapshots/feature_sample.nu
Normal file
|
@ -0,0 +1,39 @@
|
|||
module completions {
|
||||
|
||||
def "nu-complete my-app choice" [] {
|
||||
[ "first" "second" ]
|
||||
}
|
||||
|
||||
# Tests completions
|
||||
export extern my-app [
|
||||
file?: string # some input file
|
||||
--config(-c) # some config file
|
||||
--conf # some config file
|
||||
-C # some config file
|
||||
choice?: string@"nu-complete my-app choice"
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# tests things
|
||||
export extern "my-app test" [
|
||||
--case: string # the case to test
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app help" [
|
||||
]
|
||||
|
||||
# tests things
|
||||
export extern "my-app help test" [
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app help help" [
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
use completions *
|
78
clap_complete_nushell/tests/snapshots/quoting.nu
Normal file
78
clap_complete_nushell/tests/snapshots/quoting.nu
Normal file
|
@ -0,0 +1,78 @@
|
|||
module completions {
|
||||
|
||||
export extern my-app [
|
||||
--single-quotes # Can be 'always', 'auto', or 'never'
|
||||
--double-quotes # Can be "always", "auto", or "never"
|
||||
--backticks # For more information see `echo test`
|
||||
--backslash # Avoid '\n'
|
||||
--brackets # List packages [filter]
|
||||
--expansions # Execute the shell command with $SHELL
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# Can be 'always', 'auto', or 'never'
|
||||
export extern "my-app cmd-single-quotes" [
|
||||
--help(-h) # Print help
|
||||
]
|
||||
|
||||
# Can be "always", "auto", or "never"
|
||||
export extern "my-app cmd-double-quotes" [
|
||||
--help(-h) # Print help
|
||||
]
|
||||
|
||||
# For more information see `echo test`
|
||||
export extern "my-app cmd-backticks" [
|
||||
--help(-h) # Print help
|
||||
]
|
||||
|
||||
# Avoid '\n'
|
||||
export extern "my-app cmd-backslash" [
|
||||
--help(-h) # Print help
|
||||
]
|
||||
|
||||
# List packages [filter]
|
||||
export extern "my-app cmd-brackets" [
|
||||
--help(-h) # Print help
|
||||
]
|
||||
|
||||
# Execute the shell command with $SHELL
|
||||
export extern "my-app cmd-expansions" [
|
||||
--help(-h) # Print help
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app help" [
|
||||
]
|
||||
|
||||
# Can be 'always', 'auto', or 'never'
|
||||
export extern "my-app help cmd-single-quotes" [
|
||||
]
|
||||
|
||||
# Can be "always", "auto", or "never"
|
||||
export extern "my-app help cmd-double-quotes" [
|
||||
]
|
||||
|
||||
# For more information see `echo test`
|
||||
export extern "my-app help cmd-backticks" [
|
||||
]
|
||||
|
||||
# Avoid '\n'
|
||||
export extern "my-app help cmd-backslash" [
|
||||
]
|
||||
|
||||
# List packages [filter]
|
||||
export extern "my-app help cmd-brackets" [
|
||||
]
|
||||
|
||||
# Execute the shell command with $SHELL
|
||||
export extern "my-app help cmd-expansions" [
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app help help" [
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
use completions *
|
67
clap_complete_nushell/tests/snapshots/special_commands.nu
Normal file
67
clap_complete_nushell/tests/snapshots/special_commands.nu
Normal file
|
@ -0,0 +1,67 @@
|
|||
module completions {
|
||||
|
||||
def "nu-complete my-app choice" [] {
|
||||
[ "first" "second" ]
|
||||
}
|
||||
|
||||
# Tests completions
|
||||
export extern my-app [
|
||||
file?: string # some input file
|
||||
--config(-c) # some config file
|
||||
--conf # some config file
|
||||
-C # some config file
|
||||
choice?: string@"nu-complete my-app choice"
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# tests things
|
||||
export extern "my-app test" [
|
||||
--case: string # the case to test
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# tests other things
|
||||
export extern "my-app some_cmd" [
|
||||
--config: string # the other case to test
|
||||
...path: string
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
export extern "my-app some-cmd-with-hyphens" [
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
export extern "my-app some-hidden-cmd" [
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app help" [
|
||||
]
|
||||
|
||||
# tests things
|
||||
export extern "my-app help test" [
|
||||
]
|
||||
|
||||
# tests other things
|
||||
export extern "my-app help some_cmd" [
|
||||
]
|
||||
|
||||
export extern "my-app help some-cmd-with-hyphens" [
|
||||
]
|
||||
|
||||
export extern "my-app help some-hidden-cmd" [
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app help help" [
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
use completions *
|
76
clap_complete_nushell/tests/snapshots/sub_subcommands.nu
Normal file
76
clap_complete_nushell/tests/snapshots/sub_subcommands.nu
Normal file
|
@ -0,0 +1,76 @@
|
|||
module completions {
|
||||
|
||||
def "nu-complete my-app choice" [] {
|
||||
[ "first" "second" ]
|
||||
}
|
||||
|
||||
# Tests completions
|
||||
export extern my-app [
|
||||
file?: string # some input file
|
||||
--config(-c) # some config file
|
||||
--conf # some config file
|
||||
-C # some config file
|
||||
choice?: string@"nu-complete my-app choice"
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# tests things
|
||||
export extern "my-app test" [
|
||||
--case: string # the case to test
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# top level subcommand
|
||||
export extern "my-app some_cmd" [
|
||||
--help(-h) # Print help
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
def "nu-complete my-app some_cmd sub_cmd config" [] {
|
||||
[ "\"Lest quotes, aren't escaped.\"" "\"Second to trigger display of options\"" ]
|
||||
}
|
||||
|
||||
# sub-subcommand
|
||||
export extern "my-app some_cmd sub_cmd" [
|
||||
--config: string@"nu-complete my-app some_cmd sub_cmd config" # the other case to test
|
||||
--help(-h) # Print help (see more with '--help')
|
||||
--version(-V) # Print version
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app some_cmd help" [
|
||||
]
|
||||
|
||||
# sub-subcommand
|
||||
export extern "my-app some_cmd help sub_cmd" [
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app some_cmd help help" [
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app help" [
|
||||
]
|
||||
|
||||
# tests things
|
||||
export extern "my-app help test" [
|
||||
]
|
||||
|
||||
# top level subcommand
|
||||
export extern "my-app help some_cmd" [
|
||||
]
|
||||
|
||||
# sub-subcommand
|
||||
export extern "my-app help some_cmd sub_cmd" [
|
||||
]
|
||||
|
||||
# Print this message or the help of the given subcommand(s)
|
||||
export extern "my-app help help" [
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
use completions *
|
27
clap_complete_nushell/tests/snapshots/value_hint.nu
Normal file
27
clap_complete_nushell/tests/snapshots/value_hint.nu
Normal file
|
@ -0,0 +1,27 @@
|
|||
module completions {
|
||||
|
||||
def "nu-complete my-app choice" [] {
|
||||
[ "bash" "fish" "zsh" ]
|
||||
}
|
||||
|
||||
export extern my-app [
|
||||
--choice: string@"nu-complete my-app choice"
|
||||
--unknown: string
|
||||
--other: string
|
||||
--path(-p): string
|
||||
--file(-f): string
|
||||
--dir(-d): string
|
||||
--exe(-e): string
|
||||
--cmd-name: string
|
||||
--cmd(-c): string
|
||||
command_with_args?: string
|
||||
--user(-u): string
|
||||
--host(-H): string
|
||||
--url: string
|
||||
--email: string
|
||||
--help(-h) # Print help
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
use completions *
|
Loading…
Reference in a new issue