1 // Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
2 // Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
3 // Ana Hobden (@hoverbear) <operator@hoverbear.org>
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 //
11 // This work was derived from Structopt (https://github.com/TeXitoi/structopt)
12 // commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
13 // MIT/Apache 2.0 license.
14
15 #![doc(html_logo_url = "https://raw.githubusercontent.com/clap-rs/clap/master/assets/clap.png")]
16 #![doc = include_str!("../README.md")]
17 #![forbid(unsafe_code)]
18
19 extern crate proc_macro;
20
21 use proc_macro::TokenStream;
22 use proc_macro_error::proc_macro_error;
23 use syn::{parse_macro_input, DeriveInput};
24
25 mod attr;
26 mod derives;
27 mod dummies;
28 mod item;
29 mod utils;
30
31 /// Generates the `ValueEnum` impl.
32 #[proc_macro_derive(ValueEnum, attributes(clap, value))]
33 #[proc_macro_error]
value_enum(input: TokenStream) -> TokenStream34 pub fn value_enum(input: TokenStream) -> TokenStream {
35 let input: DeriveInput = parse_macro_input!(input);
36 derives::derive_value_enum(&input).into()
37 }
38
39 /// Generates the `Parser` implementation.
40 ///
41 /// This is far less verbose than defining the `clap::Command` struct manually,
42 /// receiving an instance of `clap::ArgMatches` from conducting parsing, and then
43 /// implementing a conversion code to instantiate an instance of the user
44 /// context struct.
45 #[proc_macro_derive(Parser, attributes(clap, structopt, command, arg, group))]
46 #[proc_macro_error]
parser(input: TokenStream) -> TokenStream47 pub fn parser(input: TokenStream) -> TokenStream {
48 let input: DeriveInput = parse_macro_input!(input);
49 derives::derive_parser(&input).into()
50 }
51
52 /// Generates the `Subcommand` impl.
53 #[proc_macro_derive(Subcommand, attributes(clap, command, arg, group))]
54 #[proc_macro_error]
subcommand(input: TokenStream) -> TokenStream55 pub fn subcommand(input: TokenStream) -> TokenStream {
56 let input: DeriveInput = parse_macro_input!(input);
57 derives::derive_subcommand(&input).into()
58 }
59
60 /// Generates the `Args` impl.
61 #[proc_macro_derive(Args, attributes(clap, command, arg, group))]
62 #[proc_macro_error]
args(input: TokenStream) -> TokenStream63 pub fn args(input: TokenStream) -> TokenStream {
64 let input: DeriveInput = parse_macro_input!(input);
65 derives::derive_args(&input).into()
66 }
67