jubarte 0.20.0

Word-faithful DOCX redline engine — compare two Word documents into a tracked-changes document that opens cleanly in Microsoft Word; list, accept, or reject revisions
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
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
//
// SPDX-License-Identifier: Apache-2.0

//! Prints every flag of the `jubarte` command line as Markdown, read from
//! clap's own model rather than from `--help` text, so hidden flags, hidden
//! aliases, defaults, possible values and conflicts are all listed.
//! `scripts/cli_flags.sh` runs it and writes `docs/flags_datetime.md`.
//!
//! Sections: one table per task (hidden tasks included), then the flags by
//! name across tasks (one long name with different shorts, values or
//! defaults; one short letter for different long names), the timestamp
//! flags, and the retired spellings.

use std::collections::BTreeMap;
use std::fmt::Write as _;

use clap::{Arg, Command};

/// One option or positional of one task, flattened for the tables.
struct Row {
    task: String,
    short: Option<char>,
    long: Option<String>,
    id: String,
    aliases: Vec<String>,
    value: String,
    default: String,
    possible: String,
    notes: Vec<String>,
    help: String,
}

/// `text` on one Markdown table line.
fn cell(text: &str) -> String {
    text.split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
        .replace('|', "\\|")
}

/// What `arg` takes after it: `<FILE>`, `[<PLAN.json>]`, `<SPEC>...`, or
/// nothing for a flag.
fn value_shape(arg: &Arg) -> String {
    let Some(range) = arg.get_num_args() else {
        return String::new();
    };
    if !range.takes_values() {
        return String::new();
    }
    let names = arg
        .get_value_names()
        .map(|names| {
            names
                .iter()
                .map(|n| format!("<{n}>"))
                .collect::<Vec<_>>()
                .join(" ")
        })
        .unwrap_or_else(|| format!("<{}>", arg.get_id().as_str().to_uppercase()));
    let shaped = if range.min_values() == 0 {
        format!("[{names}]")
    } else {
        names
    };
    if range.max_values() > 1 {
        format!("{shaped}...")
    } else {
        shaped
    }
}

fn row(task: &str, cmd: &Command, arg: &Arg) -> Row {
    let mut aliases = Vec::new();
    let visible = arg.get_visible_aliases().unwrap_or_default();
    for alias in arg.get_all_aliases().unwrap_or_default() {
        let hidden = if visible.contains(&alias) {
            ""
        } else {
            " (hidden)"
        };
        aliases.push(format!("--{alias}{hidden}"));
    }
    let visible_short = arg.get_visible_short_aliases().unwrap_or_default();
    for alias in arg.get_all_short_aliases().unwrap_or_default() {
        let hidden = if visible_short.contains(&alias) {
            ""
        } else {
            " (hidden)"
        };
        aliases.push(format!("-{alias}{hidden}"));
    }
    let mut notes = Vec::new();
    if arg.is_hide_set() {
        notes.push("hidden".to_string());
    }
    if arg.is_global_set() {
        notes.push("global".to_string());
    }
    if arg.is_required_set() {
        notes.push("required".to_string());
    }
    if matches!(
        arg.get_action(),
        clap::ArgAction::Append | clap::ArgAction::Count
    ) {
        notes.push(format!("{:?}", arg.get_action()).to_lowercase());
    }
    if let Some(heading) = arg.get_help_heading() {
        notes.push(format!("under \"{heading}\""));
    }
    // Both ways: the conflicts `arg` declares and the args that declare a
    // conflict with it (clap reports only the first).
    let mut conflicts: Vec<String> = cmd
        .get_arg_conflicts_with(arg)
        .into_iter()
        .chain(
            cmd.get_arguments()
                .filter(|other| other.get_id() != arg.get_id())
                .filter(|other| {
                    cmd.get_arg_conflicts_with(other)
                        .iter()
                        .any(|c| c.get_id() == arg.get_id())
                }),
        )
        .map(|other| match other.get_long() {
            Some(long) => format!("--{long}"),
            None => other.get_id().as_str().to_string(),
        })
        .collect();
    conflicts.sort();
    conflicts.dedup();
    if !conflicts.is_empty() {
        notes.push(format!("conflicts {}", conflicts.join(" ")));
    }
    let default = arg
        .get_default_values()
        .iter()
        .map(|v| v.to_string_lossy().into_owned())
        .collect::<Vec<_>>()
        .join(",");
    let possible = arg
        .get_possible_values()
        .iter()
        .filter(|v| !v.is_hide_set())
        .map(|v| v.get_name_and_aliases().collect::<Vec<_>>().join("="))
        .collect::<Vec<_>>()
        .join(", ");
    let help = arg
        .get_long_help()
        .or_else(|| arg.get_help())
        .map(|h| h.to_string())
        .unwrap_or_default();
    Row {
        task: task.to_string(),
        short: arg.get_short(),
        long: arg.get_long().map(str::to_string),
        id: arg.get_id().as_str().to_string(),
        aliases,
        value: value_shape(arg),
        default,
        possible,
        notes,
        help,
    }
}

