#nushell #completion #clap

clap_complete_nushell

A generator library used with clap for Nushell completion scripts

23 releases (11 stable)

4.5.4 Oct 8, 2024
4.5.3 Jul 25, 2024
4.5.2 Jun 6, 2024
4.5.1 Feb 16, 2024
0.1.8 Nov 5, 2022

#329 in Command-line interface

Download history 30349/week @ 2024-08-06 30798/week @ 2024-08-13 32628/week @ 2024-08-20 29775/week @ 2024-08-27 37640/week @ 2024-09-03 32458/week @ 2024-09-10 29399/week @ 2024-09-17 31106/week @ 2024-09-24 27377/week @ 2024-10-01 37385/week @ 2024-10-08 35274/week @ 2024-10-15 29685/week @ 2024-10-22 29739/week @ 2024-10-29 32072/week @ 2024-11-05 35667/week @ 2024-11-12 34761/week @ 2024-11-19

137,688 downloads per month
Used in 44 crates (38 directly)

MIT/Apache and maybe CC-PDDC

270KB
3.5K SLoC

clap_complete_nushell

Generates Nushell completions for clap based CLIs

Crates.io Crates.io License License docs.rs

Examples

myapp.rs

use clap::{builder::PossibleValue, Arg, ArgAction, Command, ValueHint};
use clap_complete::generate;
use clap_complete_nushell::Nushell;
use std::io;

fn main() {
    let mut cmd = Command::new("myapp")
        .version("3.0")
        .propagate_version(true)
        .about("Tests completions")
        .arg(
            Arg::new("file")
                .value_hint(ValueHint::FilePath)
                .help("some input file"),
        )
        .arg(
            Arg::new("config")
                .action(ArgAction::Count)
                .help("some config file")
                .short('c')
                .visible_short_alias('C')
                .long("config")
                .visible_alias("conf"),
        )
        .arg(Arg::new("choice").value_parser(["first", "second"]))
        .subcommand(
            Command::new("test").about("tests things").arg(
                Arg::new("case")
                    .long("case")
                    .action(ArgAction::Set)
                    .help("the case to test"),
            ),
        )
        .subcommand(
            Command::new("some_cmd")
                .about("top level subcommand")
                .subcommand(
                    Command::new("sub_cmd").about("sub-subcommand").arg(
                        Arg::new("config")
                            .long("config")
                            .action(ArgAction::Set)
                            .value_parser([PossibleValue::new("Lest quotes aren't escaped.")])
                            .help("the other case to test"),
                    ),
                ),
        );

    generate(Nushell, &mut cmd, "myapp", &mut io::stdout());
}

myapp.nu

module completions {

  def "nu-complete myapp choice" [] {
    [ "first" "second" ]
  }

  # Tests completions
  export extern myapp [
    file?: string             # some input file
    --config(-c)              # some config file
    --conf                    # some config file
    -C                        # some config file
    choice?: string@"nu-complete myapp choice"
    --version(-V)             # Print version
  ]

  # tests things
  export extern "myapp test" [
    --case: string            # the case to test
    --version(-V)             # Print version
  ]

  # top level subcommand
  export extern "myapp some_cmd" [
    --version(-V)             # Print version
  ]

  def "nu-complete myapp some_cmd sub_cmd config" [] {
    [ "\"Lest quotes aren't escaped.\"" ]
  }

  # sub-subcommand
  export extern "myapp some_cmd sub_cmd" [
    --config: string@"nu-complete myapp some_cmd sub_cmd config" # the other case to test
    --version(-V)             # Print version
  ]

}

use completions *

Dependencies

~0.8–39MB
~631K SLoC