aargvark 0.9.6

Self-similar argument parsing
Documentation
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use {
    comfy_table::Cell,
    std::{
        any::TypeId,
        cell::RefCell,
        collections::{
            HashMap,
            HashSet,
        },
        rc::Rc,
    },
};

fn style_usage(s: impl AsRef<str>) -> String {
    return s.as_ref().to_string();
}

fn style_description(s: impl AsRef<str>) -> String {
    return s.as_ref().to_string();
}

fn style_id(s: impl AsRef<str>) -> String {
    return console::Style::new().blue().dim().apply_to(s.as_ref()).to_string();
}

fn style_type(s: impl AsRef<str>) -> String {
    return console::Style::new().magenta().apply_to(s.as_ref()).to_string();
}

fn style_logical(s: impl AsRef<str>) -> String {
    return console::Style::new().dim().apply_to(s.as_ref()).to_string();
}

fn style_literal(s: impl AsRef<str>) -> String {
    return console::Style::new().bold().apply_to(s.as_ref()).to_string();
}

#[doc(hidden)]
#[derive(Hash, PartialEq, Eq, Clone, Copy, Debug)]
pub struct HelpProductionKey {
    type_id: TypeId,
    variant: usize,
}

#[doc(hidden)]
pub struct HelpProduction {
    id: String,
    description: String,
    content: HelpProductionType,
}

#[doc(hidden)]
pub enum HelpPartialContent {
    Pattern(HelpPattern),
    Production(HelpProductionType),
}

impl HelpPartialContent {
    pub fn struct_(fields: Vec<HelpField>, optional_fields: Vec<HelpFlagField>) -> Self {
        return HelpPartialContent::Production(
            HelpProductionType::Struct(Rc::new(RefCell::new(HelpProductionTypeStruct {
                fields: fields,
                flag_fields: optional_fields,
            }))),
        );
    }

    pub fn enum_(variants: Vec<HelpVariant>) -> Self {
        return HelpPartialContent::Production(HelpProductionType::Enum(Rc::new(RefCell::new(variants))));
    }
}

/// State for a partially-parsed field for rendering help.
pub struct HelpPartialProduction {
    pub description: String,
    pub content: HelpPartialContent,
}

pub enum HelpProductionType {
    Struct(Rc<RefCell<HelpProductionTypeStruct>>),
    Enum(Rc<RefCell<Vec<HelpVariant>>>),
}

pub struct HelpProductionTypeStruct {
    pub fields: Vec<HelpField>,
    pub flag_fields: Vec<HelpFlagField>,
}

pub struct HelpField {
    pub id: String,
    pub pattern: HelpPattern,
    pub description: String,
}

pub struct HelpFlagField {
    pub option: bool,
    pub flags: Vec<String>,
    pub pattern: HelpPattern,
    pub description: String,
}

pub struct HelpVariant {
    pub literal: String,
    pub pattern: HelpPattern,
    pub description: String,
}

/// Structured help information - this list of pattern elements that is styled and
/// joined with spaces on output.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct HelpPattern(pub Vec<HelpPatternElement>);

