-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from vrmiguel/oof-argparsing
Change argparsing lib
- Loading branch information
Showing
20 changed files
with
1,134 additions
and
543 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,9 @@ | ||
[package] | ||
name = "oof" | ||
version = "0.1.0" | ||
authors = ["João M. Bezerra <marcospb19@hotmail.com>"] | ||
edition = "2018" | ||
description = "Ouch's argparsing library" | ||
repository = "https://github.com/vrmiguel/ouch" | ||
|
||
[dependencies] |
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,35 @@ | ||
use std::{error, ffi::OsString, fmt}; | ||
|
||
use crate::Flag; | ||
|
||
#[derive(Debug)] | ||
pub enum OofError { | ||
FlagValueConflict { | ||
flag: Flag, | ||
previous_value: OsString, | ||
new_value: OsString, | ||
}, | ||
} | ||
|
||
impl error::Error for OofError { | ||
fn source(&self) -> Option<&(dyn error::Error + 'static)> { | ||
None | ||
} | ||
} | ||
|
||
impl fmt::Display for OofError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
// TODO: implement proper debug messages | ||
match self { | ||
OofError::FlagValueConflict { | ||
flag, | ||
previous_value, | ||
new_value, | ||
} => write!( | ||
f, | ||
"CLI flag value conflicted for flag '--{}', previous: {:?}, new: {:?}.", | ||
flag.long, previous_value, new_value | ||
), | ||
} | ||
} | ||
} |
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,110 @@ | ||
use std::{ | ||
collections::{BTreeMap, BTreeSet}, | ||
ffi::{OsStr, OsString}, | ||
}; | ||
|
||
/// Shallow type, created to indicate a `Flag` that accepts a argument. | ||
/// | ||
/// ArgFlag::long(), is actually a Flag::long(), but sets a internal attribute. | ||
/// | ||
/// Examples in here pls | ||
#[derive(Debug)] | ||
pub struct ArgFlag; | ||
|
||
impl ArgFlag { | ||
pub fn long(name: &'static str) -> Flag { | ||
Flag { | ||
long: name, | ||
short: None, | ||
takes_value: true, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
pub struct Flag { | ||
// Also the name | ||
pub long: &'static str, | ||
pub short: Option<char>, | ||
pub takes_value: bool, | ||
} | ||
|
||
impl Flag { | ||
pub fn long(name: &'static str) -> Self { | ||
Self { | ||
long: name, | ||
short: None, | ||
takes_value: false, | ||
} | ||
} | ||
|
||
pub fn short(mut self, short_flag_char: char) -> Self { | ||
self.short = Some(short_flag_char); | ||
self | ||
} | ||
} | ||
|
||
#[derive(Default, PartialEq, Eq, Debug)] | ||
pub struct Flags { | ||
pub boolean_flags: BTreeSet<&'static str>, | ||
pub argument_flags: BTreeMap<&'static str, OsString>, | ||
} | ||
|
||
impl Flags { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
} | ||
|
||
impl Flags { | ||
pub fn is_present(&self, flag_name: &str) -> bool { | ||
self.boolean_flags.contains(flag_name) || self.argument_flags.contains_key(flag_name) | ||
} | ||
|
||
pub fn arg(&self, flag_name: &str) -> Option<&OsString> { | ||
self.argument_flags.get(flag_name) | ||
} | ||
|
||
pub fn take_arg(&mut self, flag_name: &str) -> Option<OsString> { | ||
self.argument_flags.remove(flag_name) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum FlagType { | ||
None, | ||
Short, | ||
Long, | ||
} | ||
|
||
impl FlagType { | ||
pub fn from(text: impl AsRef<OsStr>) -> Self { | ||
let text = text.as_ref(); | ||
|
||
let mut iter; | ||
|
||
#[cfg(target_family = "unix")] | ||
{ | ||
use std::os::unix::ffi::OsStrExt; | ||
iter = text.as_bytes().iter(); | ||
} | ||
#[cfg(target_family = "windows")] | ||
{ | ||
use std::os::windows::ffi::OsStrExt; | ||
iter = text.encode_wide | ||
} | ||
|
||
// 45 is the code for a hyphen | ||
// Typed as 45_u16 for Windows | ||
// Typed as 45_u8 for Unix | ||
if let Some(45) = iter.next() { | ||
if let Some(45) = iter.next() { | ||
Self::Long | ||
} else { | ||
Self::Short | ||
} | ||
} else { | ||
Self::None | ||
} | ||
} | ||
} |
Oops, something went wrong.