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

Add man page generation #11

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ license = "MIT"
keywords = ["clap", "cli", "completion"]
categories = ["command-line-interface"]

[features]
man-page = ["dep:clap_mangen"]

[dependencies]
clap = "4"
clap_complete = "4"
clap_complete_fig = "4"

clap_mangen = { version = "0.2", optional = true }

[dev-dependencies]
clap = { version = "4", features = ["derive"] }
60 changes: 25 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ pub enum Shell {
Fig,
/// Friendly Interactive SHell (fish)
Fish,
/// Man page
#[cfg(feature = "man-page")]
ManPage,
/// PowerShell
PowerShell,
/// Z SHell (zsh)
Expand All @@ -207,6 +210,9 @@ impl clap_complete::Generator for Shell {
Self::Zsh => clap_complete::Shell::Zsh.file_name(name),

Self::Fig => clap_complete_fig::Fig.file_name(name),

#[cfg(feature = "man-page")]
Self::ManPage => format!("{}.1", name),
}
}

Expand All @@ -219,6 +225,9 @@ impl clap_complete::Generator for Shell {
Self::Zsh => clap_complete::Shell::Zsh.generate(cmd, buf),

Self::Fig => clap_complete_fig::Fig.generate(cmd, buf),

#[cfg(feature = "man-page")]
Self::ManPage => clap_mangen::Man::new(cmd.clone()).render(buf).unwrap(),
}
}
}
Expand Down Expand Up @@ -264,6 +273,8 @@ impl ValueEnum for Shell {
Self::Elvish,
Self::Fig,
Self::Fish,
#[cfg(feature = "man-page")]
Self::ManPage,
Self::PowerShell,
Self::Zsh,
]
Expand All @@ -275,6 +286,8 @@ impl ValueEnum for Shell {
Self::Elvish => clap::builder::PossibleValue::new("elvish"),
Self::Fig => clap::builder::PossibleValue::new("fig"),
Self::Fish => clap::builder::PossibleValue::new("fish"),
#[cfg(feature = "man-page")]
Self::ManPage => clap::builder::PossibleValue::new("man"),
Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm torn on what this should be, for example:

Suggested change
Self::ManPage => clap::builder::PossibleValue::new("man"),
Self::ManPage => clap::builder::PossibleValue::new("man-page"),

Self::PowerShell => clap::builder::PossibleValue::new("powershell"),
Self::Zsh => clap::builder::PossibleValue::new("zsh"),
})
Expand All @@ -286,37 +299,6 @@ mod tests {
use super::*;
use clap::ValueEnum;

#[test]
fn check_casing_bash() {
assert_eq!(Shell::Bash.to_possible_value().unwrap().get_name(), "bash");
}
#[test]
fn check_casing_elvish() {
assert_eq!(
Shell::Elvish.to_possible_value().unwrap().get_name(),
"elvish",
);
}
#[test]
fn check_casing_fig() {
assert_eq!(Shell::Fig.to_possible_value().unwrap().get_name(), "fig");
}
#[test]
fn check_casing_fish() {
assert_eq!(Shell::Fish.to_possible_value().unwrap().get_name(), "fish");
}
#[test]
fn check_casing_powershell() {
assert_eq!(
Shell::PowerShell.to_possible_value().unwrap().get_name(),
"powershell",
);
}
#[test]
fn check_casing_zsh() {
assert_eq!(Shell::Zsh.to_possible_value().unwrap().get_name(), "zsh");
}

#[test]
fn check_order() {
let names = Shell::value_variants()
Expand All @@ -328,9 +310,17 @@ mod tests {
sorted.sort_unstable();

assert_eq!(names, sorted);
assert_eq!(
names,
vec!["bash", "elvish", "fig", "fish", "powershell", "zsh"],
);

if cfg!(feature = "man-page") {
assert_eq!(
names,
vec!["bash", "elvish", "fig", "fish", "man", "powershell", "zsh"],
);
} else {
assert_eq!(
names,
vec!["bash", "elvish", "fig", "fish", "powershell", "zsh"],
);
}
Comment on lines +314 to +324
Copy link
Owner Author

Choose a reason for hiding this comment

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

We should make this scale better for when we add more features in the future.

}
}