From d5bfa0cb67b02a7c9e132ebb4508d2c925f27d4e Mon Sep 17 00:00:00 2001 From: nibon7 Date: Fri, 4 Nov 2022 19:41:23 +0800 Subject: [PATCH] chore: add sub_subcommands example and nu script to generate readme dynamically --- Cargo.toml | 5 +++- examples/sub_subcommands.rs | 49 +++++++++++++++++++++++++++++++++++ scripts/gen_readme.nu | 51 +++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 examples/sub_subcommands.rs create mode 100755 scripts/gen_readme.nu diff --git a/Cargo.toml b/Cargo.toml index 1f2b83bb..67b94de6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/examples/sub_subcommands.rs b/examples/sub_subcommands.rs new file mode 100644 index 00000000..e09b7a46 --- /dev/null +++ b/examples/sub_subcommands.rs @@ -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()); +} diff --git a/scripts/gen_readme.nu b/scripts/gen_readme.nu new file mode 100755 index 00000000..87c97d52 --- /dev/null +++ b/scripts/gen_readme.nu @@ -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