Skip to content

Commit

Permalink
Generate the man page via clap_mangen
Browse files Browse the repository at this point in the history
  • Loading branch information
giodamelio committed Aug 8, 2023
1 parent a7b9f90 commit b8e0be2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ strip-ansi-escapes = "0.1.1"
anyhow = "1.0.72"

[build-dependencies]
clap = {version = "4.3.19", features = ["default", "cargo"]}
clap_mangen = "0.2.12"

[features]
29 changes: 29 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::env::var_os;
use std::fs::write;
use std::io::{ErrorKind, Result};
use std::path::PathBuf;

// This is a bit of a hack to not have to rewrite the cli in the build
#[path = "src/cli.rs"]
mod cli;

fn main() -> Result<()> {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/cli.rs");

let out_dir = PathBuf::from(var_os("OUT_DIR").ok_or(ErrorKind::NotFound)?);
println!(
"cargo:warning=manpages built at {:?}",
out_dir.join("little_boxes.1")
);

let cmd = cli::cli();

let man = clap_mangen::Man::new(cmd);
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer)?;

write(out_dir.join("little_boxes.1"), buffer)?;

Ok(())
}
3 changes: 0 additions & 3 deletions build_manpage.sh

This file was deleted.

46 changes: 26 additions & 20 deletions little_boxes.1
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.48.5.
.TH LITTLE_BOXES "1" "November 2021" "little_boxes 1.7.0" "User Commands"
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH little_boxes 1 "little_boxes 1.7.0"
.SH NAME
little_boxes \- Adds boxes around stdin. Optionally adds a title.
little_boxes \- Adds boxes around stdin. Optionally adds a title
.SH SYNOPSIS
\fBlittle_boxes\fR [\fB\-t\fR|\fB\-\-title\fR] [\fB\-c\fR|\fB\-\-charset\fR] [\fB\-f\fR|\fB\-\-file\fR] [\fB\-\-all\fR] [\fB\-h\fR|\fB\-\-help\fR] [\fB\-V\fR|\fB\-\-version\fR]
.SH DESCRIPTION
little_boxes 1.7.0
Gio d'Amelio <[email protected]>
Adds boxes around stdin. Optionally adds a title
.SS "USAGE:"
.IP
little_boxes [FLAGS] [OPTIONS]
.SS "FLAGS:"
.SH OPTIONS
.TP
\fB\-t\fR, \fB\-\-title\fR=\fITITLE\fR
Add a title to the box
.TP
\fB\-c\fR, \fB\-\-charset\fR=\fICHARSET\fR [default: thick]
The charset to draw the box with
.br

.br
[\fIpossible values: \fRthick, thin, double, box, rounded, dot]
.TP
\fB\-f\fR, \fB\-\-file\fR=\fIFILE\fR
Read input from a file instead of stdin
.TP
\fB\-\-all\fR
Compare all charsets
.TP
\fB\-h\fR, \fB\-\-help\fR
Prints help information
Print help
.TP
\fB\-V\fR, \fB\-\-version\fR
Prints version information
.SS "OPTIONS:"
.TP
\fB\-c\fR, \fB\-\-charset\fR <charset>
The charset to draw the box with [default: thick]
[possible values: thick, thin, double, box,
rounded, dot]
.TP
\fB\-t\fR, \fB\-\-title\fR <title>
Add a title to the box
Print version
.SH VERSION
v1.7.0
.SH AUTHORS
Gio d\*(AqAmelio <[email protected]>
18 changes: 18 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::path::PathBuf;

use clap::{arg, command, value_parser, Command};

pub fn cli() -> Command {
command!()
.arg(arg!(-t --title <TITLE> "Add a title to the box").required(false))
.arg(
arg!(-c --charset <CHARSET> "The charset to draw the box with")
.value_parser(["thick", "thin", "double", "box", "rounded", "dot"])
.default_value("thick"),
)
.arg(
arg!(-f --file <FILE> "Read input from a file instead of stdin")
.value_parser(value_parser!(PathBuf)),
)
.arg(arg!(--all "Compare all charsets"))
}
18 changes: 4 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use std::path::PathBuf;
use std::process;

use anyhow::{Context, Result};
use clap::{arg, command, value_parser, ArgMatches};
use clap::ArgMatches;

mod cli;
use self::cli::cli;
mod draw_box;
use self::draw_box::{DrawBox, SimpleBox, TitleBox};
mod charset;
Expand All @@ -30,19 +32,7 @@ fn get_input(matches: &ArgMatches) -> Result<Vec<String>> {
}

fn run() -> Result<()> {
let matches = command!()
.arg(arg!(-t --title <TITLE> "Add a title to the box").required(false))
.arg(
arg!(-c --charset <CHARSET> "The charset to draw the box with")
.value_parser(["thick", "thin", "double", "box", "rounded", "dot"])
.default_value("thick"),
)
.arg(
arg!(-f --file <FILE> "Read input from a file instead of stdin")
.value_parser(value_parser!(PathBuf)),
)
.arg(arg!(--all "Compare all charsets"))
.get_matches();
let matches = cli().get_matches();

let input = get_input(&matches)?;

Expand Down

0 comments on commit b8e0be2

Please sign in to comment.