Skip to content

Commit

Permalink
feat: basic --help support
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Oct 12, 2024
1 parent 0e5ef11 commit 394df50
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 5 deletions.
15 changes: 14 additions & 1 deletion cli/src/cli/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@ use miette::IntoDiagnostic;
use usage::Spec;

#[derive(Debug, Args)]
#[clap()]
#[clap(disable_help_flag = true)]
pub struct Bash {
script: PathBuf,
/// arguments to pass to script
#[clap(allow_hyphen_values = true)]
args: Vec<String>,

#[clap(short, long)]
help: bool,
}

impl Bash {
pub fn run(&mut self) -> miette::Result<()> {
let (spec, _script) = Spec::parse_file(&self.script)?;
let mut args = self.args.clone();
args.insert(0, spec.bin.clone());

if self.help {
return self.help(&spec, &args);
}

let parsed = usage::parse::parse(&spec, &args)?;

let mut cmd = std::process::Command::new("bash");
Expand All @@ -44,4 +52,9 @@ impl Bash {

Ok(())
}

pub fn help(&self, spec: &Spec, _args: &[String]) -> miette::Result<()> {
println!("{}", usage::docs::cli::render_help(spec));
Ok(())
}
}
17 changes: 17 additions & 0 deletions examples/multicmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env -S usage bash
#USAGE cmd "apply" {
#USAGE arg "<file>"
#USAGE }
#USAGE cmd "select" {}

function apply() {
echo "Applying theme $1"
}
function select_theme() {
echo "Selecting theme $1"
}

case "$1" in
"apply") apply "$2" ;;
"select") select_theme "$2" ;;
esac
33 changes: 33 additions & 0 deletions lib/src/docs/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::Spec;
use once_cell::sync::Lazy;
use tera::Tera;

pub fn render_help(spec: &Spec) -> String {
let mut ctx = tera::Context::new();
ctx.insert("spec", spec);
TERA.render("spec_template.md.tera", &ctx)
.unwrap()
.trim()
.to_string()
+ "\n"
}

static TERA: Lazy<Tera> = Lazy::new(|| {
let mut tera = Tera::default();

#[rustfmt::skip]
tera.add_raw_templates([
("spec_template.md.tera", include_str!("templates/spec_template.tera")),
]).unwrap();

// tera.register_filter(
// "repeat",
// move |value: &tera::Value, args: &HashMap<String, tera::Value>| {
// let value = value.as_str().unwrap();
// let count = args.get("count").unwrap().as_u64().unwrap();
// Ok(value.repeat(count as usize).into())
// },
// );

tera
});
4 changes: 0 additions & 4 deletions lib/src/docs/cli/spec_template.tera

This file was deleted.

File renamed without changes.
31 changes: 31 additions & 0 deletions lib/src/docs/cli/templates/spec_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{%- if spec.about_long %}{{ spec.about_long }}

{% elif spec.about %}{{ spec.about }}

{% endif %}
Usage: {{ spec.bin ~ " " ~ spec.cmd.usage | trim }}

{%- if spec.cmd.subcommands %}

Commands:
{%- for name, cmd in spec.cmd.subcommands %}
{{ cmd.usage | trim }}{% if cmd.aliases %} [aliases: {{ cmd.aliases | join(sep=", ") }}]{% endif %}
{%- endfor %}
help Print this message or the help of the given subcommand(s)
{%- endif %}

{%- if spec.cmd.args %}

Arguments:
{%- for arg in spec.cmd.args %}
{{ arg.usage | trim }}
{%- endfor %}
{%- endif %}

{%- if spec.cmd.flags %}

Flags:
{%- for flag in spec.cmd.flags %}
{{ flag.usage | trim }}{% if flag.aliases %} [aliases: {{ flag.aliases | join(sep=", ") }}]{% endif %}
{%- endfor %}
{%- endif %}
1 change: 1 addition & 0 deletions lib/src/docs/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod cli;
pub mod markdown;

0 comments on commit 394df50

Please sign in to comment.