Skip to main content

ArgumentBuilder

Struct ArgumentBuilder 

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

Builder for creating Argument instances.

Use Argument::new(name) to create a builder, then chain methods to configure the argument, and finally call build() to create the Argument.

§Example

use click::argument::Argument;
use click::parameter::Nargs;

let arg = Argument::new("files")
    .help("Files to process")
    .nargs(Nargs::Variadic)
    .required(false)
    .build();

Implementations§

Source§

impl ArgumentBuilder

Source

pub fn help(self, help: &str) -> Self

Set the help text for this argument.

Source

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

Set whether this argument is required.

By default, arguments are required unless a default value is provided.

Source

pub fn default(self, value: impl Into<String>) -> Self

Set the default value for this argument.

Setting a default value automatically makes the argument optional (unless required(true) is explicitly called).

Source

pub fn envvar(self, name: &str) -> Self

Set an environment variable for this argument.

If the argument is not provided on the command line, the value will be read from this environment variable.

Source

pub fn envvars(self, names: impl IntoIterator<Item = impl Into<String>>) -> Self

Set multiple environment variables for this argument.

The first non-empty environment variable value is used.

Source

pub fn nargs(self, n: Nargs) -> Self

Set how many values this argument consumes.

Source

pub fn callback<F>(self, callback: F) -> Self
where F: Fn(&Context, &dyn Parameter, Arc<dyn Any + Send + Sync>) -> Result<Arc<dyn Any + Send + Sync>, ClickError> + Send + Sync + 'static,

Set a callback invoked after conversion.

Source

pub fn multiple(self) -> Self

Make this argument variadic (consume all remaining arguments).

Equivalent to nargs(Nargs::Variadic).

Source

pub fn type_<T>(self, type_: T) -> Self
where T: TypeConverter + Send + Sync + 'static, T::Value: Send + Sync + 'static,

Set the type converter for this argument.

By default, arguments use STRING which passes values through unchanged. This method accepts any TypeConverter - the converted value will be stored as Box<dyn Any> in the context and can be retrieved with ctx.get_param::<T>().

§Example
use click::argument::Argument;
use click::types::{INT, FileType};

// Integer argument
let count = Argument::new("count")
    .type_(INT)
    .build();

// File argument (opens the file)
let input = Argument::new("input")
    .type_(FileType::new())
    .build();
Source

pub fn shell_complete<F>(self, callback: F) -> Self
where F: Fn(&Context, &str) -> Vec<CompletionItem> + Send + Sync + 'static,

Set a custom shell completion callback for this argument.

The callback receives the current context and the incomplete string being typed, and should return a list of completion items.

§Example
use click::argument::Argument;
use click::types::CompletionItem;

let arg = Argument::new("filename")
    .shell_complete(|_ctx, incomplete| {
        // Return file completions matching the prefix
        vec![
            CompletionItem::new(format!("{}.txt", incomplete)),
            CompletionItem::new(format!("{}.md", incomplete)),
        ]
    })
    .build();
Source

pub fn metavar(self, metavar: &str) -> Self

Set a custom metavar for help text.

By default, the uppercase name is used.

Source

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

Hide this argument from help output.

Source

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

Set whether this argument is eager (processed before others).

Source

pub fn expose_value(self, expose: bool) -> Self

Set whether this argument’s value is exposed in ctx.params.

Source

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

Mark this argument as deprecated.

Source

pub fn deprecated_with_message(self, message: impl Into<String>) -> Self

Mark this argument as deprecated with a custom message.

Source

pub fn build(self) -> Argument

Build the Argument.

This method applies the following defaults:

  • If required was not explicitly set:
    • If a default value is provided, the argument is optional
    • Otherwise, the argument is required (if nargs > 0)
  • The type converter defaults to STRING

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.