clap_complete_command-0.6.1/.cargo_vcs_info.json0000644000000001360000000000100153450ustar { "git": { "sha1": "a61c45d93f7faa33d089d55abf672631ccca7b88" }, "path_in_vcs": "" }clap_complete_command-0.6.1/.gitignore000064400000000000000000000000251046102023000161220ustar 00000000000000/target/ /Cargo.lock clap_complete_command-0.6.1/Cargo.lock0000644000000244240000000000100133260ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "anstream" version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" [[package]] name = "anstyle-parse" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" dependencies = [ "windows-sys", ] [[package]] name = "anstyle-wincon" version = "3.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" dependencies = [ "anstyle", "windows-sys", ] [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "carapace_spec_clap" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c1b382d0ea2f304c9dba34f746284c7b6d231db48eae53b36d1e6eda1aba402" dependencies = [ "clap", "clap_complete", "indexmap 2.2.6", "serde", "serde_yaml", ] [[package]] name = "clap" version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64acc1846d54c1fe936a78dc189c34e28d3f5afc348403f28ecf53660b9b8462" dependencies = [ "clap_builder", "clap_derive", ] [[package]] name = "clap_builder" version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8393d67ba2e7bfaf28a23458e4e2b543cc73a99595511eb207fdb8aede942" dependencies = [ "anstream", "anstyle", "clap_lex", "strsim", ] [[package]] name = "clap_complete" version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b4be9c4c4b1f30b78d8a750e0822b6a6102d97e62061c583a6c1dea2dfb33ae" dependencies = [ "clap", ] [[package]] name = "clap_complete_command" version = "0.6.1" dependencies = [ "carapace_spec_clap", "clap", "clap_complete", "clap_complete_fig", "clap_complete_nushell", ] [[package]] name = "clap_complete_fig" version = "4.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46b30e010e669cd021e5004f3be26cff6b7c08d2a8a0d65b48d43a8cc0efd6c3" dependencies = [ "clap", "clap_complete", ] [[package]] name = "clap_complete_nushell" version = "4.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1accf1b463dee0d3ab2be72591dccdab8bef314958340447c882c4c72acfe2a3" dependencies = [ "clap", "clap_complete", ] [[package]] name = "clap_derive" version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085" dependencies = [ "heck", "proc-macro2", "quote", "syn 2.0.71", ] [[package]] name = "clap_lex" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" [[package]] name = "colorchoice" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" [[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "hashbrown" version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "indexmap" version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" dependencies = [ "autocfg", "hashbrown 0.12.3", ] [[package]] name = "indexmap" version = "2.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ "equivalent", "hashbrown 0.14.5", "serde", ] [[package]] name = "is_terminal_polyfill" version = "1.70.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" [[package]] name = "itoa" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" [[package]] name = "proc-macro2" version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "ryu" version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" [[package]] name = "serde" version = "1.0.154" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8cdd151213925e7f1ab45a9bbfb129316bd00799784b174b7cc7bcd16961c49e" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.154" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fc80d722935453bcafdc2c9a73cd6fac4dc1938f0346035d84bf99fa9e33217" dependencies = [ "proc-macro2", "quote", "syn 1.0.105", ] [[package]] name = "serde_yaml" version = "0.9.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f82e6c8c047aa50a7328632d067bcae6ef38772a79e28daf32f735e0e4f3dd10" dependencies = [ "indexmap 1.9.2", "itoa", "ryu", "serde", "unsafe-libyaml", ] [[package]] name = "strsim" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" version = "1.0.105" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "syn" version = "2.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "unicode-ident" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" [[package]] name = "unsafe-libyaml" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad2024452afd3874bf539695e04af6732ba06517424dbf958fdb16a01f3bef6c" [[package]] name = "utf8parse" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" clap_complete_command-0.6.1/Cargo.toml0000644000000026550000000000100133530ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "clap_complete_command" version = "0.6.1" description = "Reduces boilerplate for adding a shell completion command to Clap" documentation = "https://docs.rs/clap_complete_command" readme = "README.md" keywords = [ "clap", "cli", "completion", ] categories = ["command-line-interface"] license = "MIT" repository = "https://github.com/nihaals/clap-complete-command" [package.metadata.docs.rs] all-features = true rustdoc-args = [ "--cfg", "docsrs", ] [dependencies.carapace_spec_clap] version = "1" optional = true [dependencies.clap] version = "4" [dependencies.clap_complete] version = "4" [dependencies.clap_complete_fig] version = "4" optional = true [dependencies.clap_complete_nushell] version = "4" optional = true [dev-dependencies.clap] version = "4" features = ["derive"] [features] carapace = ["dep:carapace_spec_clap"] default = ["nushell"] fig = ["dep:clap_complete_fig"] nushell = ["dep:clap_complete_nushell"] clap_complete_command-0.6.1/Cargo.toml.orig000064400000000000000000000015711046102023000170300ustar 00000000000000[package] name = "clap_complete_command" version = "0.6.1" edition = "2021" description = "Reduces boilerplate for adding a shell completion command to Clap" repository = "https://github.com/nihaals/clap-complete-command" documentation = "https://docs.rs/clap_complete_command" license = "MIT" keywords = ["clap", "cli", "completion"] categories = ["command-line-interface"] [features] default = ["nushell"] carapace = ["dep:carapace_spec_clap"] fig = ["dep:clap_complete_fig"] nushell = ["dep:clap_complete_nushell"] [dependencies] clap = "4" clap_complete = "4" carapace_spec_clap = { version = "1", optional = true } clap_complete_fig = { version = "4", optional = true } clap_complete_nushell = { version = "4", optional = true } [dev-dependencies] clap = { version = "4", features = ["derive"] } [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] clap_complete_command-0.6.1/LICENSE000064400000000000000000000021201046102023000151350ustar 00000000000000MIT License Copyright (c) 2015-2016 Kevin B. Knapp Copyright (c) Nihaal Sangha 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. clap_complete_command-0.6.1/README.md000064400000000000000000000045701046102023000154220ustar 00000000000000# clap-complete-command [![crates.io](https://img.shields.io/crates/v/clap_complete_command)](https://crates.io/crates/clap_complete_command) Reduces boilerplate for adding a shell completion command to Clap ## Compatible clap versions | `clap` version | `clap_complete_command` version | | -------------- | ------------------------------- | | v3 | v0.1, v0.2, v0.3 | | v4 | v0.4, v0.5, v0.6 | ## Examples ### Derive ```rust use clap::{CommandFactory, Parser, Subcommand}; #[derive(Parser)] struct Cli { #[command(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { /// Generate shell completions Completions { /// The shell to generate the completions for #[arg(value_enum)] shell: clap_complete_command::Shell, }, } fn main() { let cli = Cli::parse(); match cli.command { // e.g. `$ cli completions bash` Commands::Completions { shell } => { shell.generate(&mut Cli::command(), &mut std::io::stdout()); } } } ``` ### Builder ```rust use clap::{Arg, Command}; fn build_cli() -> Command { Command::new(env!("CARGO_PKG_NAME")) .subcommand_required(true) .subcommand( Command::new("completions") .about("Generate shell completions") .arg( Arg::new("shell") .value_name("SHELL") .help("The shell to generate the completions for") .required(true) .value_parser( clap::builder::EnumValueParser::::new(), ), ), ) } fn main() { let matches = build_cli().get_matches(); match matches.subcommand() { Some(("completions", sub_matches)) => { // e.g. `$ cli completions bash` if let Some(shell) = sub_matches.get_one::("shell") { let mut command = build_cli(); shell.generate(&mut command, &mut std::io::stdout()); } } _ => { unreachable!("Exhausted list of subcommands and `subcommand_required` prevents `None`") } } } ``` ## Supported shells The supported shells can be seen in `clap_complete_command::Shell`. clap_complete_command-0.6.1/examples/builder.rs000064400000000000000000000022231046102023000177460ustar 00000000000000use clap::{Arg, Command}; fn build_cli() -> Command { Command::new(env!("CARGO_PKG_NAME")) .subcommand_required(true) .subcommand( Command::new("completions") .about("Generate shell completions") .arg( Arg::new("shell") .value_name("SHELL") .help("The shell to generate the completions for") .required(true) .value_parser( clap::builder::EnumValueParser::::new(), ), ), ) } fn main() { let matches = build_cli().get_matches(); match matches.subcommand() { Some(("completions", sub_matches)) => { if let Some(shell) = sub_matches.get_one::("shell") { let mut command = build_cli(); shell.generate(&mut command, &mut std::io::stdout()); } } _ => { unreachable!("Exhausted list of subcommands and `subcommand_required` prevents `None`") } } } clap_complete_command-0.6.1/examples/derive.rs000064400000000000000000000010611046102023000175750ustar 00000000000000use clap::{CommandFactory, Parser, Subcommand}; #[derive(Parser)] struct Cli { #[command(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { /// Generate shell completions Completions { /// The shell to generate the completions for #[arg(value_enum)] shell: clap_complete_command::Shell, }, } fn main() { let cli = Cli::parse(); match cli.command { Commands::Completions { shell } => { shell.generate(&mut Cli::command(), &mut std::io::stdout()); } } } clap_complete_command-0.6.1/src/lib.rs000064400000000000000000000267601046102023000160530ustar 00000000000000//! # Examples //! //! ## Derive //! //! ```no_run //! use clap::{CommandFactory, Parser, Subcommand}; //! //! #[derive(Parser)] //! struct Cli { //! #[command(subcommand)] //! command: Commands, //! } //! //! #[derive(Subcommand)] //! enum Commands { //! /// Generate shell completions //! Completions { //! /// The shell to generate the completions for //! #[arg(value_enum)] //! shell: clap_complete_command::Shell, //! }, //! } //! //! let cli = Cli::parse(); //! //! match cli.command { //! // e.g. `$ cli completions bash` //! Commands::Completions { shell } => { //! shell.generate(&mut Cli::command(), &mut std::io::stdout()); //! } //! } //! ``` //! //! ## Builder //! //! ```no_run //! use clap::{Arg, Command}; //! //! fn build_cli() -> Command { //! Command::new(env!("CARGO_PKG_NAME")) //! .subcommand_required(true) //! .subcommand( //! Command::new("completions") //! .about("Generate shell completions") //! .arg( //! Arg::new("shell") //! .value_name("SHELL") //! .help("The shell to generate the completions for") //! .required(true) //! .value_parser( //! clap::builder::EnumValueParser::::new(), //! ), //! ), //! ) //! } //! //! let matches = build_cli().get_matches(); //! //! match matches.subcommand() { //! Some(("completions", sub_matches)) => { //! // e.g. `$ cli completions bash` //! if let Some(shell) = sub_matches.get_one::("shell") { //! let mut command = build_cli(); //! shell.generate(&mut command, &mut std::io::stdout()); //! } //! } //! _ => { //! unreachable!("Exhausted list of subcommands and `subcommand_required` prevents `None`") //! } //! } //! ``` #![cfg_attr(docsrs, feature(doc_auto_cfg))] #![warn(clippy::cast_lossless)] #![warn(clippy::cast_possible_wrap)] #![warn(clippy::default_trait_access)] #![warn(clippy::else_if_without_else)] #![warn(clippy::empty_enum)] #![warn(clippy::empty_line_after_outer_attr)] #![warn(clippy::enum_glob_use)] #![warn(clippy::equatable_if_let)] #![warn(clippy::float_cmp)] #![warn(clippy::fn_params_excessive_bools)] #![warn(clippy::get_unwrap)] #![warn(clippy::inefficient_to_string)] #![warn(clippy::integer_division)] #![warn(clippy::let_unit_value)] #![warn(clippy::linkedlist)] #![warn(clippy::lossy_float_literal)] #![warn(clippy::macro_use_imports)] #![warn(clippy::manual_assert)] #![warn(clippy::manual_ok_or)] #![warn(clippy::many_single_char_names)] #![warn(clippy::map_unwrap_or)] #![warn(clippy::match_bool)] #![warn(clippy::match_on_vec_items)] #![warn(clippy::match_same_arms)] #![warn(clippy::match_wild_err_arm)] #![warn(clippy::match_wildcard_for_single_variants)] #![warn(clippy::mem_forget)] #![warn(clippy::missing_const_for_fn)] #![warn(clippy::must_use_candidate)] #![warn(clippy::mut_mut)] #![warn(clippy::negative_feature_names)] #![warn(non_ascii_idents)] #![warn(clippy::option_option)] #![warn(clippy::redundant_feature_names)] #![warn(clippy::redundant_pub_crate)] #![warn(clippy::single_match_else)] #![warn(clippy::str_to_string)] #![warn(clippy::string_to_string)] #![warn(clippy::trait_duplication_in_bounds)] #![warn(clippy::unused_async)] #![warn(clippy::unused_self)] #![warn(clippy::use_self)] #![warn(clippy::wildcard_dependencies)] #![warn(clippy::wildcard_imports)] #![warn(clippy::zero_sized_map_values)] use std::{ffi::OsString, path::PathBuf}; use clap::ValueEnum; /// A [`clap::ValueEnum`] for available shell completions. /// /// # Examples /// /// ## Derive /// /// ```no_run /// use clap::{Parser, Subcommand}; /// /// #[derive(Parser)] /// struct Cli { /// #[command(subcommand)] /// command: Commands, /// } /// /// #[derive(Subcommand)] /// enum Commands { /// Completions { /// #[arg(value_enum)] /// shell: clap_complete_command::Shell, /// }, /// } /// ``` /// /// ## Builder /// /// ```no_run /// use clap::{Arg, Command}; /// /// fn build_cli() -> Command { /// Command::new(env!("CARGO_PKG_NAME")) /// .subcommand_required(true) /// .subcommand( /// Command::new("completions") /// .about("Generate shell completions") /// .arg( /// Arg::new("shell") /// .value_name("SHELL") /// .help("The shell to generate the completions for") /// .required(true) /// .value_parser( /// clap::builder::EnumValueParser::::new(), /// ), /// ), /// ) /// } /// /// let matches = build_cli().get_matches(); /// /// match matches.subcommand() { /// Some(("completions", sub_matches)) => { /// if let Some(shell) = sub_matches.get_one::("shell") { /// // ... /// } /// } /// _ => { /// unreachable!("Exhausted list of subcommands and `subcommand_required` prevents `None`") /// } /// } /// ``` #[derive(Clone, Copy, Debug)] #[non_exhaustive] pub enum Shell { /// Bourne Again SHell (bash) Bash, /// Carapace spec #[cfg(feature = "carapace")] Carapace, /// Elvish shell Elvish, /// Fig #[cfg(feature = "fig")] Fig, /// Friendly Interactive SHell (fish) Fish, /// NUshell (nu) #[cfg(feature = "nushell")] Nu, /// PowerShell PowerShell, /// Z SHell (zsh) Zsh, } impl clap_complete::Generator for Shell { fn file_name(&self, name: &str) -> String { match self { Self::Bash => clap_complete::Shell::Bash.file_name(name), Self::Elvish => clap_complete::Shell::Elvish.file_name(name), Self::Fish => clap_complete::Shell::Fish.file_name(name), Self::PowerShell => clap_complete::Shell::PowerShell.file_name(name), Self::Zsh => clap_complete::Shell::Zsh.file_name(name), #[cfg(feature = "carapace")] Self::Carapace => carapace_spec_clap::Spec.file_name(name), #[cfg(feature = "fig")] Self::Fig => clap_complete_fig::Fig.file_name(name), #[cfg(feature = "nushell")] Self::Nu => clap_complete_nushell::Nushell.file_name(name), } } fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) { match self { Self::Bash => clap_complete::Shell::Bash.generate(cmd, buf), Self::Elvish => clap_complete::Shell::Elvish.generate(cmd, buf), Self::Fish => clap_complete::Shell::Fish.generate(cmd, buf), Self::PowerShell => clap_complete::Shell::PowerShell.generate(cmd, buf), Self::Zsh => clap_complete::Shell::Zsh.generate(cmd, buf), #[cfg(feature = "carapace")] Self::Carapace => carapace_spec_clap::Spec.generate(cmd, buf), #[cfg(feature = "fig")] Self::Fig => clap_complete_fig::Fig.generate(cmd, buf), #[cfg(feature = "nushell")] Self::Nu => clap_complete_nushell::Nushell.generate(cmd, buf), } } } impl Shell { /// See [`clap_complete::generate()`]. /// /// The `command`'s bin name is used as the completion's bin name. /// If the `command`'s bin name is not set, it will be set to the `command`'s name. pub fn generate(self, command: &mut clap::Command, buffer: &mut dyn std::io::Write) { let bin_name = command .get_bin_name() .unwrap_or_else(|| command.get_name()) .to_owned(); clap_complete::generate(self, command, bin_name, buffer) } /// See [`clap_complete::generate_to()`]. /// /// The `command`'s bin name is used as the completion's bin name. /// If the `command`'s bin name is not set, it will be set to the `command`'s name. pub fn generate_to( self, command: &mut clap::Command, out_dir: S, ) -> Result where S: Into, { let bin_name = command .get_bin_name() .unwrap_or_else(|| command.get_name()) .to_owned(); clap_complete::generate_to(self, command, bin_name, out_dir) } } // Hand-rolled to avoid depending on Clap's `derive` feature impl ValueEnum for Shell { fn value_variants<'a>() -> &'a [Self] { &[ Self::Bash, #[cfg(feature = "carapace")] Self::Carapace, Self::Elvish, #[cfg(feature = "fig")] Self::Fig, Self::Fish, #[cfg(feature = "nushell")] Self::Nu, Self::PowerShell, Self::Zsh, ] } fn to_possible_value(&self) -> Option { Some(match self { Self::Bash => clap::builder::PossibleValue::new("bash"), #[cfg(feature = "carapace")] Self::Carapace => clap::builder::PossibleValue::new("carapace"), Self::Elvish => clap::builder::PossibleValue::new("elvish"), #[cfg(feature = "fig")] Self::Fig => clap::builder::PossibleValue::new("fig"), Self::Fish => clap::builder::PossibleValue::new("fish"), #[cfg(feature = "nushell")] Self::Nu => clap::builder::PossibleValue::new("nushell"), Self::PowerShell => clap::builder::PossibleValue::new("powershell"), Self::Zsh => clap::builder::PossibleValue::new("zsh"), }) } } #[cfg(test)] mod tests { use super::*; use clap::ValueEnum; macro_rules! check_shell_value_test { ($test_name:ident, $shell:expr, $value:expr) => { #[test] fn $test_name() { assert_eq!($shell.to_possible_value().unwrap().get_name(), $value); } }; } check_shell_value_test!(test_shell_value_bash, Shell::Bash, "bash"); #[cfg(feature = "carapace")] check_shell_value_test!(test_shell_value_carapace, Shell::Carapace, "carapace"); check_shell_value_test!(test_shell_value_elvish, Shell::Elvish, "elvish"); #[cfg(feature = "fig")] check_shell_value_test!(test_shell_value_fig, Shell::Fig, "fig"); check_shell_value_test!(test_shell_value_fish, Shell::Fish, "fish"); #[cfg(feature = "nushell")] check_shell_value_test!(test_shell_value_nushell, Shell::Nu, "nushell"); check_shell_value_test!(test_shell_value_powershell, Shell::PowerShell, "powershell"); check_shell_value_test!(test_shell_value_zsh, Shell::Zsh, "zsh"); #[test] fn check_order() { let names = Shell::value_variants() .iter() .map(|shell| shell.to_possible_value().unwrap().get_name().to_owned()) .collect::>(); let mut sorted = names.clone(); sorted.sort_unstable(); assert_eq!(names, sorted); let correct_order = [ ("bash", true), ("carapace", cfg!(feature = "carapace")), ("elvish", true), ("fig", cfg!(feature = "fig")), ("fish", true), ("nushell", cfg!(feature = "nushell")), ("powershell", true), ("zsh", true), ] .iter() .filter(|(_, enabled)| *enabled) .map(|(shell, _)| *shell) .collect::>(); assert_eq!(names, correct_order); } }