Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Positional Arguments into its own section in generated manpages #3580

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions clap_mangen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ impl<'a> Man<'a> {
self._render_synopsis_section(&mut roff);
self._render_description_section(&mut roff);

if app_has_arguments(&self.cmd) {
if app_has_positionals(&self.cmd) {
self._render_positionals_section(&mut roff);
}

if app_has_options(&self.cmd) {
self._render_options_section(&mut roff);
}

Expand Down Expand Up @@ -186,6 +190,18 @@ impl<'a> Man<'a> {
render::description(roff, &self.cmd);
}

/// Render the POSITIONAL ARGUMENTS section into the writer.
pub fn render_positionals_section(&self, w: &mut dyn Write) -> Result<(), std::io::Error> {
Comment on lines +193 to +194
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think more discussion is needed on #3579 before we merge this

let mut roff = Roff::default();
self._render_positionals_section(&mut roff);
roff.to_writer(w)
}

fn _render_positionals_section(&self, roff: &mut Roff) {
roff.control("SH", ["POSITIONAL ARGUMENTS"]);
render::positionals(roff, &self.cmd);
}

/// Render the OPTIONS section into the writer.
pub fn render_options_section(&self, w: &mut dyn Write) -> Result<(), std::io::Error> {
let mut roff = Roff::default();
Expand Down Expand Up @@ -257,9 +273,16 @@ fn app_has_version(cmd: &clap::Command) -> bool {
.is_some()
}

// Does the application have any command line arguments?
fn app_has_arguments(cmd: &clap::Command) -> bool {
cmd.get_arguments().any(|i| !i.is_hide_set())
// Does the application have any option arguments?
fn app_has_options(cmd: &clap::Command) -> bool {
cmd.get_arguments()
.any(|i| !i.is_hide_set() && !i.is_positional())
}

// Does the application have any positional arguments?
fn app_has_positionals(cmd: &clap::Command) -> bool {
cmd.get_arguments()
.any(|i| !i.is_hide_set() && i.is_positional())
}

// Does the application have any subcommands?
Expand Down
54 changes: 30 additions & 24 deletions clap_mangen/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
roff.text(line);
}

pub(crate) fn positionals(roff: &mut Roff, cmd: &clap::Command) {
let items: Vec<_> = cmd.get_arguments().filter(|i| !i.is_hide_set()).collect();

for pos in items.iter().filter(|a| a.is_positional()) {
let name = pos
.get_value_names()
.map_or(pos.get_id(), |value_names| value_names[0]);

let mut header = vec![italic(name)];

let mut body = vec![];

if let Some(defs) = option_default_values(pos) {
header.push(roman(&format!(" {}", defs)));
}

if let Some(help) = pos.get_long_help().or_else(|| pos.get_help()) {
body.push(roman(&help.to_string()));
}

if let Some(mut env) = option_environment(pos) {
body.append(&mut env);
}

roff.control("TP", []);
roff.text(header);
roff.text(body);
}
}

pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
let items: Vec<_> = cmd.get_arguments().filter(|i| !i.is_hide_set()).collect();

Expand Down Expand Up @@ -117,30 +147,6 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
roff.text(header);
roff.text(body);
}

for pos in items.iter().filter(|a| a.is_positional()) {
let (lhs, rhs) = option_markers(pos);
let name = format!("{}{}{}", lhs, pos.get_id(), rhs);

let mut header = vec![bold(&name)];

let mut body = vec![];

if let Some(defs) = option_default_values(pos) {
header.push(roman(&format!(" {}", defs)));
}

if let Some(help) = pos.get_long_help().or_else(|| pos.get_help()) {
body.push(roman(&help.to_string()));
}

if let Some(mut env) = option_environment(pos) {
body.append(&mut env);
}

roff.control("TP", []);
roff.text(body);
}
}

pub(crate) fn subcommands(roff: &mut Roff, cmd: &clap::Command, section: &str) {
Expand Down