arkham/src/context.rs

42 lines
1.0 KiB
Rust

use crate::{
opt::{ActiveOpt, OptKind},
Command,
};
pub struct Context {
opts: Vec<ActiveOpt>,
cmd: Command,
}
impl Context {
pub(crate) fn new(cmd: Command, opts: Vec<ActiveOpt>) -> Self {
Self { opts, cmd }
}
/// Checks for the existance of a flag
pub fn flag(&self, name: &str) -> bool {
self.opts
.iter()
.any(|o| o.definition.name == name && matches!(o.definition.kind, OptKind::Flag))
}
/// Returns the value of an option if one exists
pub fn get_string(&self, name: &str) -> Option<String> {
self.opts
.iter()
.find_map(|o| {
if o.definition.name == name {
Some(o.raw_value.first().cloned())
} else {
None
}
})
.flatten()
}
/// Can be used to display the automatic help message for the current command.
pub fn display_help(&self) {
crate::command::print_command_help(&self.cmd, &vec![])
}
}