#clap #completion #nushell #arguments-parser #cli

clap_complete_nushell

A generator library used with clap for Nushell completion scripts

20 releases (8 stable)

4.5.1 Feb 16, 2024
4.4.2 Oct 24, 2023
4.3.1 Jun 2, 2023
0.1.11 May 23, 2023
0.1.8 Nov 5, 2022

#176 in Command-line interface

Download history 9092/week @ 2024-01-02 10257/week @ 2024-01-09 10358/week @ 2024-01-16 10214/week @ 2024-01-23 11194/week @ 2024-01-30 11737/week @ 2024-02-06 11519/week @ 2024-02-13 18465/week @ 2024-02-20 24594/week @ 2024-02-27 31413/week @ 2024-03-05 40559/week @ 2024-03-12 39404/week @ 2024-03-19 18862/week @ 2024-03-26 17591/week @ 2024-04-02 17210/week @ 2024-04-09 15151/week @ 2024-04-16

71,807 downloads per month
Used in 34 crates (27 directly)

MIT/Apache

210KB
2.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

~1MB
~15K SLoC