Struct aopt_help::prelude::DefaultPolicy
source · pub struct DefaultPolicy<'a, I> { /* private fields */ }
Implementations§
source§impl<'a, I> DefaultPolicy<'a, I>
impl<'a, I> DefaultPolicy<'a, I>
sourcepub fn new<S: Into<Cow<'a, str>>>(
name: S,
style: Style,
block: Vec<Style>,
hiding_pos: bool
) -> Self
pub fn new<S: Into<Cow<'a, str>>>(
name: S,
style: Style,
block: Vec<Style>,
hiding_pos: bool
) -> Self
Examples found in repository?
src/lib.rs (line 291)
283 284 285 286 287 288 289 290 291 292 293 294 295 296
pub fn display_cmd<S>(&mut self, cmd: S) -> Result<()>
where
S: Into<Cow<'a, str>>,
{
let name = cmd.into();
let cmd = self.cmds.iter().find(|v| v.name() == name).ok_or_else(|| {
Error::raise(format!("Can not format help of {name} with DefaultPolicy"))
})?;
let policy = DefaultPolicy::new(self.name(), self.style.clone(), vec![], true);
let help = policy.format(cmd).ok_or_else(|| todo!())?;
writeln!(&mut self.writer, "{}", help)
.map_err(|e| Error::raise(format!("Can not write to handler: {:?}", e)))
}
source§impl<'a> DefaultPolicy<'a, Command<'a>>
impl<'a> DefaultPolicy<'a, Command<'a>>
sourcepub fn get_block_usage(
&self,
item: &Block<'a, Cow<'a, str>>,
stores: &[Store<'a>]
) -> (Vec<String>, Vec<String>)
pub fn get_block_usage(
&self,
item: &Block<'a, Cow<'a, str>>,
stores: &[Store<'a>]
) -> (Vec<String>, Vec<String>)
Examples found in repository?
src/format/policy.rs (line 91)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
pub fn get_command_usage(&self, item: &Command<'a>) -> Cow<'a, str> {
let mut usages = vec![];
let mut args = vec![];
let mut block_hint = vec![];
for block in item.block() {
let (mut block_usages, mut block_args) = self.get_block_usage(block, item);
if !block_usages.is_empty() {
usages.append(&mut block_usages);
}
// if not omit args, using the args, otherwise using hint of block
if !block_args.is_empty() {
args.append(&mut block_args);
}
}
for block in item.block() {
let arg = block.hint();
if !arg.is_empty() {
block_hint.push(arg);
}
}
let usage = usages.join(" ");
let args = if self.hiding_pos {
"".to_owned()
} else {
args.join(" ")
};
let block_hint = block_hint.join(" ");
let ret = if self.hiding_pos {
format!(
"Usage: {} {} {} {}",
self.name,
item.name(),
usage,
block_hint
)
} else {
format!("Usage: {} {} {} {}", self.name, item.name(), usage, args)
};
ret.into()
}
sourcepub fn get_command_usage(&self, item: &Command<'a>) -> Cow<'a, str>
pub fn get_command_usage(&self, item: &Command<'a>) -> Cow<'a, str>
Examples found in repository?
src/format/policy.rs (line 215)
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
fn format(&self, item: &Command<'a>) -> Option<Cow<'a, str>> {
let usage = self.get_command_usage(item);
let mut blocks = vec![usage];
let head = item.head();
let foot = item.foot();
let block_spacing = "\n".repeat(1 + self.style.block_spacing);
if !head.is_empty() {
blocks.push(head);
}
for block in item.block() {
let help = self.get_block_help(block, item);
if !help.is_empty() {
blocks.push(help);
}
}
if !foot.is_empty() {
blocks.push(foot);
}
Some(blocks.join(&block_spacing).into())
}
sourcepub fn get_block_help(
&self,
item: &Block<'a, Cow<'a, str>>,
stores: &Vec<Store<'a>>
) -> Cow<'a, str>
pub fn get_block_help(
&self,
item: &Block<'a, Cow<'a, str>>,
stores: &Vec<Store<'a>>
) -> Cow<'a, str>
Examples found in repository?
src/format/policy.rs (line 225)
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
fn format(&self, item: &Command<'a>) -> Option<Cow<'a, str>> {
let usage = self.get_command_usage(item);
let mut blocks = vec![usage];
let head = item.head();
let foot = item.foot();
let block_spacing = "\n".repeat(1 + self.style.block_spacing);
if !head.is_empty() {
blocks.push(head);
}
for block in item.block() {
let help = self.get_block_help(block, item);
if !help.is_empty() {
blocks.push(help);
}
}
if !foot.is_empty() {
blocks.push(foot);
}
Some(blocks.join(&block_spacing).into())
}