Files
jaarg/jaarg/src/std.rs

79 lines
2.9 KiB
Rust
Raw Normal View History

/* jaarg - Argument parser
* SPDX-FileCopyrightText: (C) 2025 Gay Pizza Specifications
* SPDX-License-Identifier: MIT OR Apache-2.0
*/
extern crate std;
2025-11-07 06:41:34 +11:00
use crate::{
ErrorUsageWriter, ErrorUsageWriterContext, HandlerResult, HelpWriter, HelpWriterContext, Opt, Opts,
ParseControl, ParseError, ParseResult, StandardErrorUsageWriter, StandardFullHelpWriter, alloc::ParseMapResult
};
2025-11-01 16:19:38 +11:00
use std::path::Path;
use std::rc::Rc;
2025-11-05 07:48:15 +11:00
use std::{env, eprint, print};
impl<ID: 'static> Opts<ID> {
2025-11-02 22:22:42 +11:00
/// Wrapper around [Opts::parse] that gathers arguments from the command line and prints errors to stderr.
2025-11-01 16:19:38 +11:00
/// The errors are formatted in a standard user-friendly format.
///
2025-11-05 07:32:32 +11:00
/// Requires `features = ["std"]`.
2025-11-01 16:19:38 +11:00
pub fn parse_easy<'a>(&self, handler: impl FnMut(&str, &ID, &Opt<ID>, &str, &str) -> HandlerResult<'a, ParseControl>
) -> ParseResult {
let (program_name, argv) = Self::easy_args();
2025-11-05 07:48:15 +11:00
self.parse(&program_name, argv, handler,
|name, e| self.eprint_usage::<StandardErrorUsageWriter<'_, ID>>(name, e))
2025-11-01 16:19:38 +11:00
}
2025-11-02 22:22:42 +11:00
/// Prints full help text for the options using the standard full.
2025-11-01 16:19:38 +11:00
///
2025-11-05 07:32:32 +11:00
/// Requires `features = ["std"]`.
2025-11-01 16:19:38 +11:00
pub fn print_full_help(&self, program_name: &str) {
self.print_help::<StandardFullHelpWriter<'_, ID>>(program_name);
}
2025-11-02 22:22:42 +11:00
/// Print help text to stdout using the provided help writer.
2025-11-01 16:19:38 +11:00
///
2025-11-05 07:32:32 +11:00
/// Requires `features = ["std"]`.
2025-11-01 16:19:38 +11:00
pub fn print_help<'a, W: HelpWriter<'a, ID>>(&'a self, program_name: &'a str) {
let ctx = HelpWriterContext { options: self, program_name };
print!("{}", W::new(ctx));
2025-11-01 16:19:38 +11:00
}
2025-11-02 22:22:42 +11:00
/// Print help text to stderr using the provided help writer.
2025-11-01 16:19:38 +11:00
///
2025-11-05 07:32:32 +11:00
/// Requires `features = ["std"]`.
2025-11-01 16:19:38 +11:00
pub fn eprint_help<'a, W: HelpWriter<'a, ID>>(&'a self, program_name: &'a str) {
let ctx = HelpWriterContext { options: self, program_name };
eprint!("{}", W::new(ctx));
}
2025-11-05 07:48:15 +11:00
/// Print error & usage text to stderr using the provided error & usage writer.
///
/// Requires `features = ["std"]`.
pub fn eprint_usage<'a, W: ErrorUsageWriter<'a, ID>>(&'a self, program_name: &'a str, error: ParseError<'a>) {
let ctx = ErrorUsageWriterContext { options: self, program_name, error };
eprint!("{}", W::new(ctx));
}
fn easy_args() -> (Rc<str>, env::Args) {
let mut argv = env::args();
let argv0 = argv.next().unwrap();
let program_name = Path::new(&argv0).file_name().unwrap().to_string_lossy();
(program_name.into(), argv)
}
}
impl Opts<&'static str> {
2025-11-05 07:32:32 +11:00
/// Parse arguments from the command line and return the results in a [`alloc::collections::BTreeMap`].
/// Help and errors are formatted in a standard user-friendly format.
///
2025-11-05 07:32:32 +11:00
/// Requires `features = ["std"]`.
pub fn parse_map_easy(&self) -> ParseMapResult {
let (program_name, argv) = Self::easy_args();
self.parse_map(&program_name, argv,
|name| self.print_full_help(name),
2025-11-05 07:48:15 +11:00
|name, e| self.eprint_usage::<StandardErrorUsageWriter<'_, &'static str>>(name, e))
}
}