• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright ⓒ 2015-2016 Kevin B. Knapp and [`clap-rs` contributors](https://github.com/clap-rs/clap/graphs/contributors).
2 // Licensed under the MIT license
3 // (see LICENSE or <http://opensource.org/licenses/MIT>) All files in the project carrying such
4 // notice may not be copied, modified, or distributed except according to those terms.
5 
6 //! > **Command Line Argument Parser for Rust**
7 //!
8 //! Quick Links:
9 //! - Derive [tutorial][_derive::_tutorial] and [reference][_derive]
10 //! - Builder [tutorial][_tutorial] and [reference](index.html)
11 //! - [Cookbook][_cookbook]
12 //! - [FAQ][_faq]
13 //! - [Discussions](https://github.com/clap-rs/clap/discussions)
14 //!
15 //! ## Aspirations
16 //!
17 //! - Out of the box, users get a polished CLI experience
18 //!   - Including common argument behavior, help generation, suggested fixes for users, colored output, [shell completions](https://github.com/clap-rs/clap/tree/master/clap_complete), etc
19 //! - Flexible enough to port your existing CLI interface
20 //!   - However, we won't necessarily streamline support for each use case
21 //! - Reasonable parse performance
22 //! - Resilient maintainership, including
23 //!   - Willing to break compatibility rather than batching up breaking changes in large releases
24 //!   - Leverage feature flags to keep to one active branch
25 //!   - Being under [WG-CLI](https://github.com/rust-cli/team/) to increase the bus factor
26 //! - We follow semver and will wait about 6-9 months between major breaking changes
27 //! - We will support the last two minor Rust releases (MSRV, currently 1.56.1)
28 //!
29 //! While these aspirations can be at odds with fast build times and low binary
30 //! size, we will still strive to keep these reasonable for the flexibility you
31 //! get.  Check out the
32 //! [argparse-benchmarks](https://github.com/rust-cli/argparse-benchmarks-rs) for
33 //! CLI parsers optimized for other use cases.
34 //!
35 //! ## Example
36 //!
37 //! Run
38 //! ```console
39 //! $ cargo add clap --features derive
40 //! ```
41 //! *(See also [feature flag reference][_features])*
42 //!
43 //! Then define your CLI in `main.rs`:
44 #![cfg_attr(not(feature = "derive"), doc = " ```ignore")]
45 #![cfg_attr(feature = "derive", doc = " ```no_run")]
46 #![doc = include_str!("../examples/demo.rs")]
47 //! ```
48 //!
49 //! And try it out:
50 #![doc = include_str!("../examples/demo.md")]
51 //!
52 //! See also the derive [tutorial][_derive::_tutorial] and [reference][_derive]
53 //!
54 //! ### Related Projects
55 //!
56 //! Augment clap:
57 //! - [wild](https://crates.io/crates/wild) for supporting wildcards (`*`) on Windows like you do Linux
58 //! - [argfile](https://crates.io/crates/argfile) for loading additional arguments from a file (aka response files)
59 //! - [shadow-rs](https://crates.io/crates/shadow-rs) for generating `Command::long_version`
60 //! - [clap_mangen](https://crates.io/crates/clap_mangen) for generating man page source (roff)
61 //! - [clap_complete](https://crates.io/crates/clap_complete) for shell completion support
62 //!
63 //! CLI Helpers
64 //! - [clap-verbosity-flag](https://crates.io/crates/clap-verbosity-flag)
65 //! - [clap-cargo](https://crates.io/crates/clap-cargo)
66 //! - [concolor-clap](https://crates.io/crates/concolor-clap)
67 //!
68 //! Testing
69 //! - [`trycmd`](https://crates.io/crates/trycmd):  Bulk snapshot testing
70 //! - [`snapbox`](https://crates.io/crates/snapbox):  Specialized snapshot testing
71 //! - [`assert_cmd`](https://crates.io/crates/assert_cmd) and [`assert_fs`](https://crates.io/crates/assert_fs): Customized testing
72 //!
73 //! Documentation:
74 //! - [Command-line Apps for Rust](https://rust-cli.github.io/book/index.html) book
75 //!
76 
77 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
78 #![doc(html_logo_url = "https://raw.githubusercontent.com/clap-rs/clap/master/assets/clap.png")]
79 #![warn(
80     missing_docs,
81     missing_debug_implementations,
82     missing_copy_implementations,
83     trivial_casts,
84     unused_allocation,
85     trivial_numeric_casts,
86     clippy::single_char_pattern
87 )]
88 #![forbid(unsafe_code)]
89 // HACK https://github.com/rust-lang/rust-clippy/issues/7290
90 #![allow(clippy::single_component_path_imports)]
91 #![allow(clippy::branches_sharing_code)]
92 // Doesn't allow for debug statements, etc to be unique
93 #![allow(clippy::if_same_then_else)]
94 
95 #[cfg(not(feature = "std"))]
96 compile_error!("`std` feature is currently required to build `clap`");
97 
98 pub use crate::builder::ArgAction;
99 pub use crate::builder::Command;
100 pub use crate::builder::{Arg, ArgGroup};
101 pub use crate::error::Error;
102 pub use crate::parser::ArgMatches;
103 #[cfg(feature = "color")]
104 pub use crate::util::color::ColorChoice;
105 #[cfg(not(feature = "color"))]
106 #[allow(unused_imports)]
107 pub(crate) use crate::util::color::ColorChoice;
108 
109 pub use crate::derive::{Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum};
110 
111 #[allow(deprecated)]
112 pub use crate::builder::App;
113 pub use crate::builder::{AppFlags, AppSettings, ArgFlags, ArgSettings, PossibleValue, ValueHint};
114 pub use crate::error::{ErrorKind, Result};
115 #[allow(deprecated)]
116 pub use crate::parser::{Indices, OsValues, ValueSource, Values};
117 
118 #[cfg(feature = "yaml")]
119 #[doc(hidden)]
120 #[cfg_attr(
121     feature = "deprecated",
122     deprecated(
123         since = "3.0.0",
124         note = "Deprecated in Issue #3087, maybe clap::Parser would fit your use case?"
125     )
126 )]
127 #[doc(hidden)]
128 pub use yaml_rust::YamlLoader;
129 
130 #[cfg(feature = "derive")]
131 #[doc(hidden)]
132 pub use clap_derive::{self, *};
133 
134 /// Deprecated, replaced with [`CommandFactory`]
135 #[cfg_attr(
136     feature = "deprecated",
137     deprecated(since = "3.0.0", note = "Replaced with `CommandFactory`")
138 )]
139 pub use CommandFactory as IntoApp;
140 /// Deprecated, replaced with [`Parser`]
141 #[cfg_attr(
142     feature = "deprecated",
143     deprecated(since = "3.0.0", note = "Replaced with `Parser`")
144 )]
145 #[doc(hidden)]
146 pub use Parser as StructOpt;
147 /// Deprecated, replaced with [`ValueEnum`]
148 #[cfg_attr(
149     feature = "deprecated",
150     deprecated(since = "3.2.0", note = "Replaced with `ValueEnum`")
151 )]
152 pub use ValueEnum as ArgEnum;
153 
154 #[cfg(feature = "unstable-doc")]
155 pub mod _cookbook;
156 #[cfg(feature = "unstable-doc")]
157 pub mod _derive;
158 #[cfg(feature = "unstable-doc")]
159 pub mod _faq;
160 #[cfg(feature = "unstable-doc")]
161 pub mod _features;
162 #[cfg(feature = "unstable-doc")]
163 pub mod _tutorial;
164 
165 #[doc(hidden)]
166 pub mod __macro_refs {
167     #[cfg(any(feature = "derive", feature = "cargo"))]
168     #[doc(hidden)]
169     pub use once_cell;
170 }
171 
172 #[macro_use]
173 #[allow(missing_docs)]
174 mod macros;
175 
176 mod derive;
177 
178 #[cfg(feature = "regex")]
179 pub use crate::builder::RegexRef;
180 
181 pub mod builder;
182 pub mod error;
183 pub mod parser;
184 
185 mod mkeymap;
186 mod output;
187 mod util;
188 
189 const INTERNAL_ERROR_MSG: &str = "Fatal internal error. Please consider filing a bug \
190                                   report at https://github.com/clap-rs/clap/issues";
191 const INVALID_UTF8: &str = "unexpected invalid UTF-8 code point";
192 
193 /// Deprecated, replaced with [`Command::new`], unless you were looking for [Subcommand]
194 #[cfg_attr(
195     feature = "deprecated",
196     deprecated(
197         since = "3.0.0",
198         note = "Replaced with `Command::new` unless you intended the `Subcommand` trait"
199     )
200 )]
201 #[doc(hidden)]
202 #[derive(Debug, Copy, Clone)]
203 pub struct SubCommand {}
204 
205 #[allow(deprecated)]
206 impl SubCommand {
207     /// Deprecated, replaced with [`Command::new`].
208     /// Did you mean Subcommand (lower-case c)?
209     #[cfg_attr(
210         feature = "deprecated",
211         deprecated(since = "3.0.0", note = "Replaced with `Command::new`")
212     )]
213     #[doc(hidden)]
with_name<'help>(name: &str) -> App<'help>214     pub fn with_name<'help>(name: &str) -> App<'help> {
215         Command::new(name)
216     }
217 
218     /// Deprecated in [Issue #3087](https://github.com/clap-rs/clap/issues/3087), maybe [`clap::Parser`][crate::Parser] would fit your use case?
219     #[cfg(feature = "yaml")]
220     #[cfg_attr(
221         feature = "deprecated",
222         deprecated(
223             since = "3.0.0",
224             note = "Deprecated in Issue #3087, maybe clap::Parser would fit your use case?"
225         )
226     )]
227     #[doc(hidden)]
from_yaml(yaml: &yaml_rust::Yaml) -> App228     pub fn from_yaml(yaml: &yaml_rust::Yaml) -> App {
229         #![allow(deprecated)]
230         Command::from_yaml(yaml)
231     }
232 }
233