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
|
use arg_picker::{IntoPicker, PickerArg, PickerArgs, value::Flag};
use mingling_core::{Program, ProgramCollect, setup::ProgramSetup};
use crate::setups::picker::{CONFIRM_FLAG, HELP_FLAG, QUIET_FLAG};
/// Helper: picks a boolean flag from the program arguments, calls `f` with the
/// flag value, then replaces the program arguments with the remaining args.
fn pick_flag<'a, C>(
program: &mut Program<C>,
flag: &PickerArg<'a, Flag>,
f: impl FnOnce(bool, &mut Program<C>),
) where
C: ProgramCollect<Enum = C>,
{
let args = program.take_args();
let remains_arg = PickerArg::<PickerArgs<'a>>::new(&[], None, true);
let (active, remains) = args.pick(flag).pick(&remains_arg).unwrap();
f(*active, program);
program.replace_args(remains.into());
}
/// Performs basic program initialization:
///
/// - Collects `--quiet` flag to control message rendering
/// - Collects `--help` flag to enable help mode
/// - Collects `--confirm` flag to skip user confirmation
pub struct BasicProgramSetup;
impl<C> ProgramSetup<C> for BasicProgramSetup
where
C: ProgramCollect<Enum = C>,
{
fn setup(self, program: &mut Program<C>) {
program.with_setup(HelpFlagSetup::default());
program.with_setup(QuietFlagSetup::default());
program.with_setup(ConfirmFlagSetup::default());
}
}
/// Provides setup for parsing the user help flag
///
/// The default value is `--help / -h`
pub struct HelpFlagSetup<'a> {
flag: &'a PickerArg<'a, Flag>,
}
impl<'a> HelpFlagSetup<'a> {
/// Creates a new `HelpFlagSetup` with the given flag aliases.
pub fn new(flag: &'a PickerArg<Flag>) -> Self {
Self { flag }
}
}
impl<'a, C> ProgramSetup<C> for HelpFlagSetup<'a>
where
C: ProgramCollect<Enum = C>,
{
fn setup(self, program: &mut Program<C>) {
pick_flag(program, self.flag, |active, ctx| {
ctx.user_context.help = active;
});
}
}
impl<'a> Default for HelpFlagSetup<'a> {
fn default() -> Self {
Self { flag: &HELP_FLAG }
}
}
/// Provides setup for parsing the quiet flag
///
/// The default value is `--quiet / -q`
pub struct QuietFlagSetup<'a> {
flag: &'a PickerArg<'a, Flag>,
}
impl<'a> QuietFlagSetup<'a> {
/// Creates a new `QuietFlagSetup` with the given flag aliases.
pub fn new(flag: &'a PickerArg<Flag>) -> Self {
Self { flag }
}
}
impl<'a, C> ProgramSetup<C> for QuietFlagSetup<'a>
where
C: ProgramCollect<Enum = C>,
{
fn setup(self, program: &mut Program<C>) {
pick_flag(program, self.flag, |active, ctx| {
if active {
ctx.stdout_setting.render_output = false;
ctx.stdout_setting.error_output = false;
}
});
}
}
impl<'a> Default for QuietFlagSetup<'a> {
fn default() -> Self {
Self { flag: &QUIET_FLAG }
}
}
/// Provides setup for parsing the confirm flag
///
/// The default value is `--confirm / -C`
pub struct ConfirmFlagSetup<'a> {
flag: &'a PickerArg<'a, Flag>,
}
impl<'a> ConfirmFlagSetup<'a> {
/// Creates a new `ConfirmFlagSetup` with the given flag aliases.
pub fn new(flag: &'a PickerArg<Flag>) -> Self {
Self { flag }
}
}
impl<'a, C> ProgramSetup<C> for ConfirmFlagSetup<'a>
where
C: ProgramCollect<Enum = C>,
{
fn setup(self, program: &mut Program<C>) {
pick_flag(program, self.flag, |active, ctx| {
if active {
ctx.user_context.confirm = true;
}
});
}
}
impl<'a> Default for ConfirmFlagSetup<'a> {
fn default() -> Self {
Self {
flag: &CONFIRM_FLAG,
}
}
}
|