-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// Copyright (c) 2023 Piotr Stolarz | ||
// GNU-like Command line options parser. | ||
// | ||
// Distributed under the 2-clause BSD License (the License) | ||
// see accompanying file LICENSE for details. | ||
// | ||
// This software is distributed WITHOUT ANY WARRANTY; without even the | ||
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// See the License for more information. | ||
// | ||
|
||
use cmdopts::{parse_opts, InfoCode, ProcessCode, ParseError, CmdOpt}; | ||
|
||
#[allow(unused_imports)] | ||
#[allow(unused_must_use)] | ||
pub fn main() | ||
{ | ||
use CmdOpt::*; | ||
use InfoCode::*; | ||
use ProcessCode::*; | ||
use ParseError::*; | ||
use OptId::*; | ||
use std::cell::Cell; | ||
|
||
#[derive(Clone)] | ||
#[derive(Copy)] | ||
enum OptId { OptA, OptB } | ||
impl Default for OptId { fn default() -> Self { OptA } } | ||
let opt_id: Cell<OptId> = Default::default(); | ||
|
||
let rc = parse_opts( | ||
// Options info provider | ||
// | ||
|opt| { | ||
match opt { | ||
Short(c) => { | ||
match c { | ||
// -a, option w/o value | ||
'a' => { opt_id.set(OptA); NoValueOpt }, | ||
// -b, option w/value | ||
'b' => { opt_id.set(OptB); ValueOpt }, | ||
_ => InfoCode::InvalidOpt, | ||
} | ||
}, | ||
Long(s) => { | ||
match s.as_str() { | ||
// long counterpart of the short options | ||
"long_a" => { opt_id.set(OptA); NoValueOpt }, | ||
"long_b" => { opt_id.set(OptB); ValueOpt }, | ||
_ => InfoCode::InvalidOpt, | ||
} | ||
}, | ||
} | ||
}, | ||
|
||
// Options handler | ||
// | ||
|opt, val| { | ||
// all parsed tokens must form options | ||
opt.as_ref().ok_or(GenericErr("Non-option detected".to_string()))?; | ||
|
||
match opt_id.get() { | ||
OptA => { | ||
println!("A option"); | ||
} | ||
OptB => { | ||
println!("B option with {}", val.as_ref().unwrap().val); | ||
}, | ||
}; | ||
|
||
Ok(ProcessCode::Continue) | ||
}); | ||
|
||
if rc.is_err() { | ||
eprintln!("[ERROR] {}", rc.unwrap_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters