Skip to main content

getargs_derive_macros/
lib.rs

1#![deny(missing_docs)]
2//! Internal proc-macro implementation for `getargs-derive`.
3//!
4//! This crate is not meant to be used directly. Re-exported via `getargs_derive::GetArgs`.
5
6mod expand;
7mod help;
8mod model;
9mod subcommand;
10mod value_enum;
11
12use proc_macro::TokenStream;
13use syn::{DeriveInput, parse_macro_input};
14
15/// Derive macro for the [`getargs`](https://crates.io/crates/getargs) crate.
16///
17/// Generates a `parse` method on your struct that consumes CLI arguments via
18/// `getargs::Options` and populates the struct's fields, along with the
19/// `HELP` and `LONG_HELP` constants holding the generated short and long help texts.
20///
21/// # Attribute syntax
22///
23/// See the [crate-level documentation](https://docs.rs/getargs-derive/latest/getargs_derive/)
24/// for a full reference of supported `#[arg(...)]` and `#[command(...)]` attributes.
25#[proc_macro_derive(GetArgs, attributes(arg, command))]
26pub fn derive_get_args(input: TokenStream) -> TokenStream {
27    let input = parse_macro_input!(input as DeriveInput);
28    match model::parse(&input, model::Keys::Struct) {
29        Ok(model) => expand::expand(&input, &model).into(),
30        Err(error) => error.to_compile_error().into(),
31    }
32}
33
34/// Derive macro for an enum of subcommands, used by a `#[command(subcommand)]` field of a struct
35/// deriving `GetArgs`.
36///
37/// Each variant is a subcommand named after the kebab-cased variant name, which
38/// `#[command(name = "...")]` overrides. Variants can be unit variants, variants with named fields
39/// (declared with `#[arg(...)]` as in a `GetArgs` struct) or newtype variants wrapping a struct
40/// deriving `GetArgs`.
41///
42/// See the
43/// [subcommands guide](https://docs.rs/getargs-derive/latest/getargs_derive/guide/subcommands/index.html)
44/// for details.
45#[proc_macro_derive(Subcommand, attributes(arg, command))]
46pub fn derive_subcommand(input: TokenStream) -> TokenStream {
47    let input = parse_macro_input!(input as DeriveInput);
48    match subcommand::parse(&input) {
49        Ok(variants) => subcommand::expand(&input, &variants).into(),
50        Err(error) => error.to_compile_error().into(),
51    }
52}
53
54/// Derive macro implementing `getargs_derive::ValueEnum` and `FromStr` for an enum with unit
55/// variants.
56///
57/// Each variant is accepted under its kebab-cased name, which `#[value(name = "...")]` overrides,
58/// and under every `#[value(alias = "...")]`.
59///
60/// See the
61/// [value enum guide](https://docs.rs/getargs-derive/latest/getargs_derive/guide/value_enum/index.html)
62/// for details.
63#[proc_macro_derive(ValueEnum, attributes(value))]
64pub fn derive_value_enum(input: TokenStream) -> TokenStream {
65    let input = parse_macro_input!(input as DeriveInput);
66    match value_enum::parse(&input) {
67        Ok(variants) => value_enum::expand(&input, &variants).into(),
68        Err(error) => error.to_compile_error().into(),
69    }
70}