Skip to content

Allow users to generate man pages #280

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ xdg = "3.0.0"
xml-rs = "0.8.20"
xz2 = "0.1.7"
zstd = { version = "0.13.3", features = [ "zstdmt" ] }
clap_mangen = "0.2.28"

[dependencies.tokio]
features = ["full"]
Expand Down
7 changes: 7 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
lockFile = ./Cargo.lock;
};

nativeBuildInputs = [ installShellFiles ];
buildInputs = [ sqlite ];

postInstall = ''
Expand All @@ -42,6 +43,12 @@
substituteInPlace command-not-found.nu \
--subst-var out
install -Dm555 command-not-found.nu -t $out/etc/profile.d

"$out/bin/nix-locate" --mangen > nix-locate.1
"$out/bin/nix-index" --mangen > nix-index.1
"$out/bin/nix-channel-index" --mangen > nix-channel-index.1

installManPage nix-locate.1 nix-index.1 nix-channel-index.1
'';

meta = with lib; {
Expand Down
18 changes: 17 additions & 1 deletion src/bin/nix-channel-index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ async fn update_index(args: &Args) -> Result<()> {
}

#[derive(Debug, Parser)]
#[clap(author, about, version)]
#[clap(author, about, version, name = "nix-channel-index")]
struct Args {
/// Make REQUESTS http requests in parallel
#[clap(short = 'r', long = "requests", default_value = "500")]
Expand All @@ -200,12 +200,28 @@ struct Args {
/// Show a stack trace in the case of a Nix evaluation error
#[clap(long)]
show_trace: bool,

/// Generate man page, then exit.
#[clap(long, hide = true)]
mangen: bool,
}

#[tokio::main]
async fn main() {
let args = Args::parse();

if args.mangen {
use clap::CommandFactory;
let man = clap_mangen::Man::new(Args::command());

if let Err(err) = man.render(&mut std::io::stdout()) {
eprintln!("error: {}", err);
process::exit(2)
} else {
return;
}
}

if let Err(e) = update_index(&args).await {
eprintln!("error: {:?}", e);
process::exit(2);
Expand Down
18 changes: 17 additions & 1 deletion src/bin/nix-index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn cache_dir() -> &'static OsStr {

/// Builds an index for nix-locate
#[derive(Debug, Parser)]
#[clap(author, about, version)]
#[clap(author, about, version, name = "nix-index")]
struct Args {
/// Make REQUESTS http requests in parallel
#[clap(short = 'r', long = "requests", default_value = "100")]
Expand Down Expand Up @@ -168,12 +168,28 @@ struct Args {
/// Note: does not check if the cached data is up to date! Use only for development.
#[clap(long)]
path_cache: bool,

/// Generate man page, then exit
#[clap(long, hide = true)]
mangen: bool,
}

#[tokio::main]
async fn main() {
let args = Args::parse();

if args.mangen {
use clap::CommandFactory;
let man = clap_mangen::Man::new(Args::command());

if let Err(err) = man.render(&mut std::io::stdout()) {
eprintln!("error: {}", err);
process::exit(2)
} else {
return;
}
}

if let Err(e) = update_index(&args).await {
eprintln!("error: {:?}", e);
process::exit(2);
Expand Down
24 changes: 20 additions & 4 deletions src/bin/nix-locate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fn locate(args: &Args) -> Result<()> {
///
/// Handles parsing the values of more complex arguments.
fn process_args(matches: Opts) -> result::Result<Args, clap::Error> {
let pattern_arg = matches.pattern;
let pattern_arg = matches.pattern.unwrap();
let package_arg = matches.package;

let start_anchor = if matches.at_root { "^" } else { "" };
Expand Down Expand Up @@ -260,11 +260,11 @@ fn cache_dir() -> &'static OsStr {

/// Quickly finds the derivation providing a certain file
#[derive(Debug, Parser)]
#[clap(author, about, version, after_help = LONG_USAGE)]
#[clap(author, about, version, after_help = LONG_USAGE, name = "nix-locate")]
struct Opts {
/// Pattern for which to search
// #[clap(name = "PATTERN")]
pattern: String,
#[clap(required_unless_present = "mangen", name = "PATTERN")]
pattern: Option<String>,

/// Directory where the index is stored
#[clap(short, long = "db", default_value_os = cache_dir(), env = "NIX_INDEX_DATABASE")]
Expand Down Expand Up @@ -320,6 +320,10 @@ struct Opts {
/// store path are omitted. This is useful for scripts that use the output of nix-locate.
#[clap(long)]
minimal: bool,

/// Generate the man page, then exit
#[clap(long, hide = true)]
mangen: bool,
}

#[derive(clap::ValueEnum, Clone, Copy, Debug)]
Expand All @@ -345,6 +349,18 @@ impl FromStr for Color {
fn main() {
let args = Opts::parse();

if args.mangen {
use clap::CommandFactory;
let man = clap_mangen::Man::new(Opts::command());

if let Err(e) = man.render(&mut std::io::stdout()) {
eprintln!("error: {}", e);
process::exit(2)
} else {
return;
}
}

let args = process_args(args).unwrap_or_else(|e| e.exit());

if let Err(e) = locate(&args) {
Expand Down
Loading