Skip to content

Commit

Permalink
add dd_style arguments to man and md generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Mar 22, 2024
1 parent c9c2c75 commit c5651a9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions complete/src/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ mod test {
value: Value::No,
}],
long: vec![],
dd_style: vec![],
help: "some flag",
value: Some(hint),
}],
Expand Down
1 change: 1 addition & 0 deletions complete/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Command<'a> {
pub struct Arg<'a> {
pub short: Vec<Flag<'a>>,
pub long: Vec<Flag<'a>>,
pub dd_style: Vec<(&'a str, &'a str)>,
pub help: &'a str,
pub value: Option<ValueHint>,
}
Expand Down
8 changes: 8 additions & 0 deletions complete/src/man.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ pub fn render(c: &Command) -> String {
Value::No => {}
}
}
for (flag, value) in &arg.dd_style {
if !flags.is_empty() {
flags.push(roman(", "));
}
flags.push(bold(*flag));
flags.push(roman("="));
flags.push(italic(*value));
}
page.text(flags);
page.text([roman(arg.help)]);
}
Expand Down
4 changes: 4 additions & 0 deletions complete/src/md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ fn options(c: &Command) -> String {
flags.push(format!("<code>-{flag}{value_str}</code>"));
}

for (flag, value) in &arg.dd_style {
flags.push(format!("<code>{flag}={value}</code>"));
}

out.push_str(&flags.join(", "));
out.push_str("</dt>\n");
out.push_str(&format!("<dd>\n\n{}\n\n</dd>\n", arg.help));
Expand Down
19 changes: 15 additions & 4 deletions derive/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ pub fn complete(args: &[Argument], file: &Option<String>) -> TokenStream {
continue;
};

let Flags { short, long, .. } = flags;
if short.is_empty() && long.is_empty() {
let Flags {
short,
long,
dd_style,
} = flags;
if short.is_empty() && long.is_empty() && dd_style.is_empty() {
continue;
}

// If none of the flags take an argument, we won't need ValueHint
// based on that type. So we should not attempt to call `value_hint`
// on it.
let any_flag_takes_argument =
short.iter().any(|f| f.value != Value::No) && long.iter().any(|f| f.value != Value::No);
let any_flag_takes_argument = !dd_style.is_empty()
&& short.iter().any(|f| f.value != Value::No)
&& long.iter().any(|f| f.value != Value::No);

let short: Vec<_> = short
.iter()
Expand Down Expand Up @@ -75,6 +80,11 @@ pub fn complete(args: &[Argument], file: &Option<String>) -> TokenStream {
})
.collect();

let dd_style: Vec<_> = dd_style
.iter()
.map(|(flag, value)| quote!((#flag, #value)))
.collect();

let hint = match (field, any_flag_takes_argument) {
(Some(ty), true) => quote!(Some(<#ty>::value_hint())),
_ => quote!(None),
Expand All @@ -84,6 +94,7 @@ pub fn complete(args: &[Argument], file: &Option<String>) -> TokenStream {
::uutils_args_complete::Arg {
short: vec![#(#short),*],
long: vec![#(#long),*],
dd_style: vec![#(#dd_style),*],
help: #help,
value: #hint,
}
Expand Down
4 changes: 4 additions & 0 deletions examples/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ enum Arg {
/// Give it a path!
#[arg("-p P", "--path=P")]
Path(PathBuf),

/// A dd_style argument!
#[arg("if=file")]
File(PathBuf),
}

struct Settings;
Expand Down

0 comments on commit c5651a9

Please sign in to comment.