pub struct ContextBuilder { /* private fields */ }Expand description
Builder for creating Context instances with custom settings.
§Example
use click::context::ContextBuilder;
let ctx = ContextBuilder::new()
.info_name("myapp")
.allow_extra_args(true)
.terminal_width(100)
.color(true)
.build();
assert_eq!(ctx.info_name(), Some("myapp"));
assert!(ctx.allow_extra_args());
assert_eq!(ctx.terminal_width(), Some(100));
assert_eq!(ctx.color(), Some(true));Implementations§
Source§impl ContextBuilder
impl ContextBuilder
Sourcepub fn parent(self, parent: Arc<Context>) -> Self
pub fn parent(self, parent: Arc<Context>) -> Self
Set the parent context.
Settings like terminal_width, color, and show_default will be
inherited from the parent if not explicitly set.
Sourcepub fn info_name(self, name: impl Into<String>) -> Self
pub fn info_name(self, name: impl Into<String>) -> Self
Set the info name for this context.
This is typically the command name.
Sourcepub fn obj<T: Any + Send + Sync + 'static>(self, obj: T) -> Self
pub fn obj<T: Any + Send + Sync + 'static>(self, obj: T) -> Self
Set the user object.
If not set and there’s a parent context, the parent’s obj will be inherited.
Sourcepub fn auto_envvar_prefix(self, prefix: impl Into<String>) -> Self
pub fn auto_envvar_prefix(self, prefix: impl Into<String>) -> Self
Set the auto envvar prefix.
If set, parameters can automatically read from environment variables
with the format {PREFIX}_{PARAM_NAME}.
Sourcepub fn default_map(self, map: HashMap<String, BoxedValue>) -> Self
pub fn default_map(self, map: HashMap<String, BoxedValue>) -> Self
Set the default map.
This provides default values for parameters that override the parameter’s own default.
Sourcepub fn terminal_width(self, width: usize) -> Self
pub fn terminal_width(self, width: usize) -> Self
Set the terminal width.
Sourcepub fn max_content_width(self, width: usize) -> Self
pub fn max_content_width(self, width: usize) -> Self
Set the maximum content width for help text.
Sourcepub fn resilient_parsing(self, enabled: bool) -> Self
pub fn resilient_parsing(self, enabled: bool) -> Self
Enable or disable resilient parsing mode.
In resilient parsing mode, Click will parse without interactivity or callback invocation. This is useful for shell completion.
Sourcepub fn allow_extra_args(self, allow: bool) -> Self
pub fn allow_extra_args(self, allow: bool) -> Self
Set whether extra arguments are allowed.
Sourcepub fn allow_interspersed_args(self, allow: bool) -> Self
pub fn allow_interspersed_args(self, allow: bool) -> Self
Set whether interspersed arguments are allowed.
Sourcepub fn ignore_unknown_options(self, ignore: bool) -> Self
pub fn ignore_unknown_options(self, ignore: bool) -> Self
Set whether unknown options should be ignored.
Sourcepub fn help_option_names(self, names: Vec<String>) -> Self
pub fn help_option_names(self, names: Vec<String>) -> Self
Set the help option names.
Default is ["--help"].
Sourcepub fn show_default(self, show: bool) -> Self
pub fn show_default(self, show: bool) -> Self
Set the show_default setting.
Sourcepub fn help_renderer(self, renderer: HelpRenderer) -> Self
pub fn help_renderer(self, renderer: HelpRenderer) -> Self
Install a pluggable help renderer.
When installed on the root context, Group::invoke will call this
renderer for any subcommand’s --help request (Exit{0} from
make_context) instead of falling back to cmd.get_help().
The renderer is inherited by child contexts through the parent chain; you only need to install it once on the root.
§Example
use click::context::{ContextBuilder, HelpRenderer};
use std::sync::Arc;
let renderer: HelpRenderer = Arc::new(|cmd, ctx| {
format!("CUSTOM HELP: {}", ctx.info_name().unwrap_or(""))
});
let ctx = ContextBuilder::new()
.info_name("myapp")
.help_renderer(renderer)
.build();
assert!(ctx.help_renderer().is_some());