mirror of
https://github.com/gay-pizza/jaarg.git
synced 2025-12-19 07:20:18 +00:00
50 lines
1.7 KiB
Rust
50 lines
1.7 KiB
Rust
/* basic - jaarg example program using parse_easy
|
|
* SPDX-FileCopyrightText: (C) 2025 Gay Pizza Specifications
|
|
* SPDX-License-Identifier: MIT OR Apache-2.0
|
|
*/
|
|
|
|
use jaarg::{Opt, OptHide, 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"]).hide_usage(OptHide::Short)
|
|
.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(|ctx| {
|
|
match ctx.id {
|
|
Arg::Help => {
|
|
OPTIONS.print_full_help(ctx.program_name);
|
|
return Ok(ParseControl::Quit);
|
|
}
|
|
Arg::Number => { number = str::parse(ctx.arg.unwrap().as_ref())?; }
|
|
Arg::File => { file = ctx.arg.unwrap().into(); }
|
|
Arg::Out => { out = Some(ctx.arg.unwrap().into()); }
|
|
}
|
|
Ok(ParseControl::Continue)
|
|
}) {
|
|
ParseResult::ContinueSuccess => (),
|
|
ParseResult::ExitSuccess => std::process::exit(0),
|
|
ParseResult::ExitFailure => std::process::exit(1),
|
|
}
|
|
|
|
// Print the result variables
|
|
println!("{file:?} -> {out:?} (number: {number:?})",
|
|
out = out.unwrap_or(file.with_extension("out")));
|
|
}
|