You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: REFERENCE_derive_conf.md
+76Lines changed: 76 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,7 @@ The `#[conf(...)]` attributes conform to [Rust’s structured attribute conventi
21
21
*[Parameter](#parameter)
22
22
*[short](#parameter-short)
23
23
*[long](#parameter-long)
24
+
*[pos](#parameter-pos)
24
25
*[env](#parameter-env)
25
26
*[aliases](#parameter-aliases)
26
27
*[env_aliases](#parameter-env-aliases)
@@ -238,6 +239,81 @@ A parameter represents a single value that can be parsed from a string.
238
239
239
240
*Note*: This behavior is the same as in `clap-derive`.
240
241
242
+
* <aname="parameter-pos"></a> `pos` (no arguments)
243
+
244
+
Specifies that this parameter is a positional argument.
245
+
Positional arguments are identified by their position in the command line, not by a flag name.
246
+
The order of positional arguments is determined by the order of fields in the struct.
247
+
248
+
example: `#[arg(pos)]`
249
+
250
+
example command-line: `./my_prog input.txt output.txt` where the first positional is assigned to the first `pos` field, the second to the second `pos` field, etc.
251
+
252
+
**Positional argument filling**:
253
+
- Positional arguments are filled left-to-right based on their declaration order in the struct
254
+
- You cannot skip a positional argument to provide a later one
255
+
- If you have multiple optional positionals and provide fewer arguments, they fill from left to right
256
+
257
+
**Optional positionals**:
258
+
- A positional parameter can be optional by using `Option<T>` as the field type
259
+
- All optional positional arguments must come after all required positional arguments
260
+
- The parser will panic at construction time if a required positional follows an optional positional
261
+
262
+
**Compatibility**:
263
+
-`pos` is mutually exclusive with `short` and `long` (you cannot have a positional argument with flag names)
264
+
-`pos` is compatible with `env` (the value can be provided via environment variable or positional argument)
265
+
-`pos` is supported in regular `flatten` and in subcommands
266
+
-`pos` is NOT supported in `flatten` with `Option<T>` (flatten optional) - this will produce an error
267
+
268
+
**Examples**:
269
+
270
+
Valid configuration:
271
+
```rust
272
+
#[derive(Conf)]
273
+
structMyConfig {
274
+
/// Input file (required positional)
275
+
#[conf(pos)]
276
+
input:String,
277
+
278
+
/// Output file (required positional)
279
+
#[conf(pos)]
280
+
output:String,
281
+
282
+
/// Optional log file (optional positional - OK because it's at the end)
283
+
#[conf(pos)]
284
+
log_file:Option<String>,
285
+
}
286
+
```
287
+
288
+
Command-line usage: `./my_prog input.txt output.txt` or `./my_prog input.txt output.txt debug.log`
289
+
290
+
Invalid configuration (will panic):
291
+
```rust
292
+
#[derive(Conf)]
293
+
structBadConfig {
294
+
#[conf(pos)]
295
+
first:String,
296
+
297
+
#[conf(pos)]
298
+
second:Option<String>, // optional
299
+
300
+
#[conf(pos)]
301
+
third:String, // ERROR: required after optional!
302
+
}
303
+
```
304
+
305
+
Positional with environment variable fallback:
306
+
```rust
307
+
#[derive(Conf)]
308
+
structMyConfig {
309
+
/// Can be provided as first positional arg or via INPUT env var
310
+
#[conf(pos, env)]
311
+
input:String,
312
+
}
313
+
```
314
+
315
+
Command-line usage: `./my_prog input.txt` or `INPUT=input.txt ./my_prog`
use clap::{builder::Styles, error::ErrorKind,Command,ErrorasClapError};
2
+
use clap::{Command,ErrorasClapError,builder::Styles, error::ErrorKind};
3
3
use std::{ffi::OsString, fmt, fmt::Write};
4
4
5
5
/// An error which occurs when a `Conf::parse` function is called.
@@ -42,9 +42,24 @@ impl Error {
42
42
field_name:&'staticstr,
43
43
field_type_name:&'staticstr,
44
44
) -> Self{
45
-
let buf = format!("Internal error (invalid skip short)\n When flattening {field_type_name} at {field_name}, these short options were not found: {not_found_chars:?}\n To fix this error, remove them from the skip_short attribute list.");
45
+
let buf = format!(
46
+
"Internal error (invalid skip short)\n When flattening {field_type_name} at {field_name}, these short options were not found: {not_found_chars:?}\n To fix this error, remove them from the skip_short attribute list."
// An error reported when positional arguments are used in flatten optional
52
+
#[doc(hidden)]
53
+
pubfnpositional_in_flatten_optional(
54
+
field_name:&str,
55
+
field_type_name:&str,
56
+
option_id:&str,
57
+
) -> Self{
58
+
let buf = format!(
59
+
"Cannot use flatten optional with struct '{field_type_name}' at field '{field_name}' because it contains positional argument '{option_id}'. Positional arguments are not supported in flatten optional structs (but are supported in regular flatten)."
0 commit comments