Skip to content

Commit 40cce9c

Browse files
authored
Merge pull request #411 from antalsz/format-fallback
Add a `format_fallback` method for printing fallback values in the `--help` output with a custom formatting function
2 parents fa29b12 + 50198f2 commit 40cce9c

File tree

25 files changed

+498
-33
lines changed

25 files changed

+498
-33
lines changed

bpaf_derive/src/attrs.rs

+9
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ impl ToTokens for PostDecor {
232232
PostDecor::CompleteShell { f, .. } => quote!(complete_shell(#f)),
233233
PostDecor::DebugFallback { .. } => quote!(debug_fallback()),
234234
PostDecor::DisplayFallback { .. } => quote!(display_fallback()),
235+
PostDecor::FormatFallback { formatter, .. } => quote!(format_fallback(#formatter)),
235236
PostDecor::Fallback { value, .. } => quote!(fallback(#value)),
236237
PostDecor::FallbackWith { f, .. } => quote!(fallback_with(#f)),
237238
PostDecor::Last { .. } => quote!(last()),
@@ -299,6 +300,10 @@ pub(crate) enum PostDecor {
299300
DisplayFallback {
300301
span: Span,
301302
},
303+
FormatFallback {
304+
span: Span,
305+
formatter: Box<Expr>,
306+
},
302307
Fallback {
303308
span: Span,
304309
value: Box<Expr>,
@@ -338,6 +343,7 @@ impl PostDecor {
338343
| Self::CompleteShell { span, .. }
339344
| Self::DebugFallback { span }
340345
| Self::DisplayFallback { span }
346+
| Self::FormatFallback { span, .. }
341347
| Self::Fallback { span, .. }
342348
| Self::Last { span }
343349
| Self::FallbackWith { span, .. }
@@ -512,6 +518,9 @@ impl PostDecor {
512518
Self::DebugFallback { span }
513519
} else if kw == "display_fallback" {
514520
Self::DisplayFallback { span }
521+
} else if kw == "format_fallback" {
522+
let formatter = parse_expr(input)?;
523+
Self::FormatFallback { span, formatter }
515524
} else if kw == "fallback" {
516525
let value = parse_expr(input)?;
517526
Self::Fallback { span, value }

docs2/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `docs2`
2+
3+
A crate containing detailed documentation for `bpaf`. This is the source of the
4+
Markdown documentation files in `src/docs2`; to change those files, update the
5+
files here and then regenerate the documentation.
6+
7+
All the examples in this crate are tested before the new documentation will be
8+
generated. To test the examples and generate the unified documentation files in
9+
`src/docs2`, run `cargo test -p docs2`.

docs2/src/deb_fallback/cases.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ If value is present - fallback value is ignored
66

77
> --jobs 10
88
9-
Parsing errors are preserved and preserved to user
9+
Parsing errors are preserved and presented to the user
1010

1111
> --jobs ten
1212
13-
With [`display_fallback`](ParseFallback::display_fallback) and
14-
[`debug_fallback`](ParseFallback::debug_fallback) you can make it so default value
15-
is visible in `--help` output
13+
With [`display_fallback`](ParseFallback::display_fallback),
14+
[`debug_fallback`](ParseFallback::debug_fallback), and
15+
[`format_fallback`](ParseFallback::format_fallback), you can make it so the default value
16+
is visible in the `--help` output.
1617

1718
> --help

docs2/src/deb_fallback_with/cases.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ If value is present - fallback value is ignored
77

88
> --version 10
99
10-
Parsing errors are preserved and preserved to user
10+
Parsing errors are preserved and presented to the user
1111

1212
> --version ten
1313

docs2/src/dis_fallback/cases.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ If value is present - fallback value is ignored
66

77
> --jobs 10
88
9-
Parsing errors are preserved and preserved to user
9+
Parsing errors are preserved and presented to the user
1010

1111
> --jobs ten
1212
13-
With [`display_fallback`](ParseFallback::display_fallback) and
14-
[`debug_fallback`](ParseFallback::debug_fallback) you can make it so default value
15-
is visible in `--help` output
13+
With [`display_fallback`](ParseFallback::display_fallback),
14+
[`debug_fallback`](ParseFallback::debug_fallback), and
15+
[`format_fallback`](ParseFallback::format_fallback), you can make it so the default value
16+
is visible in the `--help` output.
1617

1718
> --help

docs2/src/dis_fallback_with/cases.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ If value is present - fallback value is ignored
77

88
> --version 10
99
10-
Parsing errors are preserved and preserved to user
10+
Parsing errors are preserved and presented to the user
1111

1212
> --version ten
1313

docs2/src/fallback/cases.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ If value is present - fallback value is ignored
66

77
> --jobs 10
88
9-
Parsing errors are preserved and preserved to user
9+
Parsing errors are preserved and presented to the user
1010

1111
> --jobs ten
1212

docs2/src/fallback_with/cases.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ If value is present - fallback value is ignored
77

88
> --version 10
99
10-
Parsing errors are preserved and preserved to user
10+
Parsing errors are preserved and presented to the user
1111

1212
> --version ten
1313

docs2/src/format_fallback/cases.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
`fallback` changes parser to fallback to a default value used when argument is not specified
2+
3+
>
4+
5+
If value is present - fallback value is ignored
6+
7+
> --log-file output.txt
8+
9+
Parsing errors are preserved and presented to the user
10+
11+
> --log-file /
12+
13+
With [`display_fallback`](ParseFallback::display_fallback),
14+
[`debug_fallback`](ParseFallback::debug_fallback), and
15+
[`format_fallback`](ParseFallback::format_fallback), you can make it so the default value
16+
is visible in the `--help` output.
17+
18+
> --help

docs2/src/format_fallback/combine.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
use bpaf::*;
3+
use std::{fmt::Display as _, path::PathBuf};
4+
#[derive(Debug, Clone)]
5+
pub struct Options {
6+
log_file: PathBuf,
7+
}
8+
9+
pub fn options() -> OptionParser<Options> {
10+
let log_file = long("log-file")
11+
.help("Path to log file")
12+
.argument::<PathBuf>("FILE")
13+
.guard(
14+
|log_file| !log_file.is_dir(),
15+
"The log file can't be a directory",
16+
)
17+
.fallback(PathBuf::from("logfile.txt"))
18+
.format_fallback(|path, f| path.display().fmt(f));
19+
construct!(Options { log_file }).to_options()
20+
}

docs2/src/format_fallback/derive.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
use bpaf::*;
3+
use std::{fmt::Display as _, path::PathBuf};
4+
#[derive(Debug, Clone, Bpaf)]
5+
#[bpaf(options)]
6+
#[allow(dead_code)]
7+
pub struct Options {
8+
/// Path to log file
9+
#[bpaf(
10+
argument("FILE"),
11+
guard(|log_file| !log_file.is_dir(), "The log file can't be a directory"),
12+
fallback(PathBuf::from("logfile.txt")),
13+
format_fallback(|path, f| path.display().fmt(f)),
14+
)]
15+
log_file: PathBuf,
16+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
`fallback_with` changes parser to fallback to a value that comes from a potentially failing
2+
computation when argument is not specified
3+
4+
>
5+
6+
If value is present - fallback value is ignored
7+
8+
> --log-file output.txt
9+
10+
Parsing errors are preserved and presented to the user
11+
12+
> --log-file /
13+
14+
`bpaf` encases parsers with fallback value of some sort in usage with `[]`
15+
16+
> --help
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
use bpaf::*;
3+
use std::{fmt::Display as _, path::PathBuf};
4+
fn try_to_get_log_file() -> Result<PathBuf, &'static str> {
5+
Ok(PathBuf::from("logfile.txt"))
6+
}
7+
8+
#[derive(Debug, Clone)]
9+
pub struct Options {
10+
log_file: PathBuf,
11+
}
12+
13+
pub fn options() -> OptionParser<Options> {
14+
let log_file = long("log-file")
15+
.help("Path to log file")
16+
.argument::<PathBuf>("FILE")
17+
.guard(
18+
|log_file| !log_file.is_dir(),
19+
"The log file can't be a directory",
20+
)
21+
.fallback_with(try_to_get_log_file)
22+
.format_fallback(|path, f| path.display().fmt(f));
23+
construct!(Options { log_file }).to_options()
24+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
use bpaf::*;
3+
use std::{fmt::Display as _, path::PathBuf};
4+
fn try_to_get_log_file() -> Result<PathBuf, &'static str> {
5+
Ok(PathBuf::from("logfile.txt"))
6+
}
7+
8+
#[derive(Debug, Clone, Bpaf)]
9+
#[bpaf(options)]
10+
pub struct Options {
11+
/// Path to log file
12+
#[bpaf(
13+
argument("FILE"),
14+
guard(|log_file| !log_file.is_dir(), "The log file can't be a directory"),
15+
fallback_with(try_to_get_log_file),
16+
format_fallback(|path, f| path.display().fmt(f)),
17+
)]
18+
log_file: PathBuf,
19+
}

src/docs2/deb_fallback.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Options { jobs: 10 }
5959
</div>
6060

6161

62-
Parsing errors are preserved and preserved to user
62+
Parsing errors are preserved and presented to the user
6363

6464

6565
<div class='bpaf-doc'>
@@ -80,9 +80,10 @@ div.bpaf-doc { padding-left: 1em; }
8080
</div>
8181

8282

83-
With [`display_fallback`](ParseFallback::display_fallback) and
84-
[`debug_fallback`](ParseFallback::debug_fallback) you can make it so default value
85-
is visible in `--help` output
83+
With [`display_fallback`](ParseFallback::display_fallback),
84+
[`debug_fallback`](ParseFallback::debug_fallback), and
85+
[`format_fallback`](ParseFallback::format_fallback), you can make it so the default value
86+
is visible in the `--help` output.
8687

8788

8889
<div class='bpaf-doc'>

src/docs2/deb_fallback_with.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Options { version: 10 }
6767
</div>
6868

6969

70-
Parsing errors are preserved and preserved to user
70+
Parsing errors are preserved and presented to the user
7171

7272

7373
<div class='bpaf-doc'>

src/docs2/dis_fallback.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Options { jobs: 10 }
5959
</div>
6060

6161

62-
Parsing errors are preserved and preserved to user
62+
Parsing errors are preserved and presented to the user
6363

6464

6565
<div class='bpaf-doc'>
@@ -80,9 +80,10 @@ div.bpaf-doc { padding-left: 1em; }
8080
</div>
8181

8282

83-
With [`display_fallback`](ParseFallback::display_fallback) and
84-
[`debug_fallback`](ParseFallback::debug_fallback) you can make it so default value
85-
is visible in `--help` output
83+
With [`display_fallback`](ParseFallback::display_fallback),
84+
[`debug_fallback`](ParseFallback::debug_fallback), and
85+
[`format_fallback`](ParseFallback::format_fallback), you can make it so the default value
86+
is visible in the `--help` output.
8687

8788

8889
<div class='bpaf-doc'>

src/docs2/dis_fallback_with.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Options { version: 10 }
6767
</div>
6868

6969

70-
Parsing errors are preserved and preserved to user
70+
Parsing errors are preserved and presented to the user
7171

7272

7373
<div class='bpaf-doc'>

src/docs2/fallback.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Options { jobs: 10 }
5959
</div>
6060

6161

62-
Parsing errors are preserved and preserved to user
62+
Parsing errors are preserved and presented to the user
6363

6464

6565
<div class='bpaf-doc'>

src/docs2/fallback_with.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Options { version: 10 }
6767
</div>
6868

6969

70-
Parsing errors are preserved and preserved to user
70+
Parsing errors are preserved and presented to the user
7171

7272

7373
<div class='bpaf-doc'>

0 commit comments

Comments
 (0)