|
1 |
| -//! Control `log` level with a `--verbose` flag for your CLI |
| 1 | +//! Easily add `--verbose` and `--quiet` flags to CLIs using [Clap](http://crates.io/crates/clap). |
2 | 2 | //!
|
3 | 3 | //! # Examples
|
4 | 4 | //!
|
5 | 5 | //! To get `--quiet` and `--verbose` flags through your entire program, just `flatten`
|
6 | 6 | //! [`Verbosity`]:
|
| 7 | +//! |
| 8 | +//! ```rust,no_run |
| 9 | +//! use clap::Parser; |
| 10 | +//! use clap_verbosity_flag::Verbosity; |
| 11 | +//! |
| 12 | +//! #[derive(Debug, Parser)] |
| 13 | +//! struct Cli { |
| 14 | +//! #[command(flatten)] |
| 15 | +//! verbosity: Verbosity, |
| 16 | +//! |
| 17 | +//! // ... other options |
| 18 | +//! } |
| 19 | +//! ``` |
| 20 | +//! |
| 21 | +//! You can then use this to configure your logger: |
| 22 | +//! |
7 | 23 | //! ```rust,no_run
|
8 | 24 | //! # use clap::Parser;
|
9 | 25 | //! # use clap_verbosity_flag::Verbosity;
|
10 | 26 | //! #
|
11 |
| -//! # /// Le CLI |
12 | 27 | //! # #[derive(Debug, Parser)]
|
13 | 28 | //! # struct Cli {
|
14 |
| -//! #[command(flatten)] |
15 |
| -//! verbose: Verbosity, |
| 29 | +//! # #[command(flatten)] |
| 30 | +//! # verbosity: Verbosity, |
16 | 31 | //! # }
|
| 32 | +//! let cli = Cli::parse(); |
| 33 | +//! # #[cfg(feature = "log")] |
| 34 | +//! env_logger::Builder::new() |
| 35 | +//! .filter_level(cli.verbosity.log_level_filter()) |
| 36 | +//! .init(); |
17 | 37 | //! ```
|
18 | 38 | //!
|
19 |
| -//! You can then use this to configure your logger: |
| 39 | +//! ## Use with `tracing` |
| 40 | +//! |
| 41 | +//! To use with [`tracing`](https://crates.io/crates/tracing), disable the log feature flag and |
| 42 | +//! enable the `tracing` feature flag: |
| 43 | +//! |
| 44 | +//! ```shell |
| 45 | +//! cargo add clap_verbosity_flag --no-default features --features tracing |
| 46 | +//! ``` |
| 47 | +//! |
| 48 | +//! Then you can use it like this: |
| 49 | +//! |
20 | 50 | //! ```rust,no_run
|
21 | 51 | //! # use clap::Parser;
|
22 | 52 | //! # use clap_verbosity_flag::Verbosity;
|
23 | 53 | //! #
|
24 |
| -//! # /// Le CLI |
25 | 54 | //! # #[derive(Debug, Parser)]
|
26 | 55 | //! # struct Cli {
|
27 | 56 | //! # #[command(flatten)]
|
28 |
| -//! # verbose: Verbosity, |
| 57 | +//! # verbosity: Verbosity, |
29 | 58 | //! # }
|
30 | 59 | //! let cli = Cli::parse();
|
31 |
| -//! # #[cfg(feature = "log")] |
32 |
| -//! env_logger::Builder::new() |
33 |
| -//! .filter_level(cli.verbose.log_level_filter()) |
| 60 | +//! # #[cfg(feature = "tracing")] |
| 61 | +//! tracing_subscriber::fmt() |
| 62 | +//! .with_max_level(cli.verbosity) |
34 | 63 | //! .init();
|
35 | 64 | //! ```
|
36 | 65 | //!
|
37 |
| -//! By default, this will only report errors. |
38 |
| -//! - `-q` silences output |
39 |
| -//! - `-v` show warnings |
40 |
| -//! - `-vv` show info |
41 |
| -//! - `-vvv` show debug |
42 |
| -//! - `-vvvv` show trace |
| 66 | +//! # Using `--verbose` and `--quiet` flags |
| 67 | +//! |
| 68 | +//! The default verbosity level will cause `log` / `tracing` to only report errors. The flags can be |
| 69 | +//! specified multiple times to increase or decrease the verbosity level. |
| 70 | +//! |
| 71 | +//! - silence output: `-q` / `--quiet` |
| 72 | +//! - show warnings: `-v` / `--verbose` |
| 73 | +//! - show info: `-vv` / `--verbose --verbose` |
| 74 | +//! - show debug: `-vvv` / `--verbose --verbose --verbose` |
| 75 | +//! - show trace: `-vvvv` / `--verbose --verbose --verbose --verbose` |
| 76 | +//! |
| 77 | +//! # Customizing the default log level |
43 | 78 | //!
|
44 | 79 | //! By default, the log level is set to Error. To customize this to a different level, pass a type
|
45 | 80 | //! implementing the [`LogLevel`] trait to [`Verbosity`]:
|
|
48 | 83 | //! # use clap::Parser;
|
49 | 84 | //! use clap_verbosity_flag::{Verbosity, InfoLevel};
|
50 | 85 | //!
|
51 |
| -//! /// Le CLI |
52 | 86 | //! #[derive(Debug, Parser)]
|
53 | 87 | //! struct Cli {
|
54 | 88 | //! #[command(flatten)]
|
|
63 | 97 | #![warn(clippy::print_stdout)]
|
64 | 98 |
|
65 | 99 | #[doc = include_str!("../README.md")]
|
66 |
| -#[cfg(doctest)] |
| 100 | +#[cfg(all(doctest, feature = "log", feature = "tracing"))] |
67 | 101 | pub struct ReadmeDoctests;
|
68 | 102 |
|
69 | 103 | use std::fmt;
|
|
0 commit comments