Skip to main content

ContextBuilder

Struct ContextBuilder 

Source
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

Source

pub fn new() -> Self

Create a new context builder with default settings.

Source

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.

Source

pub fn info_name(self, name: impl Into<String>) -> Self

Set the info name for this context.

This is typically the command name.

Source

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.

Source

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}.

Source

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.

Source

pub fn terminal_width(self, width: usize) -> Self

Set the terminal width.

Source

pub fn max_content_width(self, width: usize) -> Self

Set the maximum content width for help text.

Source

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.

Source

pub fn allow_extra_args(self, allow: bool) -> Self

Set whether extra arguments are allowed.

Source

pub fn allow_interspersed_args(self, allow: bool) -> Self

Set whether interspersed arguments are allowed.

Source

pub fn ignore_unknown_options(self, ignore: bool) -> Self

Set whether unknown options should be ignored.

Source

pub fn help_option_names(self, names: Vec<String>) -> Self

Set the help option names.

Default is ["--help"].

Source

pub fn color(self, color: bool) -> Self

Set the color setting.

Source

pub fn show_default(self, show: bool) -> Self

Set the show_default setting.

Source

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());
Source

pub fn build(self) -> Context

Build the context.

Trait Implementations§

Source§

impl Default for ContextBuilder

Source§

fn default() -> ContextBuilder

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

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.