12 releases (2 stable)

1.1.0 Sep 15, 2024
1.0.0 Mar 12, 2024
0.1.12 Jul 17, 2023
0.1.11 Mar 25, 2023
0.1.9 Oct 24, 2022

#246 in Command-line interface

Download history 36/week @ 2024-08-22 111/week @ 2024-08-29 343/week @ 2024-09-05 659/week @ 2024-09-12 512/week @ 2024-09-19 569/week @ 2024-09-26 534/week @ 2024-10-03 596/week @ 2024-10-10 575/week @ 2024-10-17 404/week @ 2024-10-24 532/week @ 2024-10-31 406/week @ 2024-11-07 423/week @ 2024-11-14 421/week @ 2024-11-21 591/week @ 2024-11-28 478/week @ 2024-12-05

1,965 downloads per month
Used in 8 crates (3 directly)

MIT license

16KB
229 lines

carapace-spec-clap

Crates.io

Spec generation for clap-rs/clap

asciicast

use carapace_spec_clap::Spec;
use clap::{Arg, ArgAction, Command, ValueHint};
use clap_complete::generate;
use std::io;

fn main() {
    let mut cmd = Command::new("example")
        .aliases(["alias1", "alias2"])
        .about("example command")
        .arg(
            Arg::new("help")
                .long("help")
                .short('h')
                .help("show help")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("optional")
                .long("optional")
                .help("optional argument")
                .require_equals(true)
                .value_hint(ValueHint::Username),
        )
        .arg(
            Arg::new("value")
                .short('v')
                .help("takes argument")
                .value_parser(["one", "two", "three"]),
        )
        .subcommand(
            Command::new("subcommand")
                .about("example subcommand")
                .arg(
                    Arg::new("command")
                        .long("command")
                        .short('c')
                        .help("execute command")
                        .value_hint(ValueHint::CommandName),
                )
                .arg(
                    Arg::new("pos1")
                        .value_parser(["four", "five", "six"])
                        .value_hint(ValueHint::DirPath),
                )
                .arg(
                    Arg::new("posAny")
                        .num_args(1..)
                        .value_hint(ValueHint::Hostname),
                ),
        );

    generate(Spec, &mut cmd, "example", &mut io::stdout());
}
name: example
aliases:
- alias1
- alias2
description: example command
flags:
  -h, --help: show help
  --optional?: optional argument
  -v=: takes argument
completion:
  flag:
    optional:
    - $_os.Users
    v:
    - one
    - two
    - three
commands:
- name: subcommand
  description: example subcommand
  flags:
    -c, --command=: execute command
  completion:
    flag:
      command:
      - $_os.PathExecutables
      - $files
    positional:
    - - $directories
      - four
      - five
      - six
    positionalany:
    - $_net.Hosts

Dependencies

~4MB
~75K SLoC