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 5b8e978
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 19 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
15 changes: 12 additions & 3 deletions examples/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use uutils_args::{Arguments, Options, Value};

#[derive(Value)]
#[derive(Value, Debug)]
enum Number {
#[value]
One,
Expand All @@ -27,13 +27,22 @@ enum Arg {
/// Give it a path!
#[arg("-p P", "--path=P")]
Path(PathBuf),

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

struct Settings;

impl Options<Arg> for Settings {
fn apply(&mut self, _arg: Arg) {
panic!("Compile with the 'parse-is-complete' feature!")
fn apply(&mut self, arg: Arg) {
match arg {
Arg::Flag => println!("Got flag"),
Arg::Number(n) => println!("Got number {n:?}"),
Arg::Path(p) => println!("Got path {}", p.display()),
Arg::File(f) => println!("Got file {}", f.display()),
}
}
}

Expand Down
33 changes: 21 additions & 12 deletions tests/coreutils/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ struct Settings {
outfile: Option<PathBuf>,
ibs: usize,
obs: usize,
cbs: usize,
skip: u64,
seek: u64,
count: usize,
_iconv: Vec<String>,
_iflags: Vec<String>,
_oconv: Vec<String>,
_oflags: Vec<String>,
iconv: Vec<String>,
iflags: Vec<String>,
oconv: Vec<String>,
oflags: Vec<String>,
status: Option<StatusLevel>,
}

Expand All @@ -77,15 +78,16 @@ impl Default for Settings {
Self {
ibs: 512,
obs: 512,
cbs: 512,
infile: Default::default(),
outfile: Default::default(),
skip: Default::default(),
seek: Default::default(),
count: Default::default(),
_iconv: Default::default(),
_iflags: Default::default(),
_oconv: Default::default(),
_oflags: Default::default(),
iconv: Default::default(),
iflags: Default::default(),
oconv: Default::default(),
oflags: Default::default(),
status: Default::default(),
}
}
Expand All @@ -102,14 +104,21 @@ impl Options<Arg> for Settings {
self.ibs = b;
self.obs = b;
}
Arg::Cbs(_) => todo!(),
Arg::Cbs(b) => self.cbs = b,
Arg::Skip(b) => self.skip = b,
Arg::Seek(b) => self.seek = b,
Arg::Count(n) => self.count = n,
Arg::Status(level) => self.status = Some(level),
Arg::Conv(_) => todo!(),
Arg::Iflag(_) => todo!(),
Arg::Oflag(_) => todo!(),
Arg::Conv(c) => {
self.iconv.push(c.clone());
self.oconv.push(c);
}
Arg::Iflag(f) => {
self.iflags.push(f);
}
Arg::Oflag(f) => {
self.oflags.push(f);
}
}
}
}
Expand Down

0 comments on commit 5b8e978

Please sign in to comment.