1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Derive macros for click-rs CLI library.
//!
//! This crate provides procedural macros for automatically generating
//! `CommandLike` implementations from Rust structs.
//!
//! # Example
//!
//! ```ignore
//! use click_derive::Command;
//!
//! #[derive(Command)]
//! #[command(name = "greet")]
//! /// A friendly greeter
//! struct Greet {
//! /// Name to greet
//! #[argument]
//! name: String,
//!
//! /// Number of times to greet
//! #[option(short, long, default = 1)]
//! count: i32,
//! }
//!
//! impl Greet {
//! fn run(&self, _ctx: &click::Context) -> click::Result<()> {
//! for _ in 0..self.count {
//! println!("Hello, {}!", self.name);
//! }
//! Ok(())
//! }
//! }
//! ```
use TokenStream;
use ;
use expand_command;
use ;
use expand_group;
/// Derive macro for creating CLI commands from structs.
///
/// # Container Attributes
///
/// - `#[command(name = "...")]` - Set the command name (defaults to struct name in kebab-case)
/// - `#[command(help = "...")]` - Set the help text (defaults to doc comment)
/// - `#[command(run)]` - Wire `Self::run(&self|self, &Context) -> Result<()>` as callback
/// - `#[command(hidden)]` - Hide the command from help
/// - `#[command(no_args_is_help)]` - Show help when no arguments provided
///
/// # Field Attributes
///
/// ## Options
///
/// - `#[option(short, long)]` - Create option with short (-n) and long (--name) flags
/// - `#[option(short = 'n')]` - Specify custom short flag
/// - `#[option(long = "name")]` - Specify custom long flag name
/// - `#[option(help = "...")]` - Set help text
/// - `#[option(default = value)]` - Set default value
/// - `#[option(required)]` - Mark as required
/// - `#[option(count)]` - Count occurrences (-v -v -v = 3)
/// - `#[option(flag)]` - Boolean flag (no value)
/// - `#[option(nargs = -1)]` - Variadic option values
/// - `#[option(type = click::PathType::new())]` - Explicit converter expression
/// - `#[option(validate = my_validator)]` - Declarative validation (`fn(&T) -> Result<(), String>`)
/// - `#[option(envvar = "VAR")]` - Read from environment variable
/// - `#[option(dest = "name")]` - Override destination parameter name
/// - `#[option(shell_complete = my_completer)]` - Custom value completion callback
///
/// ## Arguments
///
/// - `#[argument]` - Positional argument
/// - `#[argument(help = "...")]` - Set help text
/// - `#[argument(required = false)]` - Optional argument
/// - `#[argument(multiple)]` - Accept multiple values
/// - `#[argument(nargs = -1)]` - Variadic argument values
/// - `#[argument(type = click::FileType::new())]` - Explicit converter expression
/// - `#[argument(validate = my_validator)]` - Declarative validation (`fn(&T) -> Result<(), String>`)
/// - `#[argument(shell_complete = my_completer)]` - Custom completion callback
///
/// # Example
///
/// ```ignore
/// #[derive(Command)]
/// #[command(name = "greet")]
/// struct Greet {
/// #[option(short, long)]
/// name: String,
///
/// #[option(short, long, default = 1)]
/// count: i32,
///
/// #[argument]
/// target: String,
/// }
/// ```
/// Derive macro for creating CLI command groups from structs.
///
/// # Container Attributes
///
/// - `#[group(name = "...")]` - Set the group name
/// - `#[group(help = "...")]` - Set the help text
/// - `#[group(run)]` - Wire `Self::run(&self|self, &Context) -> Result<()>` as callback
/// - `#[group(chain)]` - Enable command chaining
/// - `#[group(invoke_without_command)]` - Run callback even without subcommand
///
/// # Field Attributes
///
/// - `#[subcommand]` - Mark field as a subcommand
///
/// # Example
///
/// ```ignore
/// #[derive(Group)]
/// #[group(name = "cli")]
/// struct Cli {
/// #[option(short, long)]
/// verbose: bool,
///
/// #[subcommand]
/// command: Commands,
/// }
///
/// enum Commands {
/// Add(AddCmd),
/// Remove(RemoveCmd),
/// }
/// ```
/// Attribute macro for function-first command definitions.
///
/// The annotated function parameters must use click parameter attributes such as
/// `#[option(...)]` or `#[argument(...)]`. The macro generates:
/// - a hidden derive-backed struct that carries the parameter metadata
/// - a `<function_name>_command()` helper that builds a `click::Command`
///
/// # Example
///
/// ```ignore
/// #[click::command(name = "hello")]
/// fn hello(#[argument] name: String, #[option(short, long)] loud: bool) -> click::Result<()> {
/// if loud { println!("HELLO, {}!", name); } else { println!("Hello, {}!", name); }
/// Ok(())
/// }
///
/// let cmd = hello_command();
/// ```
/// Attribute macro for function-first group definitions.
///
/// Supports all `#[group(...)]` options accepted by the derive macro, plus:
/// - `commands = [a, b, c]` where each entry resolves to `<name>_command()` (or can be explicit calls)
/// - `groups = [nested]` where each entry resolves to `<name>_group()` (or can be explicit calls)
///
/// # Example
///
/// ```ignore
/// #[click::group(name = "cli", commands = [hello], groups = [admin])]
/// fn cli(#[option(short, long)] verbose: bool) -> click::Result<()> {
/// if verbose { println!("verbose mode"); }
/// Ok(())
/// }
///
/// let grp = cli_group();
/// ```