Skip to main content

Argument

Struct Argument 

Source
pub struct Argument {
    pub config: ParameterConfig,
    pub default_value: Option<String>,
    /* private fields */
}
Expand description

A positional command-line argument.

Arguments are positional parameters that appear without dashes. Unlike options, they are required by default and have a single name (no aliases).

§Key Differences from Options

  • Arguments are positional (no -- prefix)
  • Required by default (unless a default is provided)
  • Single name only (no short/long aliases)
  • Name is displayed in uppercase in help text

§Example

use click::argument::Argument;
use click::parameter::Parameter;

// Required argument
let filename = Argument::new("filename").build();
assert!(filename.required());

// Optional argument with default
let output = Argument::new("output")
    .default("out.txt")
    .build();
assert!(!output.required());

Fields§

§config: ParameterConfig

Parameter configuration.

§default_value: Option<String>

The default value (if any).

Implementations§

Source§

impl Argument

Source

pub fn new(name: &str) -> ArgumentBuilder

Create a new argument builder with the given name.

§Example
use click::argument::Argument;

let arg = Argument::builder("filename")
    .help("The file to process")
    .required(true)
    .build();
Source

pub fn builder(name: &str) -> ArgumentBuilder

Alias for new() - create a new argument builder.

Source

pub fn type_converter(&self) -> &dyn AnyTypeConverter

Get the type converter for this argument.

Source

pub fn convert_any( &self, value: &str, ) -> Result<Box<dyn Any + Send + Sync>, String>

Convert a string value using this argument’s type converter. Returns the converted value as a boxed Any type.

Source

pub fn convert(&self, value: &str) -> Result<String, String>

Convert a string value using this argument’s type converter. This is a convenience method that attempts to downcast to String. For non-String types, use convert_any instead.

Source

pub fn get_completions( &self, ctx: &Context, incomplete: &str, ) -> Vec<CompletionItem>

Get shell completions for this argument.

If a custom shell_complete callback is set, it will be used. Otherwise, completions from the type converter are returned.

Source

pub fn has_shell_complete_callback(&self) -> bool

Check if this argument has a custom shell_complete callback.

Source

pub fn default_value(&self) -> Option<&str>

Get the default value for this argument.

Source

pub fn make_metavar(&self) -> String

Generate the metavar for this argument (used in help text).

The metavar is formatted according to Click’s conventions:

  • Custom metavar if set, otherwise uppercase name
  • Wrapped in [] if optional
  • Suffixed with ... if variadic or multiple
  • Suffixed with ! if deprecated

Note: Unlike options, arguments use their name (uppercase) by default, not the type’s metavar. This matches Python Click’s behavior.

Trait Implementations§

Source§

impl Debug for Argument

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Parameter for Argument

Source§

fn name(&self) -> &str

The primary name of the parameter. Read more
Source§

fn human_readable_name(&self) -> String

Human-readable name for errors and help text. Read more
Source§

fn nargs(&self) -> Nargs

Number of arguments this parameter consumes.
Source§

fn multiple(&self) -> bool

Whether this parameter can be specified multiple times. Read more
Source§

fn is_eager(&self) -> bool

Whether this parameter should be processed before others. Read more
Source§

fn expose_value(&self) -> bool

Whether this parameter’s value should be exposed in ctx.params. Read more
Source§

fn required(&self) -> bool

Whether this parameter is required. Read more
Source§

fn envvar(&self) -> Option<&[String]>

Get environment variable name(s) for this parameter. Read more
Source§

fn help(&self) -> Option<&str>

Get help text for this parameter.
Source§

fn hidden(&self) -> bool

Whether this parameter is hidden from help.
Source§

fn get_metavar(&self) -> Option<String>

Get the metavar for help text (e.g., “FILE”, “TEXT”). Read more
Source§

fn get_help_record(&self) -> Option<(String, String)>

Generate help record for formatting. Read more
Source§

fn param_type_name(&self) -> &str

Get the parameter type name (for error messages).

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.