Skip to content

Commit

Permalink
basic.rs example added
Browse files Browse the repository at this point in the history
  • Loading branch information
pstolarz committed May 21, 2023
1 parent 9dca1a7 commit 6c67faa
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
1 change: 0 additions & 1 deletion examples/advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ fn process_cmdopts(opts: &mut Options) -> Result<(), ParseError>

#[derive(Clone)]
#[derive(Copy)]
#[derive(Debug)]
enum OptId {
OptA,
OptB,
Expand Down
78 changes: 78 additions & 0 deletions examples/basic.rs
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());
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// See the License for more information.
//

//!
//! GNU-like command line options parser with long and short formats. Optional
//! argument (aka option's value) may be associated with an option. The parsing
//! routine `parse_opts()` accepts two user callbacks:
Expand Down Expand Up @@ -362,6 +361,7 @@ where
/// retrieve information about parsed option followed by call to `opt_h()` to
/// handle that option.
///
#[inline]
pub fn parse_opts<Fi, Fh>(opt_i: Fi, opt_h: Fh) -> Result<(), ParseError>
where
Fi: FnMut(&CmdOpt) -> InfoCode,
Expand Down

0 comments on commit 6c67faa

Please sign in to comment.