/// The flag as written: `-p, --location`, or `<FILE>` for a positional.
fn spelled(row: &Row) -> String {
    match (row.short, &row.long) {
        (Some(s), Some(l)) => format!("-{s}, --{l}"),
        (Some(s), None) => format!("-{s}"),
        (None, Some(l)) => format!("--{l}"),
        (None, None) => format!("<{}>", row.id.to_uppercase()),
    }
}

/// Every task, depth first, as `(path, command)`; the generated `help`
/// subcommand is left out.
fn tasks<'a>(path: &str, cmd: &'a Command, out: &mut Vec<(String, &'a Command)>) {
    out.push((path.to_string(), cmd));
    for sub in cmd.get_subcommands() {
        if sub.get_name() == "help" {
            continue;
        }
        let child = if path.is_empty() {
            sub.get_name().to_string()
        } else {
            format!("{path} {}", sub.get_name())
        };
        tasks(&child, sub, out);
    }
}

fn main() {
    let mut root = jubarte::cli::top_level_command();
    root.build();
    let mut all = Vec::new();
    tasks("", &root, &mut all);

    let mut out = String::new();
    let mut rows: Vec<Row> = Vec::new();
    for (path, cmd) in &all {
        let title = if path.is_empty() {
            "jubarte (no task: the compare/read shorthand)".to_string()
        } else {
            format!("jubarte {path}")
        };
        let mut flags = Vec::new();
        if cmd.is_hide_set() {
            flags.push("hidden task".to_string());
        }
        let visible: Vec<&str> = cmd.get_visible_aliases().collect();
        let aliases: Vec<String> = cmd
            .get_all_aliases()
            .map(|alias| {
                if visible.contains(&alias) {
                    alias.to_string()
                } else {
                    format!("{alias} (hidden)")
                }
            })
            .collect();
        if !aliases.is_empty() {
            flags.push(format!("aliases: {}", aliases.join(", ")));
        }
        let suffix = if flags.is_empty() {
            String::new()
        } else {
            format!(" ({})", flags.join("; "))
        };
        let _ = writeln!(out, "## {title}{suffix}\n");
        if let Some(about) = cmd.get_about() {
            let _ = writeln!(out, "{}\n", cell(&about.to_string()));
        }
        let _ = writeln!(
            out,
            "| Flag | Aliases | Value | Default | Possible values | Notes | Help |"
        );
        let _ = writeln!(out, "|-|-|-|-|-|-|-|");
        for arg in cmd.get_arguments() {
            let id = arg.get_id().as_str();
            if id == "help" || id == "version" {
                continue;
            }
            let r = row(path, cmd, arg);
            let _ = writeln!(
                out,
                "| `{}` | {} | {} | {} | {} | {} | {} |",
                spelled(&r),
                cell(&r.aliases.join(", ")),
                cell(&r.value),
                cell(&r.default),
                cell(&r.possible),
                cell(&r.notes.join("; ")),
                cell(&r.help),
            );
            rows.push(r);
        }
        out.push('\n');
    }

    // One long name across tasks: the shorts, values and defaults it takes.
    // Every accepted long spelling, aliases included, so `--date` lists the
    // tasks that take it as an alias of `--datetime`.
    let mut by_long: BTreeMap<&str, Vec<&Row>> = BTreeMap::new();
    for r in &rows {
        if let Some(long) = &r.long {
            by_long.entry(long).or_default().push(r);
        }
        for alias in &r.aliases {
            if let Some(name) = alias.strip_prefix("--") {
                let name = name.strip_suffix(" (hidden)").unwrap_or(name);
                by_long.entry(name).or_default().push(r);
            }
        }
    }
    let _ = writeln!(out, "## Flags by long name\n");
    let _ = writeln!(
        out,
        "A long name whose short, value, default or possible values differ between tasks is marked **differs**.\n"
    );
    let _ = writeln!(out, "| Flag | Tasks | Shapes |");
    let _ = writeln!(out, "|-|-|-|");
    for (long, uses) in &by_long {
        let mut shapes: BTreeMap<String, Vec<&str>> = BTreeMap::new();
        for r in uses {
            let short = r.short.map(|s| format!("-{s} ")).unwrap_or_default();
            let alias_of = match &r.long {
                Some(canonical) if canonical != long => format!(" alias of --{canonical}"),
                _ => String::new(),
            };
            let shape = format!(
                "{short}{} default={} values={}{alias_of}",
                r.value, r.default, r.possible
            );
            shapes.entry(shape).or_default().push(display_task(&r.task));
        }
        let tasks: Vec<&str> = uses.iter().map(|r| display_task(&r.task)).collect();
        let shape_text = if shapes.len() > 1 {
            let listed: Vec<String> = shapes
                .iter()
                .map(|(shape, on)| format!("`{}` on {}", shape.trim(), on.join(", ")))
                .collect();
            format!("**differs**: {}", listed.join("; "))
        } else {
            shapes
                .keys()
                .next()
                .map(|s| format!("`{}`", s.trim()))
                .unwrap_or_default()
        };
        let _ = writeln!(
            out,
            "| `--{long}` | {} | {} |",
            tasks.join(", "),
            cell(&shape_text)
        );
    }
    out.push('\n');

    // One short letter across tasks: the long names it stands for.
    let mut by_short: BTreeMap<char, BTreeMap<String, Vec<&str>>> = BTreeMap::new();
    for r in &rows {
        if let Some(short) = r.short {
            let long = r
                .long
                .clone()
                .map(|l| format!("--{l}"))
                .unwrap_or_else(|| "(no long name)".to_string());
            by_short
                .entry(short)
                .or_default()
                .entry(long)
                .or_default()
                .push(display_task(&r.task));
        }
    }
    let _ = writeln!(out, "## Short flags\n");
    let _ = writeln!(
        out,
        "A letter that stands for more than one long name is marked **several**.\n"
    );
    let _ = writeln!(out, "| Short | Means |");
    let _ = writeln!(out, "|-|-|");
    for (short, meanings) in &by_short {
        let listed: Vec<String> = meanings
            .iter()
            .map(|(long, on)| format!("`{long}` on {}", on.join(", ")))
            .collect();
        let mark = if meanings.len() > 1 {
            "**several**: "
        } else {
            ""
        };
        let _ = writeln!(out, "| `-{short}` | {mark}{} |", listed.join("; "));
    }
    out.push('\n');

    // Timestamp flags (the `-d, --datetime` rule): every flag whose name,
    // alias or help speaks of a date or a time.
    let _ = writeln!(out, "## Timestamp flags\n");
    let _ = writeln!(
        out,
        "Every flag whose name, alias or help has a word starting with date, time, clock or now.\n"
    );
    let _ = writeln!(out, "| Task | Flag | Aliases | Help |");
    let _ = writeln!(out, "|-|-|-|-|");
    for r in &rows {
        let words = format!(
            "{} {} {}",
            r.long.as_deref().unwrap_or(""),
            r.aliases.join(" "),
            r.help
        )
        .to_lowercase();
        if ["date", "time", "clock", "now"].iter().any(|w| {
            words
                .split(|c: char| !c.is_alphanumeric())
                .any(|t| t.starts_with(w))
        }) {
            let _ = writeln!(
                out,
                "| {} | `{}` | {} | {} |",
                display_task(&r.task),
                spelled(r),
                cell(&r.aliases.join(", ")),
                cell(&r.help)
            );
        }
    }
    out.push('\n');

    let _ = writeln!(out, "## Retired spellings\n");
    let _ = writeln!(
        out,
        "Still accepted, with a note naming the new spelling; removed in 0.14 (`jubarte::cli::RETIRED`).\n"
    );
    let _ = writeln!(out, "| Retired | Say instead | On |");
    let _ = writeln!(out, "|-|-|-|");
    for retired in jubarte::cli::RETIRED {
        let _ = writeln!(
            out,
            "| `{}` | `{}` | {} |",
            retired.old,
            retired.now,
            retired.on.unwrap_or("(a task name)")
        );
    }
    print!("{out}");
}

/// The task path as the tables name it.
fn display_task(task: &str) -> &str {
    if task.is_empty() { "(shorthand)" } else { task }
}