print only.Expand description
§Printting and Command Line Interface
There are 4 feature flags related to CLI
print: This is the most minimal feature set. Using features from this feature flag means you acknowledge your code is being called from a program that usescu::cli(i.e. theclifeature)cli: Use this if your crate is the end binary (i.e. not a library). This integrates and re-exportsclap.- This turns on
printautomatically
- This turns on
prompt: This impliesprintand will also enable the ability to show prompts in the terminal, including password prompts (which hide the input when user types into the terminal).
§Integration with clap
When the cli feature is enabled, clap is re-exported from the prelude,
so you can use clap as if it’s a dependency, without actually adding
it to your Cargo.toml
use cu::pre::*;
use clap::Parser;
#[derive(Parser)]
struct MyCli {
/// Just an example flag
#[clap(short, long)]
hello: bool,
}§Common Command Options
The Flags struct implement clap::Args to provide common
options that integrates with the rest of the crate:
--verbose/-vto increase verbose level.--quiet/-qto decrease verbose level.--colorto set color mode
The prompt feature enables these additional options:
--yes/-yto answeryto all yes/no prompts.--non-interactive: Disallow prompts, prompts will fail with an error instead- With
--yes --non-interactive, yes/no prompts gets answeredyesand other prompts are blocked
- With
--interactive: This is the default, and cancels the effect of one--non-interactive
The cu::cli macro generates a shim
to parse the flags and pass it to your main function.
It also handles the Result returned back. See the example
below and more usage examples in the documentation for the macro.
use cu::pre::*;
// clap will be part of the prelude
// when the `cli` feature is enabled
// Typically, you want to have a wrapper struct
// so you can derive additional options with clap,
// and provide a description via doc comments, like below
// clap will parse the doc comment of the Args struct
// as the help text
/// My program
///
/// This is my program, it is very good.
#[derive(clap::Parser, Clone)]
struct Args {
/// Input of the program
#[clap(short, long)]
input: String,
/// Output of the program
#[clap(short, long)]
output: Option<String>,
#[clap(flatten)]
inner: cu::cli::Flags,
}
// use the flags attribute to refer to the cu::cli::Flags field inside the Args struct
#[cu::cli(flags = "inner")]
fn main(args: Args) -> cu::Result<()> {
cu::info!("input is {}", args.input);
cu::info!("output is {:?}", args.output);
Ok(())
}§Printing and Logging
In addition to the logging macros re-exported from the log
crate, cu provides print and hint macros:
print: likeinfo, but has a higher importancehint: likeprint, but specifically for hinting actions the user can take (to resolve an error, for example).
These 2 levels are not directly controlled by log,
and can still print when logging is statically disabled.
The following table shows what are printed for each level,
The RUST_LOG environment variable is also supported in the same
way as in env_logger.
When mixing RUST_LOG and verbosity flags, logging messages are filtered
by RUST_LOG, and the verbosity would only apply to print and hint
§Other
When setting up test, you can use cu::cli::level to quickly inititialize logging
without dealing with the details.
cu::cli::set_thread_name can be used to add a prefix to all messages printed
by the current thread.
Messages that are too long and multi-line messages are automatically wrapped.
§Manual Parsing CLI args
cu::cli::try_parse
and cu::cli::print_help can be useful
when you want to manually invoke a command parser. These
respect the --color option passed to the program.
§Ctrl-C Signals
We wrap the ctrlc crate because it only allows
for one global handler. See Handling Ctrl-C
§Progress Bars
See Progress Bars
§Prompting
See Prompting
Structs§
- Ctrlc
Builder - Builder for a new frame for handling
Ctrl-Csignals. The canonical factory function for this iscu::cli::ctrlc_frame. See Handling Ctrl-C. - Ctrlc
Signal - Signal passed into a task executing inside a
Ctrl-Caction frame, for it to check ifCtrl-Chas been signaled - Default
LogConfig - The default
LogConfig - Flags
cli - Common flags for
cu::cli - Progress
Bar - Handle for a progress bar (This is the internal state, the handle is
Arc<ProgressBar>) - Progress
BarBuilder - Builder for a progress bar
- Prompt
Builder prompt - See
Prompting
Enums§
- Target
- Target for printing output
Traits§
- LogConfig
- Hook to configure the level and format before logging
Functions§
- add_
global_ ctrlc_ handler cli - Add a global handler to handle Ctrl-C signals
- animate_
to - Set the target for animation messages (prompts and bars)
- ctrlc_
frame - Handling Ctrl-C
- init_
options - Set global CLI options
- level
- Shorthand to quickly setup logging. Can be useful in tests.
- password_
chars_ legal prompt - Check if the password contains all “legal” characters (and is non-empty)
- print_
help cli - Print the help text from a command parser.
- print_
to - Set the target for printing messages
- progress
- Progress Bars
- prompt
prompt - Prompting
- reset_
color cli - Reset terminal color
- reset_
thread_ name - Reset the current thread to not show a name
- set_
thread_ name - Set the name to show up in messages printed by the current thread
- try_
parse cli - Try to parse arguments from an iterator and print the error/help on failure.
- yesno
prompt - Show a Yes/No prompt
Type Aliases§
- Progress
Handle - Convenience type for
Arc<cu::ProgressBar>