link-cli 2.1.1

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//! Command-line argument parsing for the `clink` binary.

use crate::protocol::ArityRange;
use anyhow::{bail, Result};
use std::env;
use std::ffi::OsString;

const DEFAULT_DATABASE_FILENAME: &str = "db.links";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cli {
    pub db: String,
    pub query: Option<String>,
    pub query_arg: Option<String>,
    pub trace: bool,
    pub indexed_lookups: bool,
    pub auto_create_missing_references: bool,
    pub structure: Option<u32>,
    pub before: bool,
    pub changes: bool,
    pub after: bool,
    pub lino_input: Option<String>,
    pub lino_output: Option<String>,
    pub binary_input: Option<String>,
    pub binary_output: Option<String>,
    pub always: bool,
    pub once: bool,
    pub never: bool,
    pub triggers: bool,
    pub triggers_file: Option<String>,
    pub embed_triggers: bool,
    pub transactions: bool,
    pub transactions_file: Option<String>,
    pub commit_mode: Option<String>,
    pub retention: Option<String>,
    pub vc: bool,
    pub vc_file: Option<String>,
    pub branch: Option<String>,
    pub branch_from: Option<i64>,
    pub checkout: Option<String>,
    pub tag: Option<String>,
    pub list_branches: bool,
    pub list_tags: bool,
    pub show_log: bool,
    pub serve: Option<String>,
    /// `--graphql`: `--serve` speaks Hasura's GraphQL for Deep (issue #153).
    pub graphql: bool,
    /// `--jwt-secret`: signs the tokens of `--graphql`; random when absent.
    pub jwt_secret: Option<String>,
    /// `--graphql-admin-secret`: what `x-hasura-admin-secret` must be.
    pub graphql_admin_secret: Option<String>,
    /// `--deep-package`: Deep links, as JSON rows, to load before serving.
    pub deep_packages: Vec<String>,
    pub connect: Option<String>,
    pub protocol: Option<String>,
    pub external_references: bool,
    pub arity: ArityRange,
    pub packed_widths: bool,
    /// `--triplets`: `--serve` reads every request as triplets (issue #152).
    pub triplets: bool,
    /// `--permissions`: `self` or a links file of permission rules (issue #149).
    pub permissions: Option<String>,
    /// `--subject`: the link index or name queries run as.
    pub subject: Option<String>,
    /// `--tokens`: `self` or a links file of the token table.
    pub tokens: Option<String>,
    pub grant_token: Option<String>,
    pub revoke_token: Option<String>,
    /// `--token`: authenticates a `--connect` client.
    pub token: Option<String>,
}

