Skip to content

Commit

Permalink
fix: allow building without docs feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Oct 14, 2024
1 parent 4ab0088 commit 212f96c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ xx = "1"
regex = "1"

[features]
default = []
default = ["docs"]
docs = ["tera"]

[dev-dependencies]
Expand Down
30 changes: 19 additions & 11 deletions lib/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use std::collections::{BTreeMap, VecDeque};
use std::fmt::{Debug, Display, Formatter};
use strum::EnumTryAs;

#[cfg(feature = "docs")]
use crate::docs;
use crate::error::UsageErr;
use crate::{docs, Spec, SpecArg, SpecCommand, SpecFlag};
use crate::{Spec, SpecArg, SpecCommand, SpecFlag};

pub struct ParseOutput {
pub cmd: SpecCommand,
Expand Down Expand Up @@ -108,9 +110,8 @@ pub fn parse_partial(spec: &Spec, input: &[String]) -> Result<ParseOutput, miett
if let Some(choices) = &arg.choices {
if !choices.choices.contains(&w) {
if is_help_arg(spec, &w) {
let long = w.len() > 2;
out.errors
.push(UsageErr::Help(docs::cli::render_help(spec, &out.cmd, long)));
.push(render_help_err(spec, &out.cmd, w.len() > 2));
return Ok(out);
}
bail!(
Expand Down Expand Up @@ -155,7 +156,7 @@ pub fn parse_partial(spec: &Spec, input: &[String]) -> Result<ParseOutput, miett
}
if is_help_arg(spec, &w) {
out.errors
.push(UsageErr::Help(docs::cli::render_help(spec, &out.cmd, true)));
.push(render_help_err(spec, &out.cmd, w.len() > 2));
return Ok(out);
}
}
Expand Down Expand Up @@ -187,9 +188,8 @@ pub fn parse_partial(spec: &Spec, input: &[String]) -> Result<ParseOutput, miett
continue;
}
if is_help_arg(spec, &w) {
out.errors.push(UsageErr::Help(docs::cli::render_help(
spec, &out.cmd, false,
)));
out.errors
.push(render_help_err(spec, &out.cmd, w.len() > 2));
return Ok(out);
}
}
Expand All @@ -210,9 +210,8 @@ pub fn parse_partial(spec: &Spec, input: &[String]) -> Result<ParseOutput, miett
if let Some(choices) = &arg.choices {
if !choices.choices.contains(&w) {
if is_help_arg(spec, &w) {
let long = w.len() > 2;
out.errors
.push(UsageErr::Help(docs::cli::render_help(spec, &out.cmd, long)));
.push(render_help_err(spec, &out.cmd, w.len() > 2));
return Ok(out);
}
bail!(
Expand All @@ -228,9 +227,8 @@ pub fn parse_partial(spec: &Spec, input: &[String]) -> Result<ParseOutput, miett
continue;
}
if is_help_arg(spec, &w) {
let long = w.len() > 2;
out.errors
.push(UsageErr::Help(docs::cli::render_help(spec, &out.cmd, long)));
.push(render_help_err(spec, &out.cmd, w.len() > 2));
return Ok(out);
}
bail!("unexpected word: {w}");
Expand All @@ -251,6 +249,16 @@ pub fn parse_partial(spec: &Spec, input: &[String]) -> Result<ParseOutput, miett
Ok(out)
}

#[cfg(feature = "docs")]
fn render_help_err(spec: &Spec, cmd: &SpecCommand, long: bool) -> UsageErr {
UsageErr::Help(docs::cli::render_help(spec, cmd, long))
}

#[cfg(not(feature = "docs"))]
fn render_help_err(_spec: &Spec, _cmd: &SpecCommand, _long: bool) -> UsageErr {
UsageErr::Help("help".to_string())
}

fn is_help_arg(spec: &Spec, w: &str) -> bool {
spec.disable_help != Some(true)
&& (w == "--help"
Expand Down

0 comments on commit 212f96c

Please sign in to comment.