Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub enum ParseValue {
MultiString(Vec<String>),
}

/// Parse command-line arguments according to a spec.
///
/// Returns the parsed arguments and flags, with defaults and env vars applied.
#[must_use = "parsing result should be used"]
pub fn parse(spec: &Spec, input: &[String]) -> Result<ParseOutput, miette::Error> {
let mut out = parse_partial(spec, input)?;
trace!("{out:?}");
Expand Down Expand Up @@ -156,6 +160,10 @@ pub fn parse(spec: &Spec, input: &[String]) -> Result<ParseOutput, miette::Error
Ok(out)
}

/// Parse command-line arguments without applying defaults.
///
/// Use this for help text generation or when you need the raw parsed values.
#[must_use = "parsing result should be used"]
pub fn parse_partial(spec: &Spec, input: &[String]) -> Result<ParseOutput, miette::Error> {
trace!("parse_partial: {input:?}");
let mut input = input.iter().cloned().collect::<VecDeque<_>>();
Expand Down
3 changes: 3 additions & 0 deletions lib/src/spec/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl SpecFlagBuilder {
}

/// Build the final SpecFlag
#[must_use]
pub fn build(mut self) -> SpecFlag {
self.inner.usage = self.inner.usage();
if self.inner.name.is_empty() {
Expand Down Expand Up @@ -300,6 +301,7 @@ impl SpecArgBuilder {
}

/// Build the final SpecArg
#[must_use]
pub fn build(mut self) -> SpecArg {
self.inner.usage = self.inner.usage();
self.inner
Expand Down Expand Up @@ -428,6 +430,7 @@ impl SpecCommandBuilder {
}

/// Build the final SpecCommand
#[must_use]
pub fn build(mut self) -> SpecCommand {
self.inner.usage = self.inner.usage();
self.inner
Expand Down
15 changes: 14 additions & 1 deletion lib/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub struct Spec {
}

impl Spec {
/// Parse a spec from a file (either a .usage.kdl file or a script with embedded spec).
#[must_use = "parsing result should be used"]
pub fn parse_file(file: &Path) -> Result<Spec, UsageErr> {
let spec = split_script(file)?;
let ctx = ParsingContext::new(file, &spec);
Expand All @@ -77,6 +79,8 @@ impl Spec {
}
Ok(schema)
}
/// Parse a spec from a script file's USAGE comments.
#[must_use = "parsing result should be used"]
pub fn parse_script(file: &Path) -> Result<Spec, UsageErr> {
let raw = extract_usage_from_comments(&file::read_to_string(file)?);
let ctx = ParsingContext::new(file, &raw);
Expand Down Expand Up @@ -177,7 +181,16 @@ impl Spec {
.ok_or_else(|| ctx.build_err("missing file".into(), node.span()))?;
let file = Path::new(&file);
let file = match file.is_relative() {
true => ctx.file.parent().unwrap().join(file),
true => ctx
.file
.parent()
.ok_or_else(|| {
ctx.build_err(
format!("cannot get parent of {}", ctx.file.display()),
node.span(),
)
})?
.join(file),
false => file.to_path_buf(),
};
info!("include: {}", file.display());
Expand Down