Skip to content

Commit

Permalink
feat: add takes field for Flag
Browse files Browse the repository at this point in the history
This indicates if the flag takes an argument
  • Loading branch information
RoloEdits committed Dec 18, 2024
1 parent f26ca56 commit 910d370
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions helix-term/src/commands/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,61 @@ pub struct Flag {
pub long: &'static str,
pub short: Option<&'static str>,
pub desc: &'static str,
pub takes: bool,
}

#[macro_export]
macro_rules! flag {
// Case: All fields provided
(
long: $long:expr,
short: $short:expr,
desc: $desc:expr
desc: $desc:expr,
takes: $takes:expr $(,)?
) => {
$crate::commands::flag::Flag {
long: $long,
short: Some($short),
desc: $desc,
takes: $takes,
}
};
// Case: All fields except takes
(
long: $long:expr,
desc: $desc:expr
short: $short:expr,
desc: $desc:expr $(,)?
) => {
$crate::commands::flag::Flag {
long: $long,
short: Some($short),
desc: $desc,
takes: false,
}
};
// Case: Only long, desc, and takes
(
long: $long:expr,
desc: $desc:expr,
takes: $takes:expr $(,)?
) => {
$crate::commands::flag::Flag {
long: $long,
short: None,
desc: $desc,
takes: $takes,
}
};
// Case: Only long and desc
(
long: $long:expr,
desc: $desc:expr $(,)?
) => {
$crate::commands::flag::Flag {
long: $long,
short: None,
desc: $desc,
takes: false,
}
};
}
Expand All @@ -37,12 +69,14 @@ mod tests {
let full = flag! {
long: "--all",
short: "-a",
desc: "clears all registers"
desc: "clears all registers",
takes: true,
};

assert_eq!("--all", full.long);
assert_eq!(Some("-a"), full.short);
assert_eq!("clears all registers", full.desc);
assert!(full.takes);

let partial = flag! {
long: "--all",
Expand All @@ -52,5 +86,6 @@ mod tests {
assert_eq!("--all", partial.long);
assert_eq!(None, partial.short);
assert_eq!("clears all registers", partial.desc);
assert!(!partial.takes);
}
}

0 comments on commit 910d370

Please sign in to comment.