Struct VerbosityLevel

Source
pub struct VerbosityLevel { /* private fields */ }
Expand description

Transform -v and -q flags to some kind of loglevel

§Example

Include this into your clap derive struct like this:

use libpt_cli::args::VerbosityLevel;
use clap::Parser;

#[derive(Parser, Debug)]
pub struct Opts {
    #[command(flatten)]
    pub verbose: VerbosityLevel,
    #[arg(short, long)]
    pub mynum: usize,
}

Get the loglevel like this:

use libpt_cli::args::VerbosityLevel;
use libpt_log::Level;
use clap::Parser;


fn main() {
    let opts = Opts::parse();

    // Level might be None if the user wants no output at all.
    // for the 'tracing' level:
    let level: Level = opts.verbose.level();
}

Implementations§

Source§

impl VerbosityLevel

Source

pub const TRACE: Self

§Examples
use libpt_log::Level; // reexport: tracing
use libpt_cli::args::VerbosityLevel;

let verbosity_level = VerbosityLevel::TRACE;
assert_eq!(verbosity_level.level(), Level::TRACE);
Source

pub const DEBUG: Self

§Examples
use libpt_log::Level; // reexport: tracing
use libpt_cli::args::VerbosityLevel;

let verbosity_level = VerbosityLevel::DEBUG;
assert_eq!(verbosity_level.level(), Level::DEBUG);
Source

pub const INFO: Self

§Examples
use libpt_log::Level; // reexport: tracing
use libpt_cli::args::VerbosityLevel;

let verbosity_level = VerbosityLevel::INFO;
assert_eq!(verbosity_level.level(), Level::INFO);
Source

pub const WARN: Self

§Examples
use libpt_log::Level; // reexport: tracing
use libpt_cli::args::VerbosityLevel;

let verbosity_level = VerbosityLevel::WARN;
assert_eq!(verbosity_level.level(), Level::WARN);
Source

pub const ERROR: Self

§Examples
use libpt_log::Level; // reexport: tracing
use libpt_cli::args::VerbosityLevel;

let verbosity_level = VerbosityLevel::ERROR;
assert_eq!(verbosity_level.level(), Level::ERROR);
Source

pub fn changed(&self) -> bool

true only if no verbose and no quiet was set (user is using defaults)

Source

pub fn level(&self) -> Level

get the Level for that VerbosityLevel

§Examples
use libpt_log::Level; // reexport: tracing
use libpt_cli::args::VerbosityLevel;

let verbosity_level = VerbosityLevel::INFO;
assert_eq!(verbosity_level.level(), Level::INFO);
Examples found in repository?
examples/cli.rs (line 26)
24fn main() {
25    let cli = Cli::parse();
26    let _logger = Logger::builder().set_level(cli.verbosity.level()).build();
27
28    debug!("logger initialized with level: {}", cli.verbosity.level());
29
30    if !cli.machine {
31        let text = cli.text.join(" ");
32        printing::blockprint(text, console::Color::Green);
33    } else {
34        for text in cli.text {
35            println!("{text}")
36        }
37    }
38}
Source

pub fn level_for_log_crate(&self) -> Level

get the log::Level for that VerbosityLevel

This is the method for the log crate, which I use less often.

None means that absolutely no output is wanted (completely quiet)

Trait Implementations§

Source§

impl Args for VerbosityLevel

Source§

fn group_id() -> Option<Id>

Report the ArgGroup::id for this set of arguments
Source§

fn augment_args<'b>(__clap_app: Command) -> Command

Append to Command so it can instantiate Self via FromArgMatches::from_arg_matches_mut Read more
Source§

fn augment_args_for_update<'b>(__clap_app: Command) -> Command

Append to Command so it can instantiate self via FromArgMatches::update_from_arg_matches_mut Read more
Source§

impl Clone for VerbosityLevel

Source§

fn clone(&self) -> VerbosityLevel

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl CommandFactory for VerbosityLevel

Source§

fn command<'b>() -> Command

Build a Command that can instantiate Self. Read more
Source§

fn command_for_update<'b>() -> Command

Build a Command that can update self. Read more
Source§

impl Debug for VerbosityLevel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for VerbosityLevel

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for VerbosityLevel

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl FromArgMatches for VerbosityLevel

Source§

fn from_arg_matches(__clap_arg_matches: &ArgMatches) -> Result<Self, Error>

Instantiate Self from ArgMatches, parsing the arguments as needed. Read more
Source§

fn from_arg_matches_mut( __clap_arg_matches: &mut ArgMatches, ) -> Result<Self, Error>

Instantiate Self from ArgMatches, parsing the arguments as needed. Read more
Source§

fn update_from_arg_matches( &mut self, __clap_arg_matches: &ArgMatches, ) -> Result<(), Error>

Assign values from ArgMatches to self.
Source§

fn update_from_arg_matches_mut( &mut self, __clap_arg_matches: &mut ArgMatches, ) -> Result<(), Error>

Assign values from ArgMatches to self.
Source§

impl Hash for VerbosityLevel

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Parser for VerbosityLevel

Source§

fn parse() -> Self

Parse from std::env::args_os(), exit on error.
Source§

fn try_parse() -> Result<Self, Error>

Parse from std::env::args_os(), return Err on error.
Source§

fn parse_from<I, T>(itr: I) -> Self
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Parse from iterator, exit on error.
Source§

fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Parse from iterator, return Err on error.
Source§

fn update_from<I, T>(&mut self, itr: I)
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Update from iterator, exit on error. Read more
Source§

fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error>
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Update from iterator, return Err on error.
Source§

impl PartialEq for VerbosityLevel

Source§

fn eq(&self, other: &VerbosityLevel) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for VerbosityLevel

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for VerbosityLevel

Source§

impl StructuralPartialEq for VerbosityLevel

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,