chore: add sub_subcommands example and nu script to generate readme dynamically

This commit is contained in:
nibon7 2022-11-04 19:41:23 +08:00
parent ede83f161b
commit d5bfa0cb67
No known key found for this signature in database
GPG key ID: 281CE465D8EEC03B
3 changed files with 104 additions and 1 deletions

View file

@ -15,7 +15,10 @@ keywords = [
"completion",
"nushell"
]
exclude = [ "tests/*" ]
exclude = [
"tests/*",
"scripts/*"
]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View 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
View 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