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
impl ArgumentBuilder
Sourcepub fn required(self, required: bool) -> Self
pub fn required(self, required: bool) -> Self
Set whether this argument is required.
By default, arguments are required unless a default value is provided.
Sourcepub fn default(self, value: impl Into<String>) -> Self
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).
Sourcepub fn envvar(self, name: &str) -> Self
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.
Sourcepub fn envvars(self, names: impl IntoIterator<Item = impl Into<String>>) -> Self
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.
Sourcepub fn multiple(self) -> Self
pub fn multiple(self) -> Self
Make this argument variadic (consume all remaining arguments).
Equivalent to nargs(Nargs::Variadic).
Sourcepub fn type_<T>(self, type_: T) -> Self
pub fn type_<T>(self, type_: T) -> Self
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();Sourcepub fn shell_complete<F>(self, callback: F) -> Self
pub fn shell_complete<F>(self, callback: F) -> Self
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();Sourcepub fn metavar(self, metavar: &str) -> Self
pub fn metavar(self, metavar: &str) -> Self
Set a custom metavar for help text.
By default, the uppercase name is used.
Hide this argument from help output.
Sourcepub fn eager(self, eager: bool) -> Self
pub fn eager(self, eager: bool) -> Self
Set whether this argument is eager (processed before others).
Sourcepub fn expose_value(self, expose: bool) -> Self
pub fn expose_value(self, expose: bool) -> Self
Set whether this argument’s value is exposed in ctx.params.
Sourcepub fn deprecated(self, deprecated: bool) -> Self
pub fn deprecated(self, deprecated: bool) -> Self
Mark this argument as deprecated.
Sourcepub fn deprecated_with_message(self, message: impl Into<String>) -> Self
pub fn deprecated_with_message(self, message: impl Into<String>) -> Self
Mark this argument as deprecated with a custom message.