clap_complete-3.1.1/.cargo_vcs_info.json0000644000000001530000000000100136440ustar { "git": { "sha1": "1e286f3dc294ca190a533b3192b5f07321659d44" }, "path_in_vcs": "clap_complete" }clap_complete-3.1.1/Cargo.lock0000644000000200300000000000100116130ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "addr2line" version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b" dependencies = [ "gimli", ] [[package]] name = "adler" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" [[package]] name = "ansi_term" version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" dependencies = [ "winapi", ] [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "backtrace" version = "0.3.64" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e121dee8023ce33ab248d9ce1493df03c3b38a659b240096fcbd7048ff9c31f" dependencies = [ "addr2line", "cc", "cfg-if", "libc", "miniz_oxide", "object", "rustc-demangle", ] [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cc" version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" version = "3.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "759e998e8d87a8b7a2c11a0e029d8772632fd3346ee4c770fb42f7fbbab76180" dependencies = [ "backtrace", "bitflags", "clap_derive", "indexmap", "lazy_static", "os_str_bytes", "textwrap", ] [[package]] name = "clap_complete" version = "3.1.1" dependencies = [ "clap", "pretty_assertions", ] [[package]] name = "clap_derive" version = "3.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da95d038ede1a964ce99f49cbe27a7fb538d1da595e4b4f70b8c8f338d17bf16" dependencies = [ "heck", "proc-macro-error", "proc-macro2", "quote", "syn", ] [[package]] name = "ctor" version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccc0a48a9b826acdf4028595adc9db92caea352f7af011a3034acd172a52a0aa" dependencies = [ "quote", "syn", ] [[package]] name = "diff" version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" [[package]] name = "gimli" version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" [[package]] name = "hashbrown" version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" [[package]] name = "heck" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" [[package]] name = "indexmap" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" dependencies = [ "autocfg", "hashbrown", ] [[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" version = "0.2.119" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" [[package]] name = "memchr" version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "miniz_oxide" version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" dependencies = [ "adler", "autocfg", ] [[package]] name = "object" version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67ac1d3f9a1d3616fd9a60c8d74296f22406a238b6a72f5cc1e6f314df4ffbf9" dependencies = [ "memchr", ] [[package]] name = "os_str_bytes" version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" dependencies = [ "memchr", ] [[package]] name = "output_vt100" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66" dependencies = [ "winapi", ] [[package]] name = "pretty_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76d5b548b725018ab5496482b45cb8bef21e9fed1858a6d674e3a8a0f0bb5d50" dependencies = [ "ansi_term", "ctor", "diff", "output_vt100", ] [[package]] name = "proc-macro-error" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", "proc-macro2", "quote", "syn", "version_check", ] [[package]] name = "proc-macro-error-attr" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ "proc-macro2", "quote", "version_check", ] [[package]] name = "proc-macro2" version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" dependencies = [ "unicode-xid", ] [[package]] name = "quote" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" dependencies = [ "proc-macro2", ] [[package]] name = "rustc-demangle" version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" [[package]] name = "syn" version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" dependencies = [ "proc-macro2", "quote", "unicode-xid", ] [[package]] name = "textwrap" version = "0.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" [[package]] name = "unicode-xid" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" clap_complete-3.1.1/Cargo.toml0000644000000044140000000000100116460ustar # 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 = "2018" name = "clap_complete" version = "3.1.1" include = ["src/**/*", "Cargo.toml", "LICENSE-*", "README.md"] description = "Generate shell completion scripts for your clap::Command" documentation = "https://docs.rs/clap_complete" readme = "README.md" keywords = ["clap", "cli", "completion", "bash"] categories = ["command-line-interface"] license = "MIT OR Apache-2.0" repository = "https://github.com/clap-rs/clap/tree/master/clap_complete" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] [[package.metadata.release.pre-release-replacements]] file = "CHANGELOG.md" min = 1 replace = "{{version}}" search = "Unreleased" [[package.metadata.release.pre-release-replacements]] exactly = 1 file = "CHANGELOG.md" replace = "...{{tag_name}}" search = "\\.\\.\\.HEAD" [[package.metadata.release.pre-release-replacements]] file = "CHANGELOG.md" min = 1 replace = "{{date}}" search = "ReleaseDate" [[package.metadata.release.pre-release-replacements]] exactly = 1 file = "CHANGELOG.md" replace = "\n## [Unreleased] - ReleaseDate\n" search = "" [[package.metadata.release.pre-release-replacements]] exactly = 1 file = "CHANGELOG.md" replace = "\n[Unreleased]: https://github.com/clap-rs/clap/compare/{{tag_name}}...HEAD" search = "" [[package.metadata.release.pre-release-replacements]] exactly = 4 file = "README.md" prerelease = true replace = "github.com/clap-rs/clap/blob/{{tag_name}}/" search = "github.com/clap-rs/clap/blob/[^/]+/" [lib] bench = false [dependencies.clap] version = "3.1.0" features = ["std"] default-features = false [dev-dependencies.clap] version = "3.1.0" features = ["std", "derive"] default-features = false [dev-dependencies.pretty_assertions] version = "1.0" [features] debug = ["clap/debug"] default = [] clap_complete-3.1.1/Cargo.toml.orig000064400000000000000000000031140072674642500153530ustar 00000000000000[package] name = "clap_complete" version = "3.1.1" edition = "2018" include = [ "src/**/*", "Cargo.toml", "LICENSE-*", "README.md" ] description = "Generate shell completion scripts for your clap::Command" repository = "https://github.com/clap-rs/clap/tree/master/clap_complete" documentation = "https://docs.rs/clap_complete" keywords = [ "clap", "cli", "completion", "bash", ] categories = ["command-line-interface"] license = "MIT OR Apache-2.0" readme = "README.md" [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="", replace="\n## [Unreleased] - ReleaseDate\n", exactly=1}, {file="CHANGELOG.md", search="", replace="\n[Unreleased]: https://github.com/clap-rs/clap/compare/{{tag_name}}...HEAD", exactly=1}, {file="README.md", search="github.com/clap-rs/clap/blob/[^/]+/", replace="github.com/clap-rs/clap/blob/{{tag_name}}/", exactly=4, prerelease = true}, ] [lib] bench = false [dependencies] clap = { path = "../", version = "3.1.0", default-features = false, features = ["std"] } [dev-dependencies] pretty_assertions = "1.0" clap = { path = "../", version = "3.1.0", default-features = false, features = ["std", "derive"] } [features] default = [] debug = ["clap/debug"] [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] clap_complete-3.1.1/LICENSE-APACHE000064400000000000000000000261350072674642500144200ustar 00000000000000 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. clap_complete-3.1.1/LICENSE-MIT000064400000000000000000000020760072674642500141260ustar 00000000000000The MIT License (MIT) Copyright (c) 2015-2016 Kevin B. Knapp 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-3.1.1/README.md000064400000000000000000000022610072674642500137450ustar 00000000000000 # clap_complete > **Shell completion generation for `clap`** [![Crates.io](https://img.shields.io/crates/v/clap_complete?style=flat-square)](https://crates.io/crates/clap_complete) [![Crates.io](https://img.shields.io/crates/d/clap_complete?style=flat-square)](https://crates.io/crates/clap_complete) [![License](https://img.shields.io/badge/license-Apache%202.0-blue?style=flat-square)](https://github.com/clap-rs/clap/blob/clap_complete-v3.1.1/LICENSE-APACHE) [![License](https://img.shields.io/badge/license-MIT-blue?style=flat-square)](https://github.com/clap-rs/clap/blob/clap_complete-v3.1.1/LICENSE-MIT) Dual-licensed under [Apache 2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT). 1. [About](#about) 2. [API Reference](https://docs.rs/clap_complete) 3. [Questions & Discussions](https://github.com/clap-rs/clap/discussions) 4. [CONTRIBUTING](https://github.com/clap-rs/clap/blob/clap_complete-v3.1.1/clap_complete/CONTRIBUTING.md) 5. [Sponsors](https://github.com/clap-rs/clap/blob/clap_complete-v3.1.1/README.md#sponsors) ## About ### Related Projects - [clap_complete_fig](https://crates.io/crates/clap_complete_fig) for [fig](https://fig.io/) shell completion support clap_complete-3.1.1/src/generator/mod.rs000064400000000000000000000150240072674642500163710ustar 00000000000000//! Shell completion machinery pub mod utils; use std::ffi::OsString; use std::fs::File; use std::io::Error; use std::io::Write; use std::path::PathBuf; use clap::Command; /// Generator trait which can be used to write generators pub trait Generator { /// Returns the file name that is created when this generator is called during compile time. /// /// # Panics /// /// May panic when called outside of the context of [`generate`] or [`generate_to`] /// /// # Examples /// /// ``` /// # use std::io::Write; /// # use clap::Command; /// use clap_complete::Generator; /// /// pub struct Fish; /// /// impl Generator for Fish { /// # fn generate(&self, cmd: &Command, buf: &mut dyn Write) {} /// fn file_name(&self, name: &str) -> String { /// format!("{}.fish", name) /// } /// } /// ``` fn file_name(&self, name: &str) -> String; /// Generates output out of [`clap::Command`](Command). /// /// # Panics /// /// May panic when called outside of the context of [`generate`] or [`generate_to`] /// /// # Examples /// /// The following example generator displays the [`clap::Command`](Command) /// as if it is printed using [`std::println`]. /// /// ``` /// use std::{io::Write, fmt::write}; /// use clap::Command; /// use clap_complete::Generator; /// /// pub struct ClapDebug; /// /// impl Generator for ClapDebug { /// fn generate(&self, cmd: &Command, buf: &mut dyn Write) { /// write!(buf, "{}", cmd).unwrap(); /// } /// # fn file_name(&self, name: &str) -> String { /// # name.into() /// # } /// } /// ``` fn generate(&self, cmd: &Command, buf: &mut dyn Write); } /// Generate a completions file for a specified shell at compile-time. /// /// **NOTE:** to generate the file at compile time you must use a `build.rs` "Build Script" or a /// [`cargo-xtask`](https://github.com/matklad/cargo-xtask) /// /// # Examples /// /// The following example generates a bash completion script via a `build.rs` script. In this /// simple example, we'll demo a very small application with only a single subcommand and two /// args. Real applications could be many multiple levels deep in subcommands, and have tens or /// potentially hundreds of arguments. /// /// First, it helps if we separate out our `Command` definition into a separate file. Whether you /// do this as a function, or bare Command definition is a matter of personal preference. /// /// ``` /// // src/cli.rs /// /// use clap::{Command, Arg}; /// /// pub fn build_cli() -> Command<'static> { /// Command::new("compl") /// .about("Tests completions") /// .arg(Arg::new("file") /// .help("some input file")) /// .subcommand(Command::new("test") /// .about("tests things") /// .arg(Arg::new("case") /// .long("case") /// .takes_value(true) /// .help("the case to test"))) /// } /// ``` /// /// In our regular code, we can simply call this `build_cli()` function, then call /// `get_matches()`, or any of the other normal methods directly after. For example: /// /// ```ignore /// // src/main.rs /// /// mod cli; /// /// fn main() { /// let _m = cli::build_cli().get_matches(); /// /// // normal logic continues... /// } /// ``` /// /// Next, we set up our `Cargo.toml` to use a `build.rs` build script. /// /// ```toml /// # Cargo.toml /// build = "build.rs" /// /// [dependencies] /// clap = "*" /// /// [build-dependencies] /// clap = "*" /// clap_complete = "*" /// ``` /// /// Next, we place a `build.rs` in our project root. /// /// ```ignore /// use clap_complete::{generate_to, shells::Bash}; /// use std::env; /// use std::io::Error; /// /// include!("src/cli.rs"); /// /// fn main() -> Result<(), Error> { /// let outdir = match env::var_os("OUT_DIR") { /// None => return Ok(()), /// Some(outdir) => outdir, /// }; /// /// let mut cmd = build_cli(); /// let path = generate_to( /// Bash, /// &mut cmd, // We need to specify what generator to use /// "myapp", // We need to specify the bin name manually /// outdir, // We need to specify where to write to /// )?; /// /// println!("cargo:warning=completion file is generated: {:?}", path); /// /// Ok(()) /// } /// ``` /// /// Now, once we compile there will be a `{bin_name}.bash` file in the directory. /// Assuming we compiled with debug mode, it would be somewhere similar to /// `/target/debug/build/myapp-/out/myapp.bash`. /// /// **NOTE:** Please look at the individual [shells][crate::shells] /// to see the name of the files generated. pub fn generate_to( gen: G, cmd: &mut clap::Command, bin_name: S, out_dir: T, ) -> Result where G: Generator, S: Into, T: Into, { cmd.set_bin_name(bin_name); let out_dir = PathBuf::from(out_dir.into()); let file_name = gen.file_name(cmd.get_bin_name().unwrap()); let path = out_dir.join(file_name); let mut file = File::create(&path)?; _generate::(gen, cmd, &mut file); Ok(path) } /// Generate a completions file for a specified shell at runtime. /// /// Until `cargo install` can install extra files like a completion script, this may be /// used e.g. in a command that outputs the contents of the completion script, to be /// redirected into a file by the user. /// /// # Examples /// /// Assuming a separate `cli.rs` like the [example above](generate_to()), /// we can let users generate a completion script using a command: /// /// ```ignore /// // src/main.rs /// /// mod cli; /// use std::io; /// use clap_complete::{generate, shells::Bash}; /// /// fn main() { /// let matches = cli::build_cli().get_matches(); /// /// if matches.is_present("generate-bash-completions") { /// generate(Bash, &mut cli::build_cli(), "myapp", &mut io::stdout()); /// } /// /// // normal logic continues... /// } /// /// ``` /// /// Usage: /// /// ```shell /// $ myapp generate-bash-completions > /usr/share/bash-completion/completions/myapp.bash /// ``` pub fn generate(gen: G, cmd: &mut clap::Command, bin_name: S, buf: &mut dyn Write) where G: Generator, S: Into, { cmd.set_bin_name(bin_name); _generate::(gen, cmd, buf) } fn _generate(gen: G, cmd: &mut clap::Command, buf: &mut dyn Write) where G: Generator, S: Into, { cmd._build_all(); gen.generate(cmd, buf) } clap_complete-3.1.1/src/generator/utils.rs000064400000000000000000000200570072674642500167540ustar 00000000000000//! Helpers for writing generators use clap::{Arg, Command}; /// Gets all subcommands including child subcommands in the form of `("name", "bin_name")`. /// /// Subcommand `rustup toolchain install` would be converted to /// `("install", "rustup toolchain install")`. pub fn all_subcommands(cmd: &Command) -> Vec<(String, String)> { let mut subcmds: Vec<_> = subcommands(cmd); for sc_v in cmd.get_subcommands().map(all_subcommands) { subcmds.extend(sc_v); } subcmds } /// Finds the subcommand [`clap::Command`] from the given [`clap::Command`] with the given path. /// /// **NOTE:** `path` should not contain the root `bin_name`. pub fn find_subcommand_with_path<'help, 'cmd>( p: &'cmd Command<'help>, path: Vec<&str>, ) -> &'cmd Command<'help> { let mut cmd = p; for sc in path { cmd = cmd.find_subcommand(sc).unwrap(); } cmd } /// Gets subcommands of [`clap::Command`] in the form of `("name", "bin_name")`. /// /// Subcommand `rustup toolchain install` would be converted to /// `("install", "rustup toolchain install")`. pub fn subcommands(p: &Command) -> Vec<(String, String)> { debug!("subcommands: name={}", p.get_name()); debug!("subcommands: Has subcommands...{:?}", p.has_subcommands()); let mut subcmds = vec![]; if !p.has_subcommands() { return subcmds; } for sc in p.get_subcommands() { let sc_bin_name = sc.get_bin_name().unwrap(); debug!( "subcommands:iter: name={}, bin_name={}", sc.get_name(), sc_bin_name ); subcmds.push((sc.get_name().to_string(), sc_bin_name.to_string())); } subcmds } /// Gets all the short options, their visible aliases and flags of a [`clap::Command`]. /// Includes `h` and `V` depending on the [`clap::AppSettings`]. pub fn shorts_and_visible_aliases(p: &Command) -> Vec { debug!("shorts: name={}", p.get_name()); p.get_arguments() .filter_map(|a| { if !a.is_positional() { if a.get_visible_short_aliases().is_some() && a.get_short().is_some() { let mut shorts_and_visible_aliases = a.get_visible_short_aliases().unwrap(); shorts_and_visible_aliases.push(a.get_short().unwrap()); Some(shorts_and_visible_aliases) } else if a.get_visible_short_aliases().is_none() && a.get_short().is_some() { Some(vec![a.get_short().unwrap()]) } else { None } } else { None } }) .flatten() .collect() } /// Gets all the long options, their visible aliases and flags of a [`clap::Command`]. /// Includes `help` and `version` depending on the [`clap::AppSettings`]. pub fn longs_and_visible_aliases(p: &Command) -> Vec { debug!("longs: name={}", p.get_name()); p.get_arguments() .filter_map(|a| { if !a.is_positional() { if a.get_visible_aliases().is_some() && a.get_long().is_some() { let mut visible_aliases: Vec<_> = a .get_visible_aliases() .unwrap() .into_iter() .map(|s| s.to_string()) .collect(); visible_aliases.push(a.get_long().unwrap().to_string()); Some(visible_aliases) } else if a.get_visible_aliases().is_none() && a.get_long().is_some() { Some(vec![a.get_long().unwrap().to_string()]) } else { None } } else { None } }) .flatten() .collect() } /// Gets all the flags of a [`clap::Command`](Command). /// Includes `help` and `version` depending on the [`clap::AppSettings`]. pub fn flags<'help>(p: &Command<'help>) -> Vec> { debug!("flags: name={}", p.get_name()); p.get_arguments() .filter(|a| !a.is_takes_value_set() && !a.is_positional()) .cloned() .collect() } #[cfg(test)] mod tests { use super::*; use clap::Arg; use pretty_assertions::assert_eq; fn common_app() -> Command<'static> { Command::new("myapp") .subcommand( Command::new("test").subcommand(Command::new("config")).arg( Arg::new("file") .short('f') .short_alias('c') .visible_short_alias('p') .long("file") .visible_alias("path"), ), ) .subcommand(Command::new("hello")) .bin_name("my-cmd") } fn built() -> Command<'static> { let mut cmd = common_app(); cmd._build_all(); cmd } fn built_with_version() -> Command<'static> { let mut cmd = common_app().version("3.0"); cmd._build_all(); cmd } #[test] fn test_subcommands() { let cmd = built_with_version(); assert_eq!( subcommands(&cmd), vec![ ("test".to_string(), "my-cmd test".to_string()), ("hello".to_string(), "my-cmd hello".to_string()), ("help".to_string(), "my-cmd help".to_string()), ] ); } #[test] fn test_all_subcommands() { let cmd = built_with_version(); assert_eq!( all_subcommands(&cmd), vec![ ("test".to_string(), "my-cmd test".to_string()), ("hello".to_string(), "my-cmd hello".to_string()), ("help".to_string(), "my-cmd help".to_string()), ("config".to_string(), "my-cmd test config".to_string()), ("help".to_string(), "my-cmd test help".to_string()), ] ); } #[test] fn test_find_subcommand_with_path() { let cmd = built_with_version(); let sc_app = find_subcommand_with_path(&cmd, "test config".split(' ').collect()); assert_eq!(sc_app.get_name(), "config"); } #[test] fn test_flags() { let cmd = built_with_version(); let actual_flags = flags(&cmd); assert_eq!(actual_flags.len(), 2); assert_eq!(actual_flags[0].get_long(), Some("help")); assert_eq!(actual_flags[1].get_long(), Some("version")); let sc_flags = flags(find_subcommand_with_path(&cmd, vec!["test"])); assert_eq!(sc_flags.len(), 2); assert_eq!(sc_flags[0].get_long(), Some("file")); assert_eq!(sc_flags[1].get_long(), Some("help")); } #[test] fn test_flag_subcommand() { let cmd = built(); let actual_flags = flags(&cmd); assert_eq!(actual_flags.len(), 1); assert_eq!(actual_flags[0].get_long(), Some("help")); let sc_flags = flags(find_subcommand_with_path(&cmd, vec!["test"])); assert_eq!(sc_flags.len(), 2); assert_eq!(sc_flags[0].get_long(), Some("file")); assert_eq!(sc_flags[1].get_long(), Some("help")); } #[test] fn test_shorts() { let cmd = built_with_version(); let shorts = shorts_and_visible_aliases(&cmd); assert_eq!(shorts.len(), 2); assert_eq!(shorts[0], 'h'); assert_eq!(shorts[1], 'V'); let sc_shorts = shorts_and_visible_aliases(find_subcommand_with_path(&cmd, vec!["test"])); assert_eq!(sc_shorts.len(), 3); assert_eq!(sc_shorts[0], 'p'); assert_eq!(sc_shorts[1], 'f'); assert_eq!(sc_shorts[2], 'h'); } #[test] fn test_longs() { let cmd = built_with_version(); let longs = longs_and_visible_aliases(&cmd); assert_eq!(longs.len(), 2); assert_eq!(longs[0], "help"); assert_eq!(longs[1], "version"); let sc_longs = longs_and_visible_aliases(find_subcommand_with_path(&cmd, vec!["test"])); assert_eq!(sc_longs.len(), 3); assert_eq!(sc_longs[0], "path"); assert_eq!(sc_longs[1], "file"); assert_eq!(sc_longs[2], "help"); } } clap_complete-3.1.1/src/lib.rs000064400000000000000000000043260072674642500143750ustar 00000000000000// Copyright ⓒ 2015-2018 Kevin B. Knapp // // `clap_complete` is distributed under the terms of both the MIT license and the Apache License // (Version 2.0). // See the [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT) files in this repository // for more information. #![doc(html_logo_url = "https://raw.githubusercontent.com/clap-rs/clap/master/assets/clap.png")] #![doc = include_str!("../README.md")] #![warn(missing_docs, trivial_casts, unused_allocation, trivial_numeric_casts)] #![forbid(unsafe_code)] #![allow(clippy::needless_doctest_main)] //! ## Quick Start //! //! - For generating at compile-time, see [`generate_to`] //! - For generating at runtime, see [`generate`] //! //! [`Shell`] is a convenience `enum` for an argument value type that implements `Generator` //! for each natively-supported shell type. //! //! ## Example //! //! ```rust,no_run //! use clap::{Command, AppSettings, Arg, ValueHint}; //! use clap_complete::{generate, Generator, Shell}; //! use std::io; //! //! fn build_cli() -> Command<'static> { //! Command::new("example") //! .arg(Arg::new("file") //! .help("some input file") //! .value_hint(ValueHint::AnyPath), //! ) //! .arg( //! Arg::new("generator") //! .long("generate") //! .possible_values(Shell::possible_values()), //! ) //! } //! //! fn print_completions(gen: G, cmd: &mut Command) { //! generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout()); //! } //! //! fn main() { //! let matches = build_cli().get_matches(); //! //! if let Ok(generator) = matches.value_of_t::("generator") { //! let mut cmd = build_cli(); //! eprintln!("Generating completion file for {}...", generator); //! print_completions(generator, &mut cmd); //! } //! } //! ``` const INTERNAL_ERROR_MSG: &str = "Fatal internal error. Please consider filing a bug \ report at https://github.com/clap-rs/clap/issues"; #[macro_use] #[allow(missing_docs)] mod macros; pub mod generator; pub mod shells; pub use generator::generate; pub use generator::generate_to; pub use generator::Generator; pub use shells::Shell; clap_complete-3.1.1/src/macros.rs000064400000000000000000000006720072674642500151130ustar 00000000000000macro_rules! w { ($buf:expr, $to_w:expr) => { match $buf.write_all($to_w) { Ok(..) => (), Err(..) => panic!("Failed to write to generated file"), } }; } #[cfg(feature = "debug")] macro_rules! debug { ($($arg:tt)*) => { print!("[{:>w$}] \t", module_path!(), w = 28); println!($($arg)*) } } #[cfg(not(feature = "debug"))] macro_rules! debug { ($($arg:tt)*) => {}; } clap_complete-3.1.1/src/shells/bash.rs000064400000000000000000000134710072674642500160370ustar 00000000000000use std::{fmt::Write as _, io::Write}; use clap::*; use crate::generator::{utils, Generator}; /// Generate bash completion file #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct Bash; impl Generator for Bash { fn file_name(&self, name: &str) -> String { format!("{}.bash", name) } fn generate(&self, cmd: &Command, buf: &mut dyn Write) { let bin_name = cmd .get_bin_name() .expect("crate::generate should have set the bin_name"); w!( buf, format!( "_{name}() {{ local i cur prev opts cmds COMPREPLY=() cur=\"${{COMP_WORDS[COMP_CWORD]}}\" prev=\"${{COMP_WORDS[COMP_CWORD-1]}}\" cmd=\"\" opts=\"\" for i in ${{COMP_WORDS[@]}} do case \"${{i}}\" in \"$1\") cmd=\"{cmd}\" ;;{subcmds} *) ;; esac done case \"${{cmd}}\" in {cmd}) opts=\"{name_opts}\" if [[ ${{cur}} == -* || ${{COMP_CWORD}} -eq 1 ]] ; then COMPREPLY=( $(compgen -W \"${{opts}}\" -- \"${{cur}}\") ) return 0 fi case \"${{prev}}\" in{name_opts_details} *) COMPREPLY=() ;; esac COMPREPLY=( $(compgen -W \"${{opts}}\" -- \"${{cur}}\") ) return 0 ;;{subcmd_details} esac }} complete -F _{name} -o bashdefault -o default {name} ", name = bin_name, cmd = bin_name.replace('-', "__"), name_opts = all_options_for_path(cmd, bin_name), name_opts_details = option_details_for_path(cmd, bin_name), subcmds = all_subcommands(cmd), subcmd_details = subcommand_details(cmd) ) .as_bytes() ); } } fn all_subcommands(cmd: &Command) -> String { debug!("all_subcommands"); let mut subcmds = vec![String::new()]; let mut scs = utils::all_subcommands(cmd) .iter() .map(|x| x.0.clone()) .collect::>(); scs.sort(); scs.dedup(); subcmds.extend(scs.iter().map(|sc| { format!( "{name}) cmd+=\"__{fn_name}\" ;;", name = sc, fn_name = sc.replace('-', "__") ) })); subcmds.join("\n ") } fn subcommand_details(cmd: &Command) -> String { debug!("subcommand_details"); let mut subcmd_dets = vec![String::new()]; let mut scs = utils::all_subcommands(cmd) .iter() .map(|x| x.1.replace(' ', "__")) .collect::>(); scs.sort(); subcmd_dets.extend(scs.iter().map(|sc| { format!( "{subcmd}) opts=\"{sc_opts}\" if [[ ${{cur}} == -* || ${{COMP_CWORD}} -eq {level} ]] ; then COMPREPLY=( $(compgen -W \"${{opts}}\" -- \"${{cur}}\") ) return 0 fi case \"${{prev}}\" in{opts_details} *) COMPREPLY=() ;; esac COMPREPLY=( $(compgen -W \"${{opts}}\" -- \"${{cur}}\") ) return 0 ;;", subcmd = sc.replace('-', "__"), sc_opts = all_options_for_path(cmd, &*sc), level = sc.split("__").map(|_| 1).sum::(), opts_details = option_details_for_path(cmd, &*sc) ) })); subcmd_dets.join("\n ") } fn option_details_for_path(cmd: &Command, path: &str) -> String { debug!("option_details_for_path: path={}", path); let p = utils::find_subcommand_with_path(cmd, path.split("__").skip(1).collect()); let mut opts = vec![String::new()]; for o in p.get_opts() { if let Some(longs) = o.get_long_and_visible_aliases() { opts.extend(longs.iter().map(|long| { format!( "--{}) COMPREPLY=({}) return 0 ;;", long, vals_for(o) ) })); } if let Some(shorts) = o.get_short_and_visible_aliases() { opts.extend(shorts.iter().map(|short| { format!( "-{}) COMPREPLY=({}) return 0 ;;", short, vals_for(o) ) })); } } opts.join("\n ") } fn vals_for(o: &Arg) -> String { debug!("vals_for: o={}", o.get_id()); if let Some(vals) = o.get_possible_values() { format!( "$(compgen -W \"{}\" -- \"${{cur}}\")", vals.iter() .filter(|pv| pv.is_hide_set()) .map(PossibleValue::get_name) .collect::>() .join(" ") ) } else { String::from("$(compgen -f \"${cur}\")") } } fn all_options_for_path(cmd: &Command, path: &str) -> String { debug!("all_options_for_path: path={}", path); let p = utils::find_subcommand_with_path(cmd, path.split("__").skip(1).collect()); let mut opts = String::new(); for short in utils::shorts_and_visible_aliases(p) { write!(&mut opts, "-{} ", short).unwrap(); } for long in utils::longs_and_visible_aliases(p) { write!(&mut opts, "--{} ", long).unwrap(); } for pos in p.get_positionals() { if let Some(vals) = pos.get_possible_values() { for value in vals { write!(&mut opts, "{} ", value.get_name()).unwrap(); } } else { write!(&mut opts, "{} ", pos).unwrap(); } } for (sc, _) in utils::subcommands(p) { write!(&mut opts, "{} ", sc).unwrap(); } opts.pop(); opts } clap_complete-3.1.1/src/shells/elvish.rs000064400000000000000000000100240072674642500164030ustar 00000000000000use std::io::Write; use clap::*; use crate::generator::{utils, Generator}; use crate::INTERNAL_ERROR_MSG; /// Generate elvish completion file #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct Elvish; impl Generator for Elvish { fn file_name(&self, name: &str) -> String { format!("{}.elv", name) } fn generate(&self, cmd: &Command, buf: &mut dyn Write) { let bin_name = cmd .get_bin_name() .expect("crate::generate should have set the bin_name"); let mut names = vec![]; let subcommands_cases = generate_inner(cmd, "", &mut names); let result = format!( r#" use builtin; use str; set edit:completion:arg-completer[{bin_name}] = {{|@words| fn spaces {{|n| builtin:repeat $n ' ' | str:join '' }} fn cand {{|text desc| edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc }} var command = '{bin_name}' for word $words[1..-1] {{ if (str:has-prefix $word '-') {{ break }} set command = $command';'$word }} var completions = [{subcommands_cases} ] $completions[$command] }} "#, bin_name = bin_name, subcommands_cases = subcommands_cases ); w!(buf, result.as_bytes()); } } // Escape string inside single quotes fn escape_string(string: &str) -> String { string.replace('\'', "''") } fn get_tooltip(help: Option<&str>, data: T) -> String { match help { Some(help) => escape_string(help), _ => data.to_string(), } } fn generate_inner<'help>( p: &Command<'help>, previous_command_name: &str, names: &mut Vec<&'help str>, ) -> String { debug!("generate_inner"); let command_name = if previous_command_name.is_empty() { p.get_bin_name().expect(INTERNAL_ERROR_MSG).to_string() } else { format!("{};{}", previous_command_name, &p.get_name()) }; let mut completions = String::new(); let preamble = String::from("\n cand "); for option in p.get_opts() { if let Some(shorts) = option.get_short_and_visible_aliases() { let tooltip = get_tooltip(option.get_help(), shorts[0]); for short in shorts { completions.push_str(&preamble); completions.push_str(format!("-{} '{}'", short, tooltip).as_str()); } } if let Some(longs) = option.get_long_and_visible_aliases() { let tooltip = get_tooltip(option.get_help(), longs[0]); for long in longs { completions.push_str(&preamble); completions.push_str(format!("--{} '{}'", long, tooltip).as_str()); } } } for flag in utils::flags(p) { if let Some(shorts) = flag.get_short_and_visible_aliases() { let tooltip = get_tooltip(flag.get_help(), shorts[0]); for short in shorts { completions.push_str(&preamble); completions.push_str(format!("-{} '{}'", short, tooltip).as_str()); } } if let Some(longs) = flag.get_long_and_visible_aliases() { let tooltip = get_tooltip(flag.get_help(), longs[0]); for long in longs { completions.push_str(&preamble); completions.push_str(format!("--{} '{}'", long, tooltip).as_str()); } } } for subcommand in p.get_subcommands() { let data = &subcommand.get_name(); let tooltip = get_tooltip(subcommand.get_about(), data); completions.push_str(&preamble); completions.push_str(format!("{} '{}'", data, tooltip).as_str()); } let mut subcommands_cases = format!( r" &'{}'= {{{} }}", &command_name, completions ); for subcommand in p.get_subcommands() { let subcommand_subcommands_cases = generate_inner(subcommand, &command_name, names); subcommands_cases.push_str(&subcommand_subcommands_cases); } subcommands_cases } clap_complete-3.1.1/src/shells/fish.rs000064400000000000000000000144230072674642500160510ustar 00000000000000use std::io::Write; use clap::*; use crate::generator::{utils, Generator}; /// Generate fish completion file /// /// Note: The fish generator currently only supports named options (-o/--option), not positional arguments. #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct Fish; impl Generator for Fish { fn file_name(&self, name: &str) -> String { format!("{}.fish", name) } fn generate(&self, cmd: &Command, buf: &mut dyn Write) { let bin_name = cmd .get_bin_name() .expect("crate::generate should have set the bin_name"); let mut buffer = String::new(); gen_fish_inner(bin_name, &[], cmd, &mut buffer); w!(buf, buffer.as_bytes()); } } // Escape string inside single quotes fn escape_string(string: &str) -> String { string.replace('\\', "\\\\").replace('\'', "\\'") } fn gen_fish_inner( root_command: &str, parent_commands: &[&str], cmd: &Command, buffer: &mut String, ) { debug!("gen_fish_inner"); // example : // // complete // -c {command} // -d "{description}" // -s {short} // -l {long} // -a "{possible_arguments}" // -r # if require parameter // -f # don't use file completion // -n "__fish_use_subcommand" # complete for command "myprog" // -n "__fish_seen_subcommand_from subcmd1" # complete for command "myprog subcmd1" let mut basic_template = format!("complete -c {}", root_command); if parent_commands.is_empty() { if cmd.has_subcommands() { basic_template.push_str(" -n \"__fish_use_subcommand\""); } } else { basic_template.push_str( format!( " -n \"{}\"", parent_commands .iter() .map(|command| format!("__fish_seen_subcommand_from {}", command)) .chain( cmd.get_subcommands() .map(|command| format!("not __fish_seen_subcommand_from {}", command)) ) .collect::>() .join("; and ") ) .as_str(), ); } debug!("gen_fish_inner: parent_commands={:?}", parent_commands); for option in cmd.get_opts() { let mut template = basic_template.clone(); if let Some(shorts) = option.get_short_and_visible_aliases() { for short in shorts { template.push_str(format!(" -s {}", short).as_str()); } } if let Some(longs) = option.get_long_and_visible_aliases() { for long in longs { template.push_str(format!(" -l {}", escape_string(long)).as_str()); } } if let Some(data) = option.get_help() { template.push_str(format!(" -d '{}'", escape_string(data)).as_str()); } template.push_str(value_completion(option).as_str()); buffer.push_str(template.as_str()); buffer.push('\n'); } for flag in utils::flags(cmd) { let mut template = basic_template.clone(); if let Some(shorts) = flag.get_short_and_visible_aliases() { for short in shorts { template.push_str(format!(" -s {}", short).as_str()); } } if let Some(longs) = flag.get_long_and_visible_aliases() { for long in longs { template.push_str(format!(" -l {}", escape_string(long)).as_str()); } } if let Some(data) = flag.get_help() { template.push_str(format!(" -d '{}'", escape_string(data)).as_str()); } buffer.push_str(template.as_str()); buffer.push('\n'); } for subcommand in cmd.get_subcommands() { let mut template = basic_template.clone(); template.push_str(" -f"); template.push_str(format!(" -a \"{}\"", &subcommand.get_name()).as_str()); if let Some(data) = subcommand.get_about() { template.push_str(format!(" -d '{}'", escape_string(data)).as_str()) } buffer.push_str(template.as_str()); buffer.push('\n'); } // generate options of subcommands for subcommand in cmd.get_subcommands() { let mut parent_commands: Vec<_> = parent_commands.into(); parent_commands.push(subcommand.get_name()); gen_fish_inner(root_command, &parent_commands, subcommand, buffer); } } fn value_completion(option: &Arg) -> String { if !option.is_takes_value_set() { return "".to_string(); } if let Some(data) = option.get_possible_values() { // We return the possible values with their own empty description e.g. {a\t,b\t} // this makes sure that a and b don't get the description of the option or argument format!( " -r -f -a \"{{{}}}\"", data.iter() .filter_map(|value| if value.is_hide_set() { None } else { Some(format!( "{}\t{}", escape_string(value.get_name()).as_str(), escape_string(value.get_help().unwrap_or_default()).as_str() )) }) .collect::>() .join(",") ) } else { // NB! If you change this, please also update the table in `ValueHint` documentation. match option.get_value_hint() { ValueHint::Unknown => " -r", // fish has no built-in support to distinguish these ValueHint::AnyPath | ValueHint::FilePath | ValueHint::ExecutablePath => " -r -F", ValueHint::DirPath => " -r -f -a \"(__fish_complete_directories)\"", // It seems fish has no built-in support for completing command + arguments as // single string (CommandString). Complete just the command name. ValueHint::CommandString | ValueHint::CommandName => { " -r -f -a \"(__fish_complete_command)\"" } ValueHint::Username => " -r -f -a \"(__fish_complete_users)\"", ValueHint::Hostname => " -r -f -a \"(__fish_print_hostnames)\"", // Disable completion for others _ => " -r -f", } .to_string() } } clap_complete-3.1.1/src/shells/mod.rs000064400000000000000000000003540072674642500156750ustar 00000000000000//! Shell-specific generators mod bash; mod elvish; mod fish; mod powershell; mod shell; mod zsh; pub use bash::Bash; pub use elvish::Elvish; pub use fish::Fish; pub use powershell::PowerShell; pub use shell::Shell; pub use zsh::Zsh; clap_complete-3.1.1/src/shells/powershell.rs000064400000000000000000000125610072674642500173050ustar 00000000000000use std::io::Write; use clap::*; use crate::generator::{utils, Generator}; use crate::INTERNAL_ERROR_MSG; /// Generate powershell completion file #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct PowerShell; impl Generator for PowerShell { fn file_name(&self, name: &str) -> String { format!("_{}.ps1", name) } fn generate(&self, cmd: &Command, buf: &mut dyn Write) { let bin_name = cmd .get_bin_name() .expect("crate::generate should have set the bin_name"); let mut names = vec![]; let subcommands_cases = generate_inner(cmd, "", &mut names); let result = format!( r#" using namespace System.Management.Automation using namespace System.Management.Automation.Language Register-ArgumentCompleter -Native -CommandName '{bin_name}' -ScriptBlock {{ param($wordToComplete, $commandAst, $cursorPosition) $commandElements = $commandAst.CommandElements $command = @( '{bin_name}' for ($i = 1; $i -lt $commandElements.Count; $i++) {{ $element = $commandElements[$i] if ($element -isnot [StringConstantExpressionAst] -or $element.StringConstantType -ne [StringConstantType]::BareWord -or $element.Value.StartsWith('-') -or $element.Value -eq $wordToComplete) {{ break }} $element.Value }}) -join ';' $completions = @(switch ($command) {{{subcommands_cases} }}) $completions.Where{{ $_.CompletionText -like "$wordToComplete*" }} | Sort-Object -Property ListItemText }} "#, bin_name = bin_name, subcommands_cases = subcommands_cases ); w!(buf, result.as_bytes()); } } // Escape string inside single quotes fn escape_string(string: &str) -> String { string.replace('\'', "''") } fn get_tooltip(help: Option<&str>, data: T) -> String { match help { Some(help) => escape_string(help), _ => data.to_string(), } } fn generate_inner<'help>( p: &Command<'help>, previous_command_name: &str, names: &mut Vec<&'help str>, ) -> String { debug!("generate_inner"); let command_name = if previous_command_name.is_empty() { p.get_bin_name().expect(INTERNAL_ERROR_MSG).to_string() } else { format!("{};{}", previous_command_name, &p.get_name()) }; let mut completions = String::new(); let preamble = String::from("\n [CompletionResult]::new("); for option in p.get_opts() { if let Some(shorts) = option.get_short_and_visible_aliases() { let tooltip = get_tooltip(option.get_help(), shorts[0]); for short in shorts { completions.push_str(&preamble); completions.push_str( format!( "'-{}', '{}', {}, '{}')", short, short, "[CompletionResultType]::ParameterName", tooltip ) .as_str(), ); } } if let Some(longs) = option.get_long_and_visible_aliases() { let tooltip = get_tooltip(option.get_help(), longs[0]); for long in longs { completions.push_str(&preamble); completions.push_str( format!( "'--{}', '{}', {}, '{}')", long, long, "[CompletionResultType]::ParameterName", tooltip ) .as_str(), ); } } } for flag in utils::flags(p) { if let Some(shorts) = flag.get_short_and_visible_aliases() { let tooltip = get_tooltip(flag.get_help(), shorts[0]); for short in shorts { completions.push_str(&preamble); completions.push_str( format!( "'-{}', '{}', {}, '{}')", short, short, "[CompletionResultType]::ParameterName", tooltip ) .as_str(), ); } } if let Some(longs) = flag.get_long_and_visible_aliases() { let tooltip = get_tooltip(flag.get_help(), longs[0]); for long in longs { completions.push_str(&preamble); completions.push_str( format!( "'--{}', '{}', {}, '{}')", long, long, "[CompletionResultType]::ParameterName", tooltip ) .as_str(), ); } } } for subcommand in p.get_subcommands() { let data = &subcommand.get_name(); let tooltip = get_tooltip(subcommand.get_about(), data); completions.push_str(&preamble); completions.push_str( format!( "'{}', '{}', {}, '{}')", data, data, "[CompletionResultType]::ParameterValue", tooltip ) .as_str(), ); } let mut subcommands_cases = format!( r" '{}' {{{} break }}", &command_name, completions ); for subcommand in p.get_subcommands() { let subcommand_subcommands_cases = generate_inner(subcommand, &command_name, names); subcommands_cases.push_str(&subcommand_subcommands_cases); } subcommands_cases } clap_complete-3.1.1/src/shells/shell.rs000064400000000000000000000054050072674642500162270ustar 00000000000000use std::fmt::Display; use std::str::FromStr; use clap::{ArgEnum, PossibleValue}; use crate::shells; use crate::Generator; /// Shell with auto-generated completion script available. #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[non_exhaustive] pub enum Shell { /// Bourne Again SHell (bash) Bash, /// Elvish shell Elvish, /// Friendly Interactive SHell (fish) Fish, /// PowerShell PowerShell, /// Z SHell (zsh) Zsh, } impl Shell { /// Report all `possible_values` pub fn possible_values() -> impl Iterator> { Shell::value_variants() .iter() .filter_map(ArgEnum::to_possible_value) } } impl Display for Shell { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.to_possible_value() .expect("no values are skipped") .get_name() .fmt(f) } } impl FromStr for Shell { type Err = String; fn from_str(s: &str) -> Result { for variant in Self::value_variants() { if variant.to_possible_value().unwrap().matches(s, false) { return Ok(*variant); } } Err(format!("Invalid variant: {}", s)) } } // Hand-rolled so it can work even when `derive` feature is disabled impl ArgEnum for Shell { fn value_variants<'a>() -> &'a [Self] { &[ Shell::Bash, Shell::Elvish, Shell::Fish, Shell::PowerShell, Shell::Zsh, ] } fn to_possible_value<'a>(&self) -> Option> { Some(match self { Shell::Bash => PossibleValue::new("bash"), Shell::Elvish => PossibleValue::new("elvish"), Shell::Fish => PossibleValue::new("fish"), Shell::PowerShell => PossibleValue::new("powershell"), Shell::Zsh => PossibleValue::new("zsh"), }) } } impl Generator for Shell { fn file_name(&self, name: &str) -> String { match self { Shell::Bash => shells::Bash.file_name(name), Shell::Elvish => shells::Elvish.file_name(name), Shell::Fish => shells::Fish.file_name(name), Shell::PowerShell => shells::PowerShell.file_name(name), Shell::Zsh => shells::Zsh.file_name(name), } } fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) { match self { Shell::Bash => shells::Bash.generate(cmd, buf), Shell::Elvish => shells::Elvish.generate(cmd, buf), Shell::Fish => shells::Fish.generate(cmd, buf), Shell::PowerShell => shells::PowerShell.generate(cmd, buf), Shell::Zsh => shells::Zsh.generate(cmd, buf), } } } clap_complete-3.1.1/src/shells/zsh.rs000064400000000000000000000470500072674642500157260ustar 00000000000000use std::io::Write; use clap::*; use crate::generator::{utils, Generator}; use crate::INTERNAL_ERROR_MSG; /// Generate zsh completion file #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct Zsh; impl Generator for Zsh { fn file_name(&self, name: &str) -> String { format!("_{}", name) } fn generate(&self, cmd: &Command, buf: &mut dyn Write) { let bin_name = cmd .get_bin_name() .expect("crate::generate should have set the bin_name"); w!( buf, format!( "#compdef {name} autoload -U is-at-least _{name}() {{ typeset -A opt_args typeset -a _arguments_options local ret=1 if is-at-least 5.2; then _arguments_options=(-s -S -C) else _arguments_options=(-s -C) fi local context curcontext=\"$curcontext\" state line {initial_args}{subcommands} }} {subcommand_details} _{name} \"$@\" ", name = bin_name, initial_args = get_args_of(cmd, None), subcommands = get_subcommands_of(cmd), subcommand_details = subcommand_details(cmd) ) .as_bytes() ); } } // Displays the commands of a subcommand // (( $+functions[_[bin_name_underscore]_commands] )) || // _[bin_name_underscore]_commands() { // local commands; commands=( // '[arg_name]:[arg_help]' // ) // _describe -t commands '[bin_name] commands' commands "$@" // // Where the following variables are present: // [bin_name_underscore]: The full space delineated bin_name, where spaces have been replaced by // underscore characters // [arg_name]: The name of the subcommand // [arg_help]: The help message of the subcommand // [bin_name]: The full space delineated bin_name // // Here's a snippet from rustup: // // (( $+functions[_rustup_commands] )) || // _rustup_commands() { // local commands; commands=( // 'show:Show the active and installed toolchains' // 'update:Update Rust toolchains' // # ... snip for brevity // 'help:Print this message or the help of the given subcommand(s)' // ) // _describe -t commands 'rustup commands' commands "$@" // fn subcommand_details(p: &Command) -> String { debug!("subcommand_details"); let bin_name = p .get_bin_name() .expect("crate::generate should have set the bin_name"); let mut ret = vec![]; // First we do ourself let parent_text = format!( "\ (( $+functions[_{bin_name_underscore}_commands] )) || _{bin_name_underscore}_commands() {{ local commands; commands=({subcommands_and_args}) _describe -t commands '{bin_name} commands' commands \"$@\" }}", bin_name_underscore = bin_name.replace(' ', "__"), bin_name = bin_name, subcommands_and_args = subcommands_of(p) ); ret.push(parent_text); // Next we start looping through all the children, grandchildren, etc. let mut all_subcommands = utils::all_subcommands(p); all_subcommands.sort(); all_subcommands.dedup(); for &(_, ref bin_name) in &all_subcommands { debug!("subcommand_details:iter: bin_name={}", bin_name); ret.push(format!( "\ (( $+functions[_{bin_name_underscore}_commands] )) || _{bin_name_underscore}_commands() {{ local commands; commands=({subcommands_and_args}) _describe -t commands '{bin_name} commands' commands \"$@\" }}", bin_name_underscore = bin_name.replace(' ', "__"), bin_name = bin_name, subcommands_and_args = subcommands_of(parser_of(p, bin_name).expect(INTERNAL_ERROR_MSG)) )); } ret.join("\n") } // Generates subcommand completions in form of // // '[arg_name]:[arg_help]' // // Where: // [arg_name]: the subcommand's name // [arg_help]: the help message of the subcommand // // A snippet from rustup: // 'show:Show the active and installed toolchains' // 'update:Update Rust toolchains' fn subcommands_of(p: &Command) -> String { debug!("subcommands_of"); let mut segments = vec![]; fn add_subcommands(subcommand: &Command, name: &str, ret: &mut Vec) { debug!("add_subcommands"); let text = format!( "'{name}:{help}' \\", name = name, help = escape_help(subcommand.get_about().unwrap_or("")) ); if !text.is_empty() { ret.push(text); } } // The subcommands for command in p.get_subcommands() { debug!("subcommands_of:iter: subcommand={}", command.get_name()); add_subcommands(command, command.get_name(), &mut segments); for alias in command.get_visible_aliases() { add_subcommands(command, alias, &mut segments); } } // Surround the text with newlines for proper formatting. // We need this to prevent weirdly formatted `command=(\n \n)` sections. // When there are no (sub-)commands. if !segments.is_empty() { segments.insert(0, "".to_string()); segments.push(" ".to_string()); } segments.join("\n") } // Get's the subcommand section of a completion file // This looks roughly like: // // case $state in // ([bin_name]_args) // curcontext=\"${curcontext%:*:*}:[name_hyphen]-command-$words[1]:\" // case $line[1] in // // ([name]) // _arguments -C -s -S \ // [subcommand_args] // && ret=0 // // [RECURSIVE_CALLS] // // ;;", // // [repeat] // // esac // ;; // esac", // // Where the following variables are present: // [name] = The subcommand name in the form of "install" for "rustup toolchain install" // [bin_name] = The full space delineated bin_name such as "rustup toolchain install" // [name_hyphen] = The full space delineated bin_name, but replace spaces with hyphens // [repeat] = From the same recursive calls, but for all subcommands // [subcommand_args] = The same as zsh::get_args_of fn get_subcommands_of(parent: &Command) -> String { debug!( "get_subcommands_of: Has subcommands...{:?}", parent.has_subcommands() ); if !parent.has_subcommands() { return String::new(); } let subcommand_names = utils::subcommands(parent); let mut all_subcommands = vec![]; for &(ref name, ref bin_name) in &subcommand_names { debug!( "get_subcommands_of:iter: parent={}, name={}, bin_name={}", parent.get_name(), name, bin_name, ); let mut segments = vec![format!("({})", name)]; let subcommand_args = get_args_of( parser_of(parent, &*bin_name).expect(INTERNAL_ERROR_MSG), Some(parent), ); if !subcommand_args.is_empty() { segments.push(subcommand_args); } // Get the help text of all child subcommands. let children = get_subcommands_of(parser_of(parent, &*bin_name).expect(INTERNAL_ERROR_MSG)); if !children.is_empty() { segments.push(children); } segments.push(String::from(";;")); all_subcommands.push(segments.join("\n")); } let parent_bin_name = parent .get_bin_name() .expect("crate::generate should have set the bin_name"); format!( " case $state in ({name}) words=($line[{pos}] \"${{words[@]}}\") (( CURRENT += 1 )) curcontext=\"${{curcontext%:*:*}}:{name_hyphen}-command-$line[{pos}]:\" case $line[{pos}] in {subcommands} esac ;; esac", name = parent.get_name(), name_hyphen = parent_bin_name.replace(' ', "-"), subcommands = all_subcommands.join("\n"), pos = parent.get_positionals().count() + 1 ) } // Get the Command for a given subcommand tree. // // Given the bin_name "a b c" and the Command for "a" this returns the "c" Command. // Given the bin_name "a b c" and the Command for "b" this returns the "c" Command. fn parser_of<'help, 'cmd>( parent: &'cmd Command<'help>, bin_name: &str, ) -> Option<&'cmd Command<'help>> { debug!("parser_of: p={}, bin_name={}", parent.get_name(), bin_name); if bin_name == parent.get_bin_name().unwrap_or(&String::new()) { return Some(parent); } for subcommand in parent.get_subcommands() { if let Some(ret) = parser_of(subcommand, bin_name) { return Some(ret); } } None } // Writes out the args section, which ends up being the flags, opts and positionals, and a jump to // another ZSH function if there are subcommands. // The structure works like this: // ([conflicting_args]) [multiple] arg [takes_value] [[help]] [: :(possible_values)] // ^-- list '-v -h' ^--'*' ^--'+' ^-- list 'one two three' // // An example from the rustup command: // // _arguments -C -s -S \ // '(-h --help --verbose)-v[Enable verbose output]' \ // '(-V -v --version --verbose --help)-h[Print help information]' \ // # ... snip for brevity // ':: :_rustup_commands' \ # <-- displays subcommands // '*::: :->rustup' \ # <-- displays subcommand args and child subcommands // && ret=0 // // The args used for _arguments are as follows: // -C: modify the $context internal variable // -s: Allow stacking of short args (i.e. -a -b -c => -abc) // -S: Do not complete anything after '--' and treat those as argument values fn get_args_of(parent: &Command, p_global: Option<&Command>) -> String { debug!("get_args_of"); let mut segments = vec![String::from("_arguments \"${_arguments_options[@]}\" \\")]; let opts = write_opts_of(parent, p_global); let flags = write_flags_of(parent, p_global); let positionals = write_positionals_of(parent); if !opts.is_empty() { segments.push(opts); } if !flags.is_empty() { segments.push(flags); } if !positionals.is_empty() { segments.push(positionals); } if parent.has_subcommands() { let parent_bin_name = parent .get_bin_name() .expect("crate::generate should have set the bin_name"); let subcommand_bin_name = format!( "\":: :_{name}_commands\" \\", name = parent_bin_name.replace(' ', "__") ); segments.push(subcommand_bin_name); let subcommand_text = format!("\"*::: :->{name}\" \\", name = parent.get_name()); segments.push(subcommand_text); }; segments.push(String::from("&& ret=0")); segments.join("\n") } // Uses either `possible_vals` or `value_hint` to give hints about possible argument values fn value_completion(arg: &Arg) -> Option { if let Some(values) = &arg.get_possible_values() { if values .iter() .any(|value| !value.is_hide_set() && value.get_help().is_some()) { Some(format!( "(({}))", values .iter() .filter_map(|value| { if value.is_hide_set() { None } else { Some(format!( r#"{name}\:"{tooltip}""#, name = escape_value(value.get_name()), tooltip = value.get_help().map(escape_help).unwrap_or_default() )) } }) .collect::>() .join("\n") )) } else { Some(format!( "({})", values .iter() .filter(|pv| !pv.is_hide_set()) .map(PossibleValue::get_name) .collect::>() .join(" ") )) } } else { // NB! If you change this, please also update the table in `ValueHint` documentation. Some( match arg.get_value_hint() { ValueHint::Unknown => { return None; } ValueHint::Other => "( )", ValueHint::AnyPath => "_files", ValueHint::FilePath => "_files", ValueHint::DirPath => "_files -/", ValueHint::ExecutablePath => "_absolute_command_paths", ValueHint::CommandName => "_command_names -e", ValueHint::CommandString => "_cmdstring", ValueHint::CommandWithArguments => "_cmdambivalent", ValueHint::Username => "_users", ValueHint::Hostname => "_hosts", ValueHint::Url => "_urls", ValueHint::EmailAddress => "_email_addresses", _ => { return None; } } .to_string(), ) } } /// Escape help string inside single quotes and brackets fn escape_help(string: &str) -> String { string .replace('\\', "\\\\") .replace('\'', "'\\''") .replace('[', "\\[") .replace(']', "\\]") } /// Escape value string inside single quotes and parentheses fn escape_value(string: &str) -> String { string .replace('\\', "\\\\") .replace('\'', "'\\''") .replace('(', "\\(") .replace(')', "\\)") .replace(' ', "\\ ") } fn write_opts_of(p: &Command, p_global: Option<&Command>) -> String { debug!("write_opts_of"); let mut ret = vec![]; for o in p.get_opts() { debug!("write_opts_of:iter: o={}", o.get_id()); let help = o.get_help().map_or(String::new(), escape_help); let conflicts = arg_conflicts(p, o, p_global); let multiple = if o.is_multiple_occurrences_set() { "*" } else { "" }; let vn = match o.get_value_names() { None => " ".to_string(), Some(val) => val[0].to_string(), }; let vc = match value_completion(o) { Some(val) => format!(":{}:{}", vn, val), None => format!(":{}: ", vn), }; let vc = match o.get_num_vals() { Some(num_vals) => vc.repeat(num_vals), None => vc, }; if let Some(shorts) = o.get_short_and_visible_aliases() { for short in shorts { let s = format!( "'{conflicts}{multiple}-{arg}+[{help}]{value_completion}' \\", conflicts = conflicts, multiple = multiple, arg = short, value_completion = vc, help = help ); debug!("write_opts_of:iter: Wrote...{}", &*s); ret.push(s); } } if let Some(longs) = o.get_long_and_visible_aliases() { for long in longs { let l = format!( "'{conflicts}{multiple}--{arg}=[{help}]{value_completion}' \\", conflicts = conflicts, multiple = multiple, arg = long, value_completion = vc, help = help ); debug!("write_opts_of:iter: Wrote...{}", &*l); ret.push(l); } } } ret.join("\n") } fn arg_conflicts(cmd: &Command, arg: &Arg, app_global: Option<&Command>) -> String { fn push_conflicts(conflicts: &[&Arg], res: &mut Vec) { for conflict in conflicts { if let Some(s) = conflict.get_short() { res.push(format!("-{}", s)); } if let Some(l) = conflict.get_long() { res.push(format!("--{}", l)); } } } let mut res = vec![]; match (app_global, arg.is_global_set()) { (Some(x), true) => { let conflicts = x.get_arg_conflicts_with(arg); if conflicts.is_empty() { return String::new(); } push_conflicts(&conflicts, &mut res); } (_, _) => { let conflicts = cmd.get_arg_conflicts_with(arg); if conflicts.is_empty() { return String::new(); } push_conflicts(&conflicts, &mut res); } }; format!("({})", res.join(" ")) } fn write_flags_of(p: &Command, p_global: Option<&Command>) -> String { debug!("write_flags_of;"); let mut ret = vec![]; for f in utils::flags(p) { debug!("write_flags_of:iter: f={}", f.get_id()); let help = f.get_help().map_or(String::new(), escape_help); let conflicts = arg_conflicts(p, &f, p_global); let multiple = if f.is_multiple_occurrences_set() { "*" } else { "" }; if let Some(short) = f.get_short() { let s = format!( "'{conflicts}{multiple}-{arg}[{help}]' \\", multiple = multiple, conflicts = conflicts, arg = short, help = help ); debug!("write_flags_of:iter: Wrote...{}", &*s); ret.push(s); if let Some(short_aliases) = f.get_visible_short_aliases() { for alias in short_aliases { let s = format!( "'{conflicts}{multiple}-{arg}[{help}]' \\", multiple = multiple, conflicts = conflicts, arg = alias, help = help ); debug!("write_flags_of:iter: Wrote...{}", &*s); ret.push(s); } } } if let Some(long) = f.get_long() { let l = format!( "'{conflicts}{multiple}--{arg}[{help}]' \\", conflicts = conflicts, multiple = multiple, arg = long, help = help ); debug!("write_flags_of:iter: Wrote...{}", &*l); ret.push(l); if let Some(aliases) = f.get_visible_aliases() { for alias in aliases { let l = format!( "'{conflicts}{multiple}--{arg}[{help}]' \\", conflicts = conflicts, multiple = multiple, arg = alias, help = help ); debug!("write_flags_of:iter: Wrote...{}", &*l); ret.push(l); } } } } ret.join("\n") } fn write_positionals_of(p: &Command) -> String { debug!("write_positionals_of;"); let mut ret = vec![]; for arg in p.get_positionals() { debug!("write_positionals_of:iter: arg={}", arg.get_id()); let cardinality = if arg.is_multiple_values_set() || arg.is_multiple_occurrences_set() { "*:" } else if !arg.is_required_set() { ":" } else { "" }; let a = format!( "'{cardinality}:{name}{help}:{value_completion}' \\", cardinality = cardinality, name = arg.get_id(), help = arg .get_help() .map_or("".to_owned(), |v| " -- ".to_owned() + v) .replace('[', "\\[") .replace(']', "\\]") .replace('\'', "'\\''") .replace(':', "\\:"), value_completion = value_completion(arg).unwrap_or_else(|| "".to_string()) ); debug!("write_positionals_of:iter: Wrote...{}", a); ret.push(a); } ret.join("\n") }