Skip to content

Commit

Permalink
Add footer attribute
Browse files Browse the repository at this point in the history
Provides a way to add extra context to the bottom of the help message.
The doc comment appears at the top, and `#[footer = "..."]` is a nice
complement.
  • Loading branch information
parasyte committed Feb 11, 2024
1 parent 04ff0b2 commit fec8567
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions examples/full-derive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use std::{fmt::Write as _, path::PathBuf, process::ExitCode};
/// A basic argument parsing example with `onlyargs_derive`.
/// Sums a list of numbers and writes the result to a file or standard output.
#[derive(Clone, Debug, Eq, PartialEq, OnlyArgs)]
#[footer = "Please consider becoming a sponsor 💖:"]
#[footer = " - https://github.com/sponsors/parasyte"]
#[footer = " - https://ko-fi.com/blipjoy"]
#[footer = " - https://patreon.com/blipjoy"]
struct Args {
/// Your username.
username: String,
Expand Down
2 changes: 1 addition & 1 deletion onlyargs_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ license = "MIT"
proc-macro = true

[dependencies]
myn = "0.2"
myn = "0.2.1"
onlyargs = { version = "0.1", path = ".." }
19 changes: 17 additions & 2 deletions onlyargs_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
//! use onlyargs_derive::OnlyArgs;
//!
//! /// Doc comments will appear in your application's help text.
//! /// Multi-line comments are also supported.
//! ///
//! /// Features:
//! /// - Supports multi-line strings.
//! /// - Supports indentation.
//! #[derive(Debug, OnlyArgs)]
//! #[footer = "Footer attributes will be included at the bottom of the help message."]
//! #[footer = ""]
//! #[footer = "Features:"]
//! #[footer = " - Also supports multi-line strings."]
//! #[footer = " - Also supports indentation."]
//! struct Args {
//! /// Optional output path.
//! output: Option<std::path::PathBuf>,
Expand Down Expand Up @@ -111,7 +119,7 @@ mod parser;

/// See the [root module documentation](crate) for the DSL specification.
#[allow(clippy::too_many_lines)]
#[proc_macro_derive(OnlyArgs, attributes(default, long, short))]
#[proc_macro_derive(OnlyArgs, attributes(footer, default, long, short))]
pub fn derive_parser(input: TokenStream) -> TokenStream {
let ast = match ArgumentStruct::parse(input) {
Ok(ast) => ast,
Expand Down Expand Up @@ -312,6 +320,11 @@ pub fn derive_parser(input: TokenStream) -> TokenStream {
} else {
format!("\n{}\n", ast.doc.join("\n"))
};
let footer = if ast.footer.is_empty() {
String::new()
} else {
format!("\n{}\n", ast.footer.join("\n"))
};
let bin_name = std::env::var_os("CARGO_BIN_NAME").and_then(|name| name.into_string().ok());
let help_impl = if bin_name.is_none() {
r#"fn help() -> ! {
Expand Down Expand Up @@ -350,6 +363,8 @@ pub fn derive_parser(input: TokenStream) -> TokenStream {
{options_help:?},
{positional_help:?},
"\n",
{footer:?},
"\n",
);
/// The application name and version.
Expand Down
7 changes: 7 additions & 0 deletions onlyargs_derive/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) struct ArgumentStruct {
pub(crate) options: Vec<ArgOption>,
pub(crate) positional: Option<ArgOption>,
pub(crate) doc: Vec<String>,
pub(crate) footer: Vec<String>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -87,13 +88,19 @@ impl ArgumentStruct {
.map(trim_with_indent)
.collect();

let footer = get_attr_strings(&attrs, "footer")
.into_iter()
.map(|line| line.trim_end().to_string())
.collect();

match input.next() {
None => Ok(Self {
name,
flags,
options,
positional,
doc,
footer,
}),
tree => Err(spanned_error("Unexpected token", tree.as_span())),
}
Expand Down

0 comments on commit fec8567

Please sign in to comment.