impl HelpPattern {
    pub fn render(&self, stack: &mut Vec<(HelpProductionKey, Rc<HelpProduction>)>, state: &HelpState) -> String {
        let mut out = String::new();
        for (i, e) in self.0.iter().enumerate() {
            if i > 0 {
                out.push_str(" ");
            }
            out.push_str(&e.render(stack, state));
        }
        return out;
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum HelpPatternElement {
    // xyz - denotes literal text user must type
    Literal(String),
    // `<XYZ>` - denotes type of data for user
    Type(String),
    // XYZ - refers to another section of help output
    Reference(HelpProductionKey),
    // Like reference, but the other section might not exist.  Used for `...` when
    // using `help_break` to limit output.
    PseudoReference(String),
    // `[XYZ]` - indicates a pattern is optional
    Option(HelpPattern),
    // `XYZ[ ...]` - indicates a pattern can be repeated, space separated
    Array(HelpPattern),
    // `xyz | abc` - indicates one of multiple patterns can be selected
    Variant(Vec<HelpPattern>),
}

impl HelpPatternElement {
    fn render(&self, stack: &mut Vec<(HelpProductionKey, Rc<HelpProduction>)>, state: &HelpState) -> String {
        match self {
            HelpPatternElement::Literal(l) => return style_literal(l),
            HelpPatternElement::Type(i) => return style_type(format!("<{}>", i)),
            HelpPatternElement::Reference(i) => {
                let production = state.productions.get(i).unwrap();
                stack.push((*i, production.clone()));
                return style_id(production.id.as_str())
            },
            HelpPatternElement::PseudoReference(key) => {
                return style_id(key.as_str())
            },
            HelpPatternElement::Option(i) => return format!(
                "{}{}{}",
                style_logical("["),
                i.render(stack, state),
                style_logical("]")
            ),
            HelpPatternElement::Array(i) => return format!("{}{}", i.render(stack, state), style_logical("[ ...]")),
            HelpPatternElement::Variant(i) => return i
                .iter()
                .map(|x| x.render(stack, state))
                .collect::<Vec<_>>()
                .join(&style_logical(" | ")),
        }
    }
}

#[doc(hidden)]
#[derive(Default)]
pub struct HelpState {
    // Write during building
    name_counter: HashMap<String, usize>,
    // Write during building, read during rendering
    productions: HashMap<HelpProductionKey, Rc<HelpProduction>>,
}

impl HelpState {
    fn add(
        &mut self,
        type_id: TypeId,
        type_id_variant: usize,
        id: impl ToString,
        description: impl ToString,
        content: HelpProductionType,
    ) -> HelpProductionKey {
        let mut id = id.to_string();
        let count = *self.name_counter.entry(id.clone()).and_modify(|x| *x += 1).or_insert(1);
        if count > 1 {
            id = format!("{} ({})", id, count);
        }
        let key = HelpProductionKey {
            type_id: type_id,
            variant: type_id_variant,
        };
        self.productions.insert(key, Rc::new(HelpProduction {
            id: id,
            description: description.to_string(),
            content: content,
        }));
        return key;
    }

    pub fn add_struct(
        &mut self,
        type_id: TypeId,
        type_id_variant: usize,
        id: impl ToString,
        description: impl ToString,
    ) -> (HelpProductionKey, Rc<RefCell<HelpProductionTypeStruct>>) {
        let out = Rc::new(RefCell::new(HelpProductionTypeStruct {
            fields: vec![],
            flag_fields: vec![],
        }));
        let key = self.add(type_id, type_id_variant, id, description, HelpProductionType::Struct(out.clone()));
        return (key, out);
    }

    pub fn add_enum(
        &mut self,
        type_id: TypeId,
        type_id_variant: usize,
        id: impl ToString,
        description: impl ToString,
    ) -> (HelpProductionKey, Rc<RefCell<Vec<HelpVariant>>>) {
        let out = Rc::new(RefCell::new(vec![]));
        let key = self.add(type_id, type_id_variant, id, description, HelpProductionType::Enum(out.clone()));
        return (key, out);
    }
}

/// State required for building a help string.
pub struct VarkRetHelp {
    pub(crate) command: Option<String>,
    pub(crate) args: Vec<String>,
    pub(crate) consumed_args: usize,
    pub(crate) builder: Box<dyn FnOnce(&mut HelpState) -> HelpPartialProduction>,
}

impl VarkRetHelp {
    /// Build a help string using current operating environment line widths.
    pub fn render(self) -> String {
        fn format_desc(out: &mut String, desc: &str) {
            if !desc.is_empty() {
                out.push_str(
                    &style_description(
                        textwrap::wrap(
                            desc,
                            &textwrap::Options::with_termwidth().initial_indent("    ").subsequent_indent("    "),
                        ).join("\n"),
                    ),
                );
                out.push_str("\n\n");
            }
        }

        fn format_pattern(out: &mut String, content: &HelpProductionType) {
            match content {
                HelpProductionType::Struct(struct_) => {
                    let struct_ = struct_.borrow();
                    for f in &struct_.fields {
                        out.push_str(" ");
                        out.push_str(&style_id(&f.id));
                    }
                    if !struct_.flag_fields.is_empty() {
                        let all_opt = struct_.flag_fields.iter().all(|x| x.option);
                        out.push_str(" ");
                        if all_opt {
                            out.push_str(&style_logical("[ ...FLAGS]"));
                        } else {
                            out.push_str(&style_logical("...FLAGS"));
                        }
                    }
                },
                HelpProductionType::Enum(fields) => {
                    for (i, f) in fields.borrow().iter().enumerate() {
                        if i > 0 {
                            out.push_str(" |");
                        }
                        out.push_str(" ");
                        out.push_str(&style_literal(&f.literal));
                    }
                },
            }
        }

        fn format_content(
            out: &mut String,
            stack: &mut Vec<(HelpProductionKey, Rc<HelpProduction>)>,
            help_state: &HelpState,
            content: &HelpProductionType,
        ) {
            let mut table = comfy_table::Table::new();
            table.load_preset(comfy_table::presets::NOTHING);
            table.set_content_arrangement(comfy_table::ContentArrangement::Dynamic);
            match content {
                HelpProductionType::Struct(struct_) => {
                    let struct_ = struct_.borrow();
                    for f in &struct_.fields {
                        table.add_row(
                            vec![
                                comfy_table::Cell::new(
                                    format!("   {}: {}", style_id(&f.id), f.pattern.render(stack, help_state)),
                                ),
                                Cell::new(style_description(&f.description))
                            ],
                        );
                    }
                    for f in &struct_.flag_fields {
                        let first_flag = f.flags.first().unwrap();
                        for (i, flag) in f.flags.iter().enumerate() {
                            let mut elems = vec![HelpPatternElement::Literal(flag.clone())];
                            elems.extend(f.pattern.0.clone());
                            let left_col = format!("   {}", if f.option {
                                HelpPattern(vec![HelpPatternElement::Option(HelpPattern(elems))])
                            } else {
                                HelpPattern(elems)
                            }.render(stack, help_state));
                            if i == 0 {
                                table.add_row(
                                    vec![
                                        comfy_table::Cell::new(left_col),
                                        Cell::new(style_description(&f.description))
                                    ],
                                );
                            } else {
                                table.add_row(
                                    vec![
                                        comfy_table::Cell::new(left_col),
                                        Cell::new(style_description(&format!("(synonym for `{}`)", first_flag)))
                                    ],
                                );
                            }
                        }
                    }
                },
                HelpProductionType::Enum(fields) => {
                    for f in &*fields.borrow() {
                        table.add_row(
                            vec![
                                comfy_table::Cell::new(
                                    format!("   {} {}", style_literal(&f.literal), f.pattern.render(stack, help_state)),
                                ),
                                Cell::new(style_description(&f.description))
                            ],
                        );
                    }
                },
            }
            table.set_constraints(vec![comfy_table::ColumnConstraint::Boundaries {
                lower: comfy_table::Width::Percentage(20),
                upper: comfy_table::Width::Percentage(60),
            }]);
            out.push_str(&table.to_string());
            out.push_str("\n\n");
        }

        let mut help_state = HelpState::default();
        let mut stack = Vec::<(HelpProductionKey, Rc<HelpProduction>)>::new();
        let mut seen_productions = HashSet::<HelpProductionKey>::new();
        let partial = (self.builder)(&mut help_state);

        // Write initial partial production
        let mut out = style_usage("Usage:");
        if let Some(s) = &self.command {
            out.push_str(" ");
            out.push_str(&style_usage(s));
        }
        for s in self.args.iter().take(self.consumed_args) {
            out.push_str(" ");
            out.push_str(&style_usage(s));
        }
        let mut temp_stack = vec![];
        match &partial.content {
            HelpPartialContent::Pattern(p) => {
                if !p.0.is_empty() {
                    out.push_str(" ");
                    out.push_str(&p.render(&mut temp_stack, &help_state));
                }
            },
            HelpPartialContent::Production(content) => {
                format_pattern(&mut out, &content);
            },
        }
        out.push_str("\n\n");
        format_desc(&mut out, &partial.description);
        match &partial.content {
            HelpPartialContent::Pattern(_) => {
                out.push_str("\n\n");
            },
            HelpPartialContent::Production(content) => {
                format_content(&mut out, &mut temp_stack, &mut help_state, content);
            },
        }
        temp_stack.reverse();
        stack.extend(temp_stack);

        // Recurse productions
        while let Some((key, top)) = stack.pop() {
            if !seen_productions.insert(key) {
                continue;
            }
            out.push_str(&style_id(&top.id));
            out.push_str(":");
            format_pattern(&mut out, &top.content);
            out.push_str("\n\n");
            format_desc(&mut out, &top.description);
            let mut temp_stack = vec![];
            format_content(&mut out, &mut temp_stack, &mut help_state, &top.content);
            temp_stack.reverse();
            stack.extend(temp_stack);
        }
        return out;
    }
}