mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
chore: add sub_subcommands example and nu script to generate readme dynamically
This commit is contained in:
parent
ede83f161b
commit
d5bfa0cb67
3 changed files with 104 additions and 1 deletions
|
@ -15,7 +15,10 @@ keywords = [
|
||||||
"completion",
|
"completion",
|
||||||
"nushell"
|
"nushell"
|
||||||
]
|
]
|
||||||
exclude = [ "tests/*" ]
|
exclude = [
|
||||||
|
"tests/*",
|
||||||
|
"scripts/*"
|
||||||
|
]
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
|
49
examples/sub_subcommands.rs
Normal file
49
examples/sub_subcommands.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
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());
|
||||||
|
}
|
51
scripts/gen_readme.nu
Executable file
51
scripts/gen_readme.nu
Executable file
|
@ -0,0 +1,51 @@
|
||||||
|
#!/usr/bin/env nu
|
||||||
|
|
||||||
|
let example_file = "examples/sub_subcommands.rs"
|
||||||
|
|
||||||
|
def generate_url [baseurl: string, ...path: string] {
|
||||||
|
[$baseurl]
|
||||||
|
|append $path
|
||||||
|
|str join '/'
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_preamble [user: string, repo: string] {
|
||||||
|
let baseurl = "https://img.shields.io"
|
||||||
|
let crates = "https://crates.io"
|
||||||
|
let docs = "https://docs.rs"
|
||||||
|
let github = "https://github.com"
|
||||||
|
|
||||||
|
$"# ($repo)
|
||||||
|
|
||||||
|
Generates [Nushell]\((generate_url $github nushell/nushell)\) completions for [`clap`]\((generate_url $github clap-rs/clap)\) based CLIs
|
||||||
|
|
||||||
|
[![Crates.io]\((generate_url $baseurl crates/v $repo)\)]\((generate_url $crates crates $repo)\)
|
||||||
|
[![Crates.io]\((generate_url $baseurl crates/d $repo)\)]\((generate_url $crates crates $repo)\)
|
||||||
|
[![License]\((generate_url $baseurl github/license $user)/(generate_url $repo)\)]\(LICENSE\)
|
||||||
|
[![docs.rs]\((generate_url $baseurl docsrs $repo)\)]\((generate_url $docs $repo)\)
|
||||||
|
[![Build Status]\((generate_url $baseurl github/workflow/status $user $repo CI/master)\)]\((generate_url $github $user $repo actions/workflows/ci.yaml?query=branch%3Amaster)\)\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
def code_to_md [title: string, lang: string, code: string] {
|
||||||
|
$"### ($title)\n\n```($lang)\n($code)```\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_md [file: path] {
|
||||||
|
let package = (open Cargo.toml | get package)
|
||||||
|
let user = ($package.authors | first | str replace ' <.*@.*>$' '')
|
||||||
|
let repo = $package.name
|
||||||
|
let stem = ($file | path parse | get stem)
|
||||||
|
let rust_code = (open -r $file)
|
||||||
|
let nu_code = (cargo run --quiet --example $stem)
|
||||||
|
let rust_example = (code_to_md myapp.rs rust $rust_code)
|
||||||
|
let nu_example = (code_to_md myapp.nu nu $nu_code)
|
||||||
|
|
||||||
|
|generate_preamble $user $repo
|
||||||
|
|[$in]
|
||||||
|
|append "## Examples\n"
|
||||||
|
|append $rust_example
|
||||||
|
|append $nu_example
|
||||||
|
|str join "\n"
|
||||||
|
|save -r README.md
|
||||||
|
}
|
||||||
|
|
||||||
|
generate_md $example_file
|
Loading…
Reference in a new issue