Skip to main content

CompletionFunc

Type Alias CompletionFunc 

Source
pub type CompletionFunc = Box<dyn Fn(&Context, &str) -> Result<CompletionResult> + Send + Sync>;
Expand description

Type alias for completion functions

Completion functions are called when the user presses TAB to get suggestions. They receive the current context and the partial text being completed.

§Arguments

  • &Context - The current command context with flags and arguments
  • &str - The partial text being completed

§Returns

Returns a Result<CompletionResult> with the suggested completions

§Examples

use flag_rs::completion::{CompletionFunc, CompletionResult};
use flag_rs::context::Context;
use flag_rs::error::Result;

// A completion function that suggests file names
let file_completer: CompletionFunc = Box::new(|_ctx, partial| {
    Ok(CompletionResult::new()
        .add("file1.txt")
        .add("file2.txt")
        .add("file3.log"))
});

// A dynamic completion function that uses context
let pod_completer: CompletionFunc = Box::new(|ctx, partial| {
    // In a real implementation, this would query the Kubernetes API
    let namespace = ctx.flag("namespace")
        .map(|s| s.as_str())
        .unwrap_or("default");
    Ok(CompletionResult::new()
        .add_with_description("pod-abc-123", format!("Pod in namespace {}", namespace))
        .add_with_description("pod-def-456", format!("Pod in namespace {}", namespace)))
});

Aliased Type§

pub struct CompletionFunc(/* private fields */);