impl Default for Cli {
    fn default() -> Self {
        Self {
            db: DEFAULT_DATABASE_FILENAME.to_string(),
            query: None,
            query_arg: None,
            trace: false,
            indexed_lookups: false,
            auto_create_missing_references: false,
            structure: None,
            before: false,
            changes: false,
            after: false,
            lino_input: None,
            lino_output: None,
            binary_input: None,
            binary_output: None,
            always: false,
            once: false,
            never: false,
            triggers: false,
            triggers_file: None,
            embed_triggers: false,
            transactions: false,
            transactions_file: None,
            commit_mode: None,
            retention: None,
            vc: false,
            vc_file: None,
            branch: None,
            branch_from: None,
            checkout: None,
            tag: None,
            list_branches: false,
            list_tags: false,
            show_log: false,
            serve: None,
            graphql: false,
            jwt_secret: None,
            graphql_admin_secret: None,
            deep_packages: Vec::new(),
            connect: None,
            protocol: None,
            external_references: false,
            arity: ArityRange::DOUBLETS,
            packed_widths: false,
            triplets: false,
            permissions: None,
            subject: None,
            tokens: None,
            grant_token: None,
            revoke_token: None,
            token: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CliCommand {
    Run(Box<Cli>),
    Help,
    Version,
}

impl Cli {
    /// True when any flag in the transactions decorator family was passed.
    pub fn transactions_requested(&self) -> bool {
        self.transactions
            || self.transactions_file.is_some()
            || self.commit_mode.is_some()
            || self.retention.is_some()
            || self.show_log
            || self.vc_requested()
    }

    /// True when a trigger command — `--always`, `--once` or `--never` — was
    /// passed. Exactly one of them may be used at a time.
    pub fn trigger_command_count(&self) -> usize {
        usize::from(self.always) + usize::from(self.once) + usize::from(self.never)
    }

    /// True when any flag in the persistent transformation family was passed.
    ///
    /// Mirrors `persistentTransformationsEnabled` in the C# tool, minus the
    /// "the triggers file already exists" clause, which needs the resolved
    /// path and therefore lives next to it in `main`.
    pub fn persistent_transformations_requested(&self) -> bool {
        self.always
            || self.once
            || self.never
            || self.triggers
            || self.embed_triggers
            || self.triggers_file.is_some()
    }

    /// True when any flag in the version-control decorator family was passed.
    pub fn vc_requested(&self) -> bool {
        self.vc
            || self.vc_file.is_some()
            || self.branch.is_some()
            || self.branch_from.is_some()
            || self.checkout.is_some()
            || self.tag.is_some()
            || self.list_branches
            || self.list_tags
    }

    pub fn parse() -> Result<CliCommand> {
        lino_arguments::init();
        Self::parse_from(env::args_os())
    }

    pub fn parse_from<I, T>(args: I) -> Result<CliCommand>
    where
        I: IntoIterator<Item = T>,
        T: Into<OsString>,
    {
        let mut cli = Cli::default();
        let mut args = args
            .into_iter()
            .map(|arg| arg.into().to_string_lossy().into_owned())
            .peekable();

        let _program = args.next();

        while let Some(arg) = args.next() {
            if let Some(value) = inline_value(&arg, &["--db", "--data-source", "--data"]) {
                cli.db = value.to_string();
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--query", "--apply", "--do"]) {
                cli.query = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--structure"]) {
                cli.structure = Some(parse_link_id("--structure", value)?);
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--indexed-lookups"]) {
                cli.indexed_lookups = parse_bool("--indexed-lookups", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--trace"]) {
                cli.trace = parse_bool("--trace", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--auto-create-missing-references"]) {
                cli.auto_create_missing_references =
                    parse_bool("--auto-create-missing-references", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--before"]) {
                cli.before = parse_bool("--before", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--changes"]) {
                cli.changes = parse_bool("--changes", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--after", "--links"]) {
                cli.after = parse_bool("--after", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--out", "--lino-output", "--export"]) {
                cli.lino_output = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--in", "--lino-input", "--import"]) {
                cli.lino_input = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(
                &arg,
                &["--export-binary", "--binary-output", "--binary-out"],
            ) {
                cli.binary_output = Some(value.to_string());
                continue;
            }
            if let Some(value) =
                inline_value(&arg, &["--import-binary", "--binary-input", "--binary-in"])
            {
                cli.binary_input = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--always"]) {
                cli.always = parse_bool("--always", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--once"]) {
                cli.once = parse_bool("--once", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--never"]) {
                cli.never = parse_bool("--never", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--triggers"]) {
                cli.triggers = parse_bool("--triggers", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--triggers-file"]) {
                cli.triggers_file = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--embed-triggers"]) {
                cli.embed_triggers = parse_bool("--embed-triggers", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--transactions"]) {
                cli.transactions = parse_bool("--transactions", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--transactions-file"]) {
                cli.transactions_file = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--commit-mode"]) {
                cli.commit_mode = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--retention"]) {
                cli.retention = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--vc"]) {
                cli.vc = parse_bool("--vc", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--vc-file"]) {
                cli.vc_file = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--branch"]) {
                cli.branch = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--branch-from"]) {
                cli.branch_from = Some(parse_seq("--branch-from", value)?);
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--checkout"]) {
                cli.checkout = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--tag"]) {
                cli.tag = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--list-branches"]) {
                cli.list_branches = parse_bool("--list-branches", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--list-tags"]) {
                cli.list_tags = parse_bool("--list-tags", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--log"]) {
                cli.show_log = parse_bool("--log", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--jwt-secret"]) {
                cli.jwt_secret = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--graphql-admin-secret"]) {
                cli.graphql_admin_secret = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--deep-package"]) {
                cli.deep_packages.push(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--serve"]) {
                cli.serve = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--connect"]) {
                cli.connect = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--protocol"]) {
                cli.protocol = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--external-references"]) {
                cli.external_references = parse_bool("--external-references", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--arity"]) {
                cli.arity = parse_arity(value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--packed-widths"]) {
                cli.packed_widths = parse_bool("--packed-widths", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--triplets"]) {
                cli.triplets = parse_bool("--triplets", value)?;
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--permissions"]) {
                cli.permissions = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--subject"]) {
                cli.subject = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--tokens"]) {
                cli.tokens = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--grant-token"]) {
                cli.grant_token = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--revoke-token"]) {
                cli.revoke_token = Some(value.to_string());
                continue;
            }
            if let Some(value) = inline_value(&arg, &["--token"]) {
                cli.token = Some(value.to_string());
                continue;
            }

            match arg.as_str() {
                "-h" | "--help" => return Ok(CliCommand::Help),
                "-V" | "--version" => return Ok(CliCommand::Version),
                "-d" | "--db" | "--data-source" | "--data" => {
                    cli.db = next_value(&mut args, &arg)?;
                }
                "-q" | "--query" | "--apply" | "--do" => {
                    cli.query = Some(next_value(&mut args, &arg)?);
                }
                "--indexed-lookups" => {
                    cli.indexed_lookups = next_bool_value(&mut args, true)?;
                }
                "-t" | "--trace" => {
                    cli.trace = next_bool_value(&mut args, true)?;
                }
                "--auto-create-missing-references" => {
                    cli.auto_create_missing_references = next_bool_value(&mut args, true)?;
                }
                "-s" | "--structure" => {
                    let value = next_value(&mut args, &arg)?;
                    cli.structure = Some(parse_link_id(&arg, &value)?);
                }
                "-b" | "--before" => {
                    cli.before = next_bool_value(&mut args, true)?;
                }
                "-c" | "--changes" => {
                    cli.changes = next_bool_value(&mut args, true)?;
                }
                "-a" | "--after" | "--links" => {
                    cli.after = next_bool_value(&mut args, true)?;
                }
                "--out" | "--lino-output" | "--export" => {
                    cli.lino_output = Some(next_value(&mut args, &arg)?);
                }
                "--in" | "--lino-input" | "--import" => {
                    cli.lino_input = Some(next_value(&mut args, &arg)?);
                }
                "--export-binary" | "--binary-output" | "--binary-out" => {
                    cli.binary_output = Some(next_value(&mut args, &arg)?);
                }
                "--import-binary" | "--binary-input" | "--binary-in" => {
                    cli.binary_input = Some(next_value(&mut args, &arg)?);
                }
                "--always" => {
                    cli.always = next_bool_value(&mut args, true)?;
                }
                "--once" => {
                    cli.once = next_bool_value(&mut args, true)?;
                }
                "--never" => {
                    cli.never = next_bool_value(&mut args, true)?;
                }
                "--triggers" => {
                    cli.triggers = next_bool_value(&mut args, true)?;
                }
                "--triggers-file" => {
                    cli.triggers_file = Some(next_value(&mut args, &arg)?);
                }
                "--embed-triggers" => {
                    cli.embed_triggers = next_bool_value(&mut args, true)?;
                }
                "--transactions" => {
                    cli.transactions = next_bool_value(&mut args, true)?;
                }
                "--transactions-file" => {
                    cli.transactions_file = Some(next_value(&mut args, &arg)?);
                }
                "--commit-mode" => {
                    cli.commit_mode = Some(next_value(&mut args, &arg)?);
                }
                "--retention" => {
                    cli.retention = Some(next_value(&mut args, &arg)?);
                }
                "--vc" => {
                    cli.vc = next_bool_value(&mut args, true)?;
                }
                "--vc-file" => {
                    cli.vc_file = Some(next_value(&mut args, &arg)?);
                }
                "--branch" => {
                    cli.branch = Some(next_value(&mut args, &arg)?);
                }
                "--branch-from" => {
                    let value = next_value(&mut args, &arg)?;
                    cli.branch_from = Some(parse_seq(&arg, &value)?);
                }
                "--checkout" => {
                    cli.checkout = Some(next_value(&mut args, &arg)?);
                }
                "--tag" => {
                    cli.tag = Some(next_value(&mut args, &arg)?);
                }
                "--list-branches" => {
                    cli.list_branches = next_bool_value(&mut args, true)?;
                }
                "--list-tags" => {
                    cli.list_tags = next_bool_value(&mut args, true)?;
                }
                "--log" => {
                    cli.show_log = next_bool_value(&mut args, true)?;
                }
                "--graphql" => {
                    cli.graphql = next_bool_value(&mut args, true)?;
                }
                "--jwt-secret" => {
                    cli.jwt_secret = Some(next_value(&mut args, &arg)?);
                }
                "--graphql-admin-secret" => {
                    cli.graphql_admin_secret = Some(next_value(&mut args, &arg)?);
                }
                "--deep-package" => {
                    cli.deep_packages.push(next_value(&mut args, &arg)?);
                }
                "--serve" => {
                    cli.serve = Some(next_value(&mut args, &arg)?);
                }
                "--connect" => {
                    cli.connect = Some(next_value(&mut args, &arg)?);
                }
                "--protocol" => {
                    cli.protocol = Some(next_value(&mut args, &arg)?);
                }
                "--external-references" => {
                    cli.external_references = next_bool_value(&mut args, true)?;
                }
                "--arity" => {
                    cli.arity = parse_arity(&next_value(&mut args, &arg)?)?;
                }
                "--packed-widths" => {
                    cli.packed_widths = next_bool_value(&mut args, true)?;
                }
                "--triplets" => {
                    cli.triplets = next_bool_value(&mut args, true)?;
                }
                "--permissions" => {
                    cli.permissions = Some(next_value(&mut args, &arg)?);
                }
                "--subject" => {
                    cli.subject = Some(next_value(&mut args, &arg)?);
                }
                "--tokens" => {
                    cli.tokens = Some(next_value(&mut args, &arg)?);
                }
                "--grant-token" => {
                    cli.grant_token = Some(next_value(&mut args, &arg)?);
                }
                "--revoke-token" => {
                    cli.revoke_token = Some(next_value(&mut args, &arg)?);
                }
                "--token" => {
                    cli.token = Some(next_value(&mut args, &arg)?);
                }
                "--" => {
                    for value in args.by_ref() {
                        set_positional_query(&mut cli, value)?;
                    }
                    break;
                }
                value if value.starts_with('-') => {
                    bail!("unknown option '{value}'");
                }
                value => {
                    set_positional_query(&mut cli, value.to_string())?;
                }
            }
        }

        Ok(CliCommand::Run(Box::new(cli)))
    }

    pub fn print_help() {
        print!("{}", Self::help_text());
    }

    pub fn help_text() -> &'static str {
        concat!(
            "LiNo CLI Tool for managing links data store\n\n",
            "Usage: clink [OPTIONS] [QUERY]\n\n",
            "Arguments:\n",
            "  [QUERY]  LiNo query for CRUD operation\n\n",
            "Options:\n",
            "  -d, --db <DB>, --data-source <DB>, --data <DB>\n",
            "          Path to the links database file [default: db.links]\n",
            "  -q, --query <QUERY>, --apply <QUERY>, --do <QUERY>\n",
            "          LiNo query for CRUD operation\n",
            "      --indexed-lookups\n",
            "          Use optional lookup-index decorators for local links and names (Rust only)\n",
            "  -t, --trace\n",
            "          Enable trace (verbose output)\n",
            "      --auto-create-missing-references\n",
            "          Create missing numeric and named references as self-referential point links\n",
            "  -s, --structure <STRUCTURE>\n",
            "          ID of the link to format its structure\n",
            "  -b, --before\n",
            "          Print the state of the database before applying changes\n",
            "  -c, --changes\n",
            "          Print the changes applied by the query\n",
            "  -a, --after, --links\n",
            "          Print the state of the database after applying changes\n",
            "      --in <IN>, --lino-input <IN>, --import <IN>\n",
            "          Read and import a LiNo file into the database\n",
            "      --out <OUT>, --lino-output <OUT>, --export <OUT>\n",
            "          Write the complete database as a LiNo file\n",
            "      --import-binary <PATH>, --binary-input <PATH>, --binary-in <PATH>\n",
            "          Read and import a store archive in binary links notation\n",
            "      --export-binary <PATH>, --binary-output <PATH>, --binary-out <PATH>\n",
            "          Write the complete database, names included, as a store archive in\n",
            "          binary links notation\n",
            "      --always\n",
            "          Store the query as an always-on persistent transformation trigger\n",
            "      --once\n",
            "          Store the query as a persistent transformation trigger that deletes\n",
            "          itself after it fires\n",
            "      --never\n",
            "          Remove stored persistent transformation triggers matching the query\n",
            "      --triggers\n",
            "          Enable persistent transformation triggers for this command\n",
            "      --triggers-file <FILE>\n",
            "          Path to the persistent transformation trigger links database\n",
            "          (default: <db>.triggers.links)\n",
            "      --embed-triggers\n",
            "          Store persistent transformation triggers directly in the main links\n",
            "          database\n",
            "      --transactions\n",
            "          Enable the transactions layer (default log path: <db>.transitions.links)\n",
            "      --transactions-file <FILE>\n",
            "          Path to the transitions log store (implies --transactions)\n",
            "      --commit-mode <MODE>\n",
            "          Choose 'sync' or 'async' commits (default: sync, implies --transactions)\n",
            "      --retention <SPEC>\n",
            "          Log retention policy: 'infinite', 'sized:<n>', or 'chunked:<n>:<dir>'\n",
            "          (implies --transactions)\n",
            "      --vc\n",
            "          Enable the version-control decorator (implies --transactions)\n",
            "      --vc-file <FILE>\n",
            "          Path to the version-control branches store\n",
            "          (default: <db>.versioncontrol.links)\n",
            "      --branch <NAME>\n",
            "          Switch to a branch (creating it if --branch-from is also passed).\n",
            "          Implies --vc.\n",
            "      --branch-from <SEQ>\n",
            "          When creating a branch with --branch, fork from this sequence point\n",
            "      --checkout <POINT>\n",
            "          Time-travel to a specific transition sequence or named tag.\n",
            "          Implies --vc.\n",
            "      --tag <NAME[=SEQ]>\n",
            "          Create a tag at current head or at the given sequence point.\n",
            "          Implies --vc.\n",
            "      --list-branches\n",
            "          List version-control branches and exit\n",
            "      --list-tags\n",
            "          List version-control tags and exit\n",
            "      --log\n",
            "          Print the transitions log and exit (implies --transactions)\n",
            "      --serve <ADDR>\n",
            "          Serve the database over TCP (e.g. 127.0.0.1:7878) until stopped;\n",
            "          each message may use the text or the binary LiNo protocol\n",
            "      --graphql\n",
            "          With --serve: speak Hasura's GraphQL over HTTP and WebSocket for Deep\n",
            "      --jwt-secret <SECRET>\n",
            "          Signs the tokens of --graphql (default: random for each run)\n",
            "      --graphql-admin-secret <SECRET>\n",
            "          The x-hasura-admin-secret that bypasses --permissions in --graphql\n",
            "      --deep-package <FILE>\n",
            "          Load a Deep package (JSON rows of links) before --graphql serves\n",
            "      --connect <ADDR>\n",
            "          Send the query to a clink server instead of opening a database;\n",
            "          an empty query reads every link\n",
            "      --protocol <PROTOCOL>\n",
            "          'text' or 'binary' for --connect (default: text); 'text', 'binary'\n",
            "          or 'any' for --serve (default: any)\n",
            "      --external-references\n",
            "          Binary protocol: send numbers and characters as external references\n",
            "      --arity <RANGE>\n",
            "          Binary protocol: link lengths to use, 'n', 'min..max' or 'min..'\n",
            "          (default: 2, doublets only; e.g. 2..3 adds triplets, 1.. any length)\n",
            "      --packed-widths\n",
            "          Binary protocol: give each section of links the narrowest reference\n",
            "          width it needs instead of one width for the whole packet\n",
            "      --triplets\n",
            "          With --serve: read every request as triplets, (create: (type from to)),\n",
            "          (each: (index type from to)) and three-part substitutions, over this\n",
            "          store of doublets; without it, every request is about doublets\n",
            "      --permissions <RULES>\n",
            "          Check every read and write against permission rules stored as links:\n",
            "          'self' (rules in the data) or a links file of rules; without it,\n",
            "          nothing is checked\n",
            "      --subject <LINK>\n",
            "          The subject (a link index or name) queries run as with --permissions,\n",
            "          and the subject of --serve connections before (auth: token)\n",
            "          (default: 0, a guest)\n",
            "      --tokens <TOKENS>\n",
            "          The token table of (auth: token) for --serve, --grant-token and\n",
            "          --revoke-token: 'self' (tokens in the data) or a links file\n",
            "      --grant-token <TOKEN>\n",
            "          Let a token authenticate as --subject in the --tokens table, then exit\n",
            "      --revoke-token <TOKEN>\n",
            "          Forget a token of the --tokens table, then exit\n",
            "      --token <TOKEN>\n",
            "          With --connect: authenticate with (auth: token) before sending the query\n",
            "  -h, --help\n",
            "          Print help\n",
            "  -V, --version\n",
            "          Print version\n",
        )
    }

    pub fn version_text() -> String {
        format!("clink {}", env!("CARGO_PKG_VERSION"))
    }
}

fn parse_arity(value: &str) -> Result<ArityRange> {
    value
        .trim()
        .parse()
        .map_err(|error| anyhow::anyhow!("invalid value for '--arity': {error}"))
}

fn inline_value<'a>(arg: &'a str, names: &[&str]) -> Option<&'a str> {
    names.iter().find_map(|name| {
        arg.strip_prefix(name)
            .and_then(|rest| rest.strip_prefix('='))
    })
}

fn next_value<I>(args: &mut I, option: &str) -> Result<String>
where
    I: Iterator<Item = String>,
{
    args.next()
        .ok_or_else(|| anyhow::anyhow!("missing value for option '{option}'"))
}

fn next_bool_value<I>(args: &mut std::iter::Peekable<I>, default: bool) -> Result<bool>
where
    I: Iterator<Item = String>,
{
    if let Some(value) = args.peek().and_then(|value| bool_literal(value)) {
        args.next();
        Ok(value)
    } else {
        Ok(default)
    }
}

fn parse_bool(option: &str, value: &str) -> Result<bool> {
    bool_literal(value)
        .ok_or_else(|| anyhow::anyhow!("invalid boolean value '{value}' for {option}"))
}

fn bool_literal(value: &str) -> Option<bool> {
    match value.to_ascii_lowercase().as_str() {
        "true" | "1" | "yes" | "on" => Some(true),
        "false" | "0" | "no" | "off" => Some(false),
        _ => None,
    }
}

fn parse_link_id(option: &str, value: &str) -> Result<u32> {
    value
        .parse()
        .map_err(|_| anyhow::anyhow!("invalid link id '{value}' for {option}"))
}

fn parse_seq(option: &str, value: &str) -> Result<i64> {
    value
        .parse()
        .map_err(|_| anyhow::anyhow!("invalid sequence value '{value}' for {option}"))
}

fn set_positional_query(cli: &mut Cli, value: String) -> Result<()> {
    if cli.query_arg.is_some() {
        bail!("unexpected extra positional argument '{value}'");
    }

    cli.query_arg = Some(value);
    Ok(())
}