Skip to content

Document full use-case (e.g., build.rs script) #33

@codesections

Description

@codesections

In discussing #31, @yoshuawuyts mentioned that we might be calling man differently. That made me realize that we don't have any documentation about the typical way to call man (e.g., at run time, in a build.rs script, or using an external tool. I propose adding something to the README that outlines calling man from a build.rs file, saving the generated file to the OUT_DIR and then (separately) installing the generated man page with make. I'm suggesting something like this be added to the README:

Example

First, generate a man page and save it to disk as part of your compilation using a build.rs file.

// build.rs
use man::prelude::*;
use std::env;
use std::fs::File;
use std::io::prelude::*;

include!("src/cli.rs");

fn main() {
    let mut out_dir = env::var("OUT_DIR").unwrap();
    let page = Manual::new("basic")
        .about("A basic example")
        .author(Author::new("Alice Person").email("[email protected]"))
        .author(Author::new("Bob Human").email("[email protected]"))
        .flag(
            Flag::new()
                .short("-d")
                .long("--debug")
                .help("Enable debug mode"),
        )
        .flag(
            Flag::new()
                .short("-v")
                .long("--verbose")
                .help("Enable verbose mode"),
        )
        .option(
            Opt::new("output")
                .short("-o")
                .long("--output")
                .help("The file path to write output to"),
        )
        .example(
            Example::new()
                .text("run basic in debug mode")
                .command("basic -d")
                .output("Debug Mode: basic will print errors to the console")
            )
        .custom(
            Section::new("usage note")
                .paragraph("This program will overwrite any file currently stored at the output path")
        )
        .render();
    out_dir.push_str("/MY_APP.1");
    let mut file = File::create(out_dir).expect("Should be able to open file in project directory");
    file.write_all(page.as_bytes()).expect("Should be able to write to file in project directory");
}

And then add a makefile to install the generated man page

# makefile
OUT_DIR := $(shell find -type d -name out | grep MY_APP)

.PHONY : install
install : 
	cp $(OUT_DIR)/MY_APP.1 /usr/local/share/man/man1/MY_APP.1
	# any other install commands for MY_APP (the bin, shell completions, etc.)

.PHONY : uninstall
uninstall :
	rm -f /usr/local/share/man/man1/MY_APP.1
	# any other uninstall commands for MY_APP

end example

Two questions on this:

  1. Is build.rs + makefile the procedure we want to recommend?
  2. If so, should an example like this go under the current example in the README (which prints to stdout) or replace that example?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions