mirror of
https://github.com/gay-pizza/jaarg.git
synced 2025-12-19 07:20:18 +00:00
Create no_std examples
This commit is contained in:
46
jaarg-nostd/examples/btreemap_nostd.rs
Normal file
46
jaarg-nostd/examples/btreemap_nostd.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
/* btreemap_nostd - jaarg example program using BTreeMap in `no_std`
|
||||
* SPDX-FileCopyrightText: (C) 2025 Gay Pizza Specifications
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use jaarg::{
|
||||
alloc::ParseMapResult, ErrorUsageWriter, ErrorUsageWriterContext, HelpWriter, HelpWriterContext,
|
||||
Opt, Opts, StandardErrorUsageWriter, StandardFullHelpWriter
|
||||
};
|
||||
use jaarg_nostd::{eprint, print, println, harness::ExitCode, simplepathbuf::SimplePathBuf};
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(improper_ctypes_definitions)]
|
||||
extern "C" fn safe_main(args: &[&str]) -> ExitCode {
|
||||
const OPTIONS: Opts<&'static str> = Opts::new(&[
|
||||
Opt::help_flag("help", &["--help"]).help_text("Show this help"),
|
||||
Opt::positional("positional", "positional").help_text("Positional argument"),
|
||||
Opt::value("value", &["-v", "--value"], "string").help_text("Value option"),
|
||||
Opt::flag("flag", &["-f", "--flag"]).help_text("Flag option"),
|
||||
]);
|
||||
|
||||
let map = match OPTIONS.parse_map(
|
||||
SimplePathBuf::from(*args.first().unwrap()).basename(),
|
||||
args.iter().skip(1),
|
||||
|name| {
|
||||
let ctx = HelpWriterContext { options: &OPTIONS, program_name: name };
|
||||
print!("{}", StandardFullHelpWriter::new(ctx));
|
||||
},
|
||||
|program_name, err| {
|
||||
let ctx = ErrorUsageWriterContext { options: &OPTIONS, program_name, error: err };
|
||||
eprint!("{}", StandardErrorUsageWriter::new(ctx));
|
||||
}
|
||||
) {
|
||||
ParseMapResult::Map(map) => map,
|
||||
ParseMapResult::ExitSuccess => { return ExitCode::SUCCESS; }
|
||||
ParseMapResult::ExitFailure => { return ExitCode::FAILURE; }
|
||||
};
|
||||
|
||||
println!("{:?}", map);
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Reference in New Issue
Block a user