Skip to main content

CompleteEnv

Struct CompleteEnv 

Source
pub struct CompleteEnv<'s, F> { /* private fields */ }
Available on crate feature unstable-dynamic only.
Expand description

Environment-activated completions for your CLI

Benefits over a CLI completion argument or subcommand

  • Performance: we don’t need to generate clap::Command twice or parse arguments
  • Flexibility: there is no concern over it interfering with other CLI logic

Warning: stdout should not be written to before CompleteEnv::complete has had a chance to run.

§Examples

fn cli() -> clap::Command {
    // ...
}

CompleteEnv::with_factory(cli)
    .complete()

// ... rest of application logic

Implementations§

Source§

impl<'s, F: Fn() -> Command> CompleteEnv<'s, F>

Source

pub fn with_factory(factory: F) -> Self

Complete a clap::Command

§Example

Builder:

fn cli() -> clap::Command {
    // ...
}

CompleteEnv::with_factory(cli)
    .complete()

// ... rest of application logic

Derive:

use clap::CommandFactory as _;

#[derive(Debug, Parser)]
struct Cli {
    custom: Option<String>,
}

CompleteEnv::with_factory(Cli::command)
    .complete()

// ... rest of application logic
Source

pub fn var(self, var: &'static str) -> Self

Override the environment variable used for enabling completions

Source

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

Override the name of the binary to complete

Default: Command::get_bin_name

Source

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

Override the binary to call to get completions

Default: args_os()[0]

Source

pub fn shells(self, shells: Shells<'s>) -> Self

Override the shells supported for completions

Source§

impl<'s, F: Fn() -> Command> CompleteEnv<'s, F>

Source

pub fn complete(self)

Process the completion request and exit

Warning: stdout should not be written to before this has had a chance to run.

Source

pub fn try_complete( self, args: impl IntoIterator<Item = impl Into<OsString>>, current_dir: Option<&Path>, ) -> Result<bool>

Process the completion request

Warning: stdout should not be written to before or after this has run.

Returns true if a command was completed and false if this is a regular run of your application

Auto Trait Implementations§

§

impl<'s, F> Freeze for CompleteEnv<'s, F>
where F: Freeze,

§

impl<'s, F> !RefUnwindSafe for CompleteEnv<'s, F>

§

impl<'s, F> !Send for CompleteEnv<'s, F>

§

impl<'s, F> !Sync for CompleteEnv<'s, F>

§

impl<'s, F> Unpin for CompleteEnv<'s, F>
where F: Unpin,

§

impl<'s, F> UnsafeUnpin for CompleteEnv<'s, F>
where F: UnsafeUnpin,

§

impl<'s, F> !UnwindSafe for CompleteEnv<'s, F>

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.