Add example from README.md as basic.rs

This commit is contained in:
2025-11-02 22:55:00 +11:00
parent 274fbbf097
commit ae7c12ad62
2 changed files with 56 additions and 4 deletions

View File

@@ -22,12 +22,15 @@ let mut number = 0;
enum Arg { Help, Number, File, Out } enum Arg { Help, Number, File, Out }
const OPTIONS: Opts<Arg> = Opts::new(&[ const OPTIONS: Opts<Arg> = Opts::new(&[
Opt::help_flag(Arg::Help, &["-h", "--help"]).help_text("Show this help and exit."), Opt::help_flag(Arg::Help, &["-h", "--help"]).help_text("Show this help and exit."),
Opt::value(Arg::Number, &["-n", "--number"], "value").help_text("Optionally specify a number (default: 0)"), Opt::value(Arg::Number, &["-n", "--number"], "value")
Opt::positional(Arg::File, "file").required().help_text("Input file."), .help_text("Optionally specify a number (default: 0)"),
Opt::positional(Arg::Out, "out").help_text("Output destination (optional).") Opt::positional(Arg::File, "file").required()
.help_text("Input file."),
Opt::positional(Arg::Out, "out")
.help_text("Output destination (optional).")
]).with_description("My simple utility."); ]).with_description("My simple utility.");
// Parse the arguments // Parse command-line arguments from `std::env::args()`
match OPTIONS.parse_easy(|program_name, id, _opt, _name, arg| { match OPTIONS.parse_easy(|program_name, id, _opt, _name, arg| {
match id { match id {
Arg::Help => { Arg::Help => {
@@ -54,6 +57,7 @@ println!("{file:?} -> {out:?} (number: {number:?})",
main: main:
* Fixed incorrect error message format for coerced parsing errors. * Fixed incorrect error message format for coerced parsing errors.
* Added basic example.
v0.1.0: v0.1.0:
* Initial release. * Initial release.

48
examples/basic.rs Normal file
View File

@@ -0,0 +1,48 @@
/* basic - jaarg example program using parse_easy
* SPDX-FileCopyrightText: (C) 2025 Gay Pizza Specifications
* SPDX-License-Identifier: MIT
*/
use jaarg::{Opt, Opts, ParseControl, ParseResult};
use std::path::PathBuf;
fn main() {
// Variables for arguments to fill
let mut file = PathBuf::new();
let mut out: Option<PathBuf> = None;
let mut number = 0;
// Set up arguments table
enum Arg { Help, Number, File, Out }
const OPTIONS: Opts<Arg> = Opts::new(&[
Opt::help_flag(Arg::Help, &["-h", "--help"]).help_text("Show this help and exit."),
Opt::value(Arg::Number, &["-n", "--number"], "value")
.help_text("Optionally specify a number (default: 0)"),
Opt::positional(Arg::File, "file").required()
.help_text("Input file."),
Opt::positional(Arg::Out, "out")
.help_text("Output destination (optional).")
]).with_description("My simple utility.");
// Parse command-line arguments from `std::env::args()`
match OPTIONS.parse_easy(|program_name, id, _opt, _name, arg| {
match id {
Arg::Help => {
OPTIONS.print_full_help(program_name);
return Ok(ParseControl::Quit);
}
Arg::Number => { number = str::parse(arg)?; }
Arg::File => { file = arg.into(); }
Arg::Out => { out = Some(arg.into()); }
}
Ok(ParseControl::Continue)
}) {
ParseResult::ContinueSuccess => (),
ParseResult::ExitSuccess => std::process::exit(0),
ParseResult::ExitError => std::process::exit(1),
}
// Print the result variables
println!("{file:?} -> {out:?} (number: {number:?})",
out = out.unwrap_or(file.with_extension("out")));
}