Move special treatment of help flag into lib to improve usage display and BTreeMap use case ergonomics

This commit is contained in:
2025-11-02 00:44:25 +11:00
parent bf10fbc0a4
commit 0d9b86c767
6 changed files with 68 additions and 31 deletions

View File

@@ -203,7 +203,7 @@ pub fn main() -> ExitCode {
// Read & parse arguments from the command line, store results into the above structure
enum Arg { Out, Bin, Txt, Whitespace, Help }
const OPTIONS: Opts<Arg> = Opts::new(&[
Opt::flag(Arg::Help, &["--help", "-h"], "Show this help message and exit"),
Opt::help_flag(Arg::Help, &["--help", "-h"], "Show this help message and exit"),
Opt::positional_required(Arg::Out, "out", "Path to generated header file"),
Opt::value(Arg::Bin, &["--bin", "-b"], "data.bin", "Add a binary file"),
Opt::value(Arg::Txt, &["--txt", "-t"], "text.txt", "Add a text file"),

View File

@@ -8,18 +8,13 @@ use std::process::ExitCode;
fn main() -> ExitCode {
const OPTIONS: Opts<&'static str> = Opts::new(&[
Opt::flag("help", &["--help"], "Show this help"),
Opt::help_flag("help", &["--help"], "Show this help"),
Opt::positional("positional", "positional", "Positional argument"),
Opt::value("value", &["-v", "--value"], "path", "Value option"),
Opt::value("value", &["-v", "--value"], "string", "Value option"),
Opt::flag("flag", &["-f", "--flag"], "Flag option"),
]);
let map = match OPTIONS.parse_map_easy() {
// TODO: There should probably be a more efficient way to make jaarg handle help for us
ParseMapResult::Map(map) if map.contains_key("help") => {
OPTIONS.print_full_help("btreemap");
return ExitCode::SUCCESS;
}
ParseMapResult::Map(map) => map,
ParseMapResult::Exit(code) => { return code; }
};