2025-10-28 18:19:51 +11:00
|
|
|
/* jaarg - Argument parser
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2025 Gay Pizza Specifications
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
extern crate std;
|
|
|
|
|
|
2025-11-01 21:59:05 +11:00
|
|
|
use crate::{HandlerResult, HelpWriter, HelpWriterContext, Opt, Opts, ParseControl, ParseError, ParseResult, StandardFullHelpWriter, StandardShortUsageWriter};
|
|
|
|
|
use std::collections::BTreeMap;
|
2025-11-01 16:19:38 +11:00
|
|
|
use std::path::Path;
|
2025-11-01 21:59:05 +11:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
use std::string::String;
|
|
|
|
|
use std::{env, eprintln, println};
|
2025-10-28 18:19:51 +11:00
|
|
|
|
|
|
|
|
impl<ID: 'static> Opts<ID> {
|
2025-11-01 16:19:38 +11:00
|
|
|
/// Wrapper around `jaarg::parse` that gathers arguments from the command line and prints errors to stderr.
|
|
|
|
|
/// The errors are formatted in a standard user-friendly format.
|
2025-10-28 18:19:51 +11:00
|
|
|
///
|
2025-11-01 16:19:38 +11:00
|
|
|
/// Requires features = [std]
|
|
|
|
|
pub fn parse_easy<'a>(&self, handler: impl FnMut(&str, &ID, &Opt<ID>, &str, &str) -> HandlerResult<'a, ParseControl>
|
2025-10-28 18:19:51 +11:00
|
|
|
) -> ParseResult {
|
2025-11-01 21:59:05 +11:00
|
|
|
let (program_name, argv) = Self::easy_args();
|
|
|
|
|
self.parse(&program_name, argv, handler, |name, e| self.easy_error(name, e))
|
2025-11-01 16:19:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Prints full help text for the options using the standard full
|
|
|
|
|
///
|
|
|
|
|
/// Requires features = [std]
|
|
|
|
|
pub fn print_full_help(&self, program_name: &str) {
|
|
|
|
|
self.print_help::<StandardFullHelpWriter<'_, ID>>(program_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Print help text to stdout using the provided help writer
|
|
|
|
|
///
|
|
|
|
|
/// Requires features = [std]
|
|
|
|
|
pub fn print_help<'a, W: HelpWriter<'a, ID>>(&'a self, program_name: &'a str) {
|
|
|
|
|
let ctx = HelpWriterContext { options: self, program_name };
|
|
|
|
|
println!("{}", W::new(ctx));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Print help text to stderr using the provided help writer
|
|
|
|
|
///
|
|
|
|
|
/// Requires features = [std]
|
|
|
|
|
pub fn eprint_help<'a, W: HelpWriter<'a, ID>>(&'a self, program_name: &'a str) {
|
|
|
|
|
let ctx = HelpWriterContext { options: self, program_name };
|
|
|
|
|
eprintln!("{}", W::new(ctx));
|
2025-10-28 18:19:51 +11:00
|
|
|
}
|
2025-11-01 21:59:05 +11:00
|
|
|
|
|
|
|
|
fn easy_args<'a>() -> (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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn easy_error(&self, program_name: &str, err: ParseError) {
|
|
|
|
|
eprintln!("{program_name}: {err}");
|
|
|
|
|
self.eprint_help::<StandardShortUsageWriter<'_, ID>>(program_name);
|
|
|
|
|
// TODO: only show when an option is marked help
|
|
|
|
|
eprintln!("Run '{program_name} --help' to view all available options.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The result of parsing commands using `jaarg::Opts::parse_map`.
|
|
|
|
|
pub enum ParseMapResult {
|
|
|
|
|
Map(BTreeMap<&'static str, String>),
|
|
|
|
|
Exit(std::process::ExitCode),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Opts<&'static str> {
|
|
|
|
|
/// Parse an iterator of strings as arguments and return the results in a BTreeMap.
|
|
|
|
|
///
|
|
|
|
|
/// Requires features = [std]
|
|
|
|
|
pub fn parse_map<'a, S: AsRef<str> + 'a, I: Iterator<Item = S>>(&self, program_name: &str, args: I,
|
|
|
|
|
error: impl FnOnce(&str, ParseError)) -> ParseMapResult {
|
|
|
|
|
let mut out: BTreeMap<&'static str, String> = BTreeMap::new();
|
2025-11-01 22:25:05 +11:00
|
|
|
match self.parse(&program_name, args, |_program_name, id, _opt, _name, arg| {
|
2025-11-01 21:59:05 +11:00
|
|
|
out.insert(id, arg.into());
|
|
|
|
|
Ok(ParseControl::Continue)
|
|
|
|
|
}, error) {
|
|
|
|
|
ParseResult::ContinueSuccess => ParseMapResult::Map(out),
|
|
|
|
|
ParseResult::ExitSuccess => ParseMapResult::Exit(std::process::ExitCode::SUCCESS),
|
|
|
|
|
ParseResult::ExitError => ParseMapResult::Exit(std::process::ExitCode::FAILURE),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parse arguments from the command line and return the results in a BTreeMap.
|
|
|
|
|
///
|
|
|
|
|
/// Requires features = [std]
|
|
|
|
|
pub fn parse_map_easy(&self) -> ParseMapResult {
|
|
|
|
|
let (program_name, argv) = Self::easy_args();
|
|
|
|
|
self.parse_map(&program_name, argv, |name, e| self.easy_error(name, e))
|
|
|
|
|
}
|
2025-10-28 18:19:51 +11:00
|
|
|
}
|