1use crate::protocol::ArityRange;
4use anyhow::{bail, Result};
5use std::env;
6use std::ffi::OsString;
7
8const DEFAULT_DATABASE_FILENAME: &str = "db.links";
9
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct Cli {
12 pub db: String,
13 pub query: Option<String>,
14 pub query_arg: Option<String>,
15 pub trace: bool,
16 pub indexed_lookups: bool,
17 pub auto_create_missing_references: bool,
18 pub structure: Option<u32>,
19 pub before: bool,
20 pub changes: bool,
21 pub after: bool,
22 pub lino_input: Option<String>,
23 pub lino_output: Option<String>,
24 pub binary_input: Option<String>,
25 pub binary_output: Option<String>,
26 pub always: bool,
27 pub once: bool,
28 pub never: bool,
29 pub triggers: bool,
30 pub triggers_file: Option<String>,
31 pub embed_triggers: bool,
32 pub transactions: bool,
33 pub transactions_file: Option<String>,
34 pub commit_mode: Option<String>,
35 pub retention: Option<String>,
36 pub vc: bool,
37 pub vc_file: Option<String>,
38 pub branch: Option<String>,
39 pub branch_from: Option<i64>,
40 pub checkout: Option<String>,
41 pub tag: Option<String>,
42 pub list_branches: bool,
43 pub list_tags: bool,
44 pub show_log: bool,
45 pub serve: Option<String>,
46 pub graphql: bool,
48 pub jwt_secret: Option<String>,
50 pub graphql_admin_secret: Option<String>,
52 pub deep_packages: Vec<String>,
54 pub connect: Option<String>,
55 pub protocol: Option<String>,
56 pub external_references: bool,
57 pub arity: ArityRange,
58 pub packed_widths: bool,
59 pub triplets: bool,
61 pub permissions: Option<String>,
63 pub subject: Option<String>,
65 pub tokens: Option<String>,
67 pub grant_token: Option<String>,
68 pub revoke_token: Option<String>,
69 pub token: Option<String>,
71}
72
73impl Default for Cli {
74 fn default() -> Self {
75 Self {
76 db: DEFAULT_DATABASE_FILENAME.to_string(),
77 query: None,
78 query_arg: None,
79 trace: false,
80 indexed_lookups: false,
81 auto_create_missing_references: false,
82 structure: None,
83 before: false,
84 changes: false,
85 after: false,
86 lino_input: None,
87 lino_output: None,
88 binary_input: None,
89 binary_output: None,
90 always: false,
91 once: false,
92 never: false,
93 triggers: false,
94 triggers_file: None,
95 embed_triggers: false,
96 transactions: false,
97 transactions_file: None,
98 commit_mode: None,
99 retention: None,
100 vc: false,
101 vc_file: None,
102 branch: None,
103 branch_from: None,
104 checkout: None,
105 tag: None,
106 list_branches: false,
107 list_tags: false,
108 show_log: false,
109 serve: None,
110 graphql: false,
111 jwt_secret: None,
112 graphql_admin_secret: None,
113 deep_packages: Vec::new(),
114 connect: None,
115 protocol: None,
116 external_references: false,
117 arity: ArityRange::DOUBLETS,
118 packed_widths: false,
119 triplets: false,
120 permissions: None,
121 subject: None,
122 tokens: None,
123 grant_token: None,
124 revoke_token: None,
125 token: None,
126 }
127 }
128}
129
130#[derive(Debug, Clone, PartialEq, Eq)]
131pub enum CliCommand {
132 Run(Box<Cli>),
133 Help,
134 Version,
135}
136
137impl Cli {
138 pub fn transactions_requested(&self) -> bool {
140 self.transactions
141 || self.transactions_file.is_some()
142 || self.commit_mode.is_some()
143 || self.retention.is_some()
144 || self.show_log
145 || self.vc_requested()
146 }
147
148 pub fn trigger_command_count(&self) -> usize {
151 usize::from(self.always) + usize::from(self.once) + usize::from(self.never)
152 }
153
154 pub fn persistent_transformations_requested(&self) -> bool {
160 self.always
161 || self.once
162 || self.never
163 || self.triggers
164 || self.embed_triggers
165 || self.triggers_file.is_some()
166 }
167
168 pub fn vc_requested(&self) -> bool {
170 self.vc
171 || self.vc_file.is_some()
172 || self.branch.is_some()
173 || self.branch_from.is_some()
174 || self.checkout.is_some()
175 || self.tag.is_some()
176 || self.list_branches
177 || self.list_tags
178 }
179
180 pub fn parse() -> Result<CliCommand> {
181 lino_arguments::init();
182 Self::parse_from(env::args_os())
183 }
184
185 pub fn parse_from<I, T>(args: I) -> Result<CliCommand>
186 where
187 I: IntoIterator<Item = T>,
188 T: Into<OsString>,
189 {
190 let mut cli = Cli::default();
191 let mut args = args
192 .into_iter()
193 .map(|arg| arg.into().to_string_lossy().into_owned())
194 .peekable();
195
196 let _program = args.next();
197
198 while let Some(arg) = args.next() {
199 if let Some(value) = inline_value(&arg, &["--db", "--data-source", "--data"]) {
200 cli.db = value.to_string();
201 continue;
202 }
203 if let Some(value) = inline_value(&arg, &["--query", "--apply", "--do"]) {
204 cli.query = Some(value.to_string());
205 continue;
206 }
207 if let Some(value) = inline_value(&arg, &["--structure"]) {
208 cli.structure = Some(parse_link_id("--structure", value)?);
209 continue;
210 }
211 if let Some(value) = inline_value(&arg, &["--indexed-lookups"]) {
212 cli.indexed_lookups = parse_bool("--indexed-lookups", value)?;
213 continue;
214 }
215 if let Some(value) = inline_value(&arg, &["--trace"]) {
216 cli.trace = parse_bool("--trace", value)?;
217 continue;
218 }
219 if let Some(value) = inline_value(&arg, &["--auto-create-missing-references"]) {
220 cli.auto_create_missing_references =
221 parse_bool("--auto-create-missing-references", value)?;
222 continue;
223 }
224 if let Some(value) = inline_value(&arg, &["--before"]) {
225 cli.before = parse_bool("--before", value)?;
226 continue;
227 }
228 if let Some(value) = inline_value(&arg, &["--changes"]) {
229 cli.changes = parse_bool("--changes", value)?;
230 continue;
231 }
232 if let Some(value) = inline_value(&arg, &["--after", "--links"]) {
233 cli.after = parse_bool("--after", value)?;
234 continue;
235 }
236 if let Some(value) = inline_value(&arg, &["--out", "--lino-output", "--export"]) {
237 cli.lino_output = Some(value.to_string());
238 continue;
239 }
240 if let Some(value) = inline_value(&arg, &["--in", "--lino-input", "--import"]) {
241 cli.lino_input = Some(value.to_string());
242 continue;
243 }
244 if let Some(value) = inline_value(
245 &arg,
246 &["--export-binary", "--binary-output", "--binary-out"],
247 ) {
248 cli.binary_output = Some(value.to_string());
249 continue;
250 }
251 if let Some(value) =
252 inline_value(&arg, &["--import-binary", "--binary-input", "--binary-in"])
253 {
254 cli.binary_input = Some(value.to_string());
255 continue;
256 }
257 if let Some(value) = inline_value(&arg, &["--always"]) {
258 cli.always = parse_bool("--always", value)?;
259 continue;
260 }
261 if let Some(value) = inline_value(&arg, &["--once"]) {
262 cli.once = parse_bool("--once", value)?;
263 continue;
264 }
265 if let Some(value) = inline_value(&arg, &["--never"]) {
266 cli.never = parse_bool("--never", value)?;
267 continue;
268 }
269 if let Some(value) = inline_value(&arg, &["--triggers"]) {
270 cli.triggers = parse_bool("--triggers", value)?;
271 continue;
272 }
273 if let Some(value) = inline_value(&arg, &["--triggers-file"]) {
274 cli.triggers_file = Some(value.to_string());
275 continue;
276 }
277 if let Some(value) = inline_value(&arg, &["--embed-triggers"]) {
278 cli.embed_triggers = parse_bool("--embed-triggers", value)?;
279 continue;
280 }
281 if let Some(value) = inline_value(&arg, &["--transactions"]) {
282 cli.transactions = parse_bool("--transactions", value)?;
283 continue;
284 }
285 if let Some(value) = inline_value(&arg, &["--transactions-file"]) {
286 cli.transactions_file = Some(value.to_string());
287 continue;
288 }
289 if let Some(value) = inline_value(&arg, &["--commit-mode"]) {
290 cli.commit_mode = Some(value.to_string());
291 continue;
292 }
293 if let Some(value) = inline_value(&arg, &["--retention"]) {
294 cli.retention = Some(value.to_string());
295 continue;
296 }
297 if let Some(value) = inline_value(&arg, &["--vc"]) {
298 cli.vc = parse_bool("--vc", value)?;
299 continue;
300 }
301 if let Some(value) = inline_value(&arg, &["--vc-file"]) {
302 cli.vc_file = Some(value.to_string());
303 continue;
304 }
305 if let Some(value) = inline_value(&arg, &["--branch"]) {
306 cli.branch = Some(value.to_string());
307 continue;
308 }
309 if let Some(value) = inline_value(&arg, &["--branch-from"]) {
310 cli.branch_from = Some(parse_seq("--branch-from", value)?);
311 continue;
312 }
313 if let Some(value) = inline_value(&arg, &["--checkout"]) {
314 cli.checkout = Some(value.to_string());
315 continue;
316 }
317 if let Some(value) = inline_value(&arg, &["--tag"]) {
318 cli.tag = Some(value.to_string());
319 continue;
320 }
321 if let Some(value) = inline_value(&arg, &["--list-branches"]) {
322 cli.list_branches = parse_bool("--list-branches", value)?;
323 continue;
324 }
325 if let Some(value) = inline_value(&arg, &["--list-tags"]) {
326 cli.list_tags = parse_bool("--list-tags", value)?;
327 continue;
328 }
329 if let Some(value) = inline_value(&arg, &["--log"]) {
330 cli.show_log = parse_bool("--log", value)?;
331 continue;
332 }
333 if let Some(value) = inline_value(&arg, &["--jwt-secret"]) {
334 cli.jwt_secret = Some(value.to_string());
335 continue;
336 }
337 if let Some(value) = inline_value(&arg, &["--graphql-admin-secret"]) {
338 cli.graphql_admin_secret = Some(value.to_string());
339 continue;
340 }
341 if let Some(value) = inline_value(&arg, &["--deep-package"]) {
342 cli.deep_packages.push(value.to_string());
343 continue;
344 }
345 if let Some(value) = inline_value(&arg, &["--serve"]) {
346 cli.serve = Some(value.to_string());
347 continue;
348 }
349 if let Some(value) = inline_value(&arg, &["--connect"]) {
350 cli.connect = Some(value.to_string());
351 continue;
352 }
353 if let Some(value) = inline_value(&arg, &["--protocol"]) {
354 cli.protocol = Some(value.to_string());
355 continue;
356 }
357 if let Some(value) = inline_value(&arg, &["--external-references"]) {
358 cli.external_references = parse_bool("--external-references", value)?;
359 continue;
360 }
361 if let Some(value) = inline_value(&arg, &["--arity"]) {
362 cli.arity = parse_arity(value)?;
363 continue;
364 }
365 if let Some(value) = inline_value(&arg, &["--packed-widths"]) {
366 cli.packed_widths = parse_bool("--packed-widths", value)?;
367 continue;
368 }
369 if let Some(value) = inline_value(&arg, &["--triplets"]) {
370 cli.triplets = parse_bool("--triplets", value)?;
371 continue;
372 }
373 if let Some(value) = inline_value(&arg, &["--permissions"]) {
374 cli.permissions = Some(value.to_string());
375 continue;
376 }
377 if let Some(value) = inline_value(&arg, &["--subject"]) {
378 cli.subject = Some(value.to_string());
379 continue;
380 }
381 if let Some(value) = inline_value(&arg, &["--tokens"]) {
382 cli.tokens = Some(value.to_string());
383 continue;
384 }
385 if let Some(value) = inline_value(&arg, &["--grant-token"]) {
386 cli.grant_token = Some(value.to_string());
387 continue;
388 }
389 if let Some(value) = inline_value(&arg, &["--revoke-token"]) {
390 cli.revoke_token = Some(value.to_string());
391 continue;
392 }
393 if let Some(value) = inline_value(&arg, &["--token"]) {
394 cli.token = Some(value.to_string());
395 continue;
396 }
397
398 match arg.as_str() {
399 "-h" | "--help" => return Ok(CliCommand::Help),
400 "-V" | "--version" => return Ok(CliCommand::Version),
401 "-d" | "--db" | "--data-source" | "--data" => {
402 cli.db = next_value(&mut args, &arg)?;
403 }
404 "-q" | "--query" | "--apply" | "--do" => {
405 cli.query = Some(next_value(&mut args, &arg)?);
406 }
407 "--indexed-lookups" => {
408 cli.indexed_lookups = next_bool_value(&mut args, true)?;
409 }
410 "-t" | "--trace" => {
411 cli.trace = next_bool_value(&mut args, true)?;
412 }
413 "--auto-create-missing-references" => {
414 cli.auto_create_missing_references = next_bool_value(&mut args, true)?;
415 }
416 "-s" | "--structure" => {
417 let value = next_value(&mut args, &arg)?;
418 cli.structure = Some(parse_link_id(&arg, &value)?);
419 }
420 "-b" | "--before" => {
421 cli.before = next_bool_value(&mut args, true)?;
422 }
423 "-c" | "--changes" => {
424 cli.changes = next_bool_value(&mut args, true)?;
425 }
426 "-a" | "--after" | "--links" => {
427 cli.after = next_bool_value(&mut args, true)?;
428 }
429 "--out" | "--lino-output" | "--export" => {
430 cli.lino_output = Some(next_value(&mut args, &arg)?);
431 }
432 "--in" | "--lino-input" | "--import" => {
433 cli.lino_input = Some(next_value(&mut args, &arg)?);
434 }
435 "--export-binary" | "--binary-output" | "--binary-out" => {
436 cli.binary_output = Some(next_value(&mut args, &arg)?);
437 }
438 "--import-binary" | "--binary-input" | "--binary-in" => {
439 cli.binary_input = Some(next_value(&mut args, &arg)?);
440 }
441 "--always" => {
442 cli.always = next_bool_value(&mut args, true)?;
443 }
444 "--once" => {
445 cli.once = next_bool_value(&mut args, true)?;
446 }
447 "--never" => {
448 cli.never = next_bool_value(&mut args, true)?;
449 }
450 "--triggers" => {
451 cli.triggers = next_bool_value(&mut args, true)?;
452 }
453 "--triggers-file" => {
454 cli.triggers_file = Some(next_value(&mut args, &arg)?);
455 }
456 "--embed-triggers" => {
457 cli.embed_triggers = next_bool_value(&mut args, true)?;
458 }
459 "--transactions" => {
460 cli.transactions = next_bool_value(&mut args, true)?;
461 }
462 "--transactions-file" => {
463 cli.transactions_file = Some(next_value(&mut args, &arg)?);
464 }
465 "--commit-mode" => {
466 cli.commit_mode = Some(next_value(&mut args, &arg)?);
467 }
468 "--retention" => {
469 cli.retention = Some(next_value(&mut args, &arg)?);
470 }
471 "--vc" => {
472 cli.vc = next_bool_value(&mut args, true)?;
473 }
474 "--vc-file" => {
475 cli.vc_file = Some(next_value(&mut args, &arg)?);
476 }
477 "--branch" => {
478 cli.branch = Some(next_value(&mut args, &arg)?);
479 }
480 "--branch-from" => {
481 let value = next_value(&mut args, &arg)?;
482 cli.branch_from = Some(parse_seq(&arg, &value)?);
483 }
484 "--checkout" => {
485 cli.checkout = Some(next_value(&mut args, &arg)?);
486 }
487 "--tag" => {
488 cli.tag = Some(next_value(&mut args, &arg)?);
489 }
490 "--list-branches" => {
491 cli.list_branches = next_bool_value(&mut args, true)?;
492 }
493 "--list-tags" => {
494 cli.list_tags = next_bool_value(&mut args, true)?;
495 }
496 "--log" => {
497 cli.show_log = next_bool_value(&mut args, true)?;
498 }
499 "--graphql" => {
500 cli.graphql = next_bool_value(&mut args, true)?;
501 }
502 "--jwt-secret" => {
503 cli.jwt_secret = Some(next_value(&mut args, &arg)?);
504 }
505 "--graphql-admin-secret" => {
506 cli.graphql_admin_secret = Some(next_value(&mut args, &arg)?);
507 }
508 "--deep-package" => {
509 cli.deep_packages.push(next_value(&mut args, &arg)?);
510 }
511 "--serve" => {
512 cli.serve = Some(next_value(&mut args, &arg)?);
513 }
514 "--connect" => {
515 cli.connect = Some(next_value(&mut args, &arg)?);
516 }
517 "--protocol" => {
518 cli.protocol = Some(next_value(&mut args, &arg)?);
519 }
520 "--external-references" => {
521 cli.external_references = next_bool_value(&mut args, true)?;
522 }
523 "--arity" => {
524 cli.arity = parse_arity(&next_value(&mut args, &arg)?)?;
525 }
526 "--packed-widths" => {
527 cli.packed_widths = next_bool_value(&mut args, true)?;
528 }
529 "--triplets" => {
530 cli.triplets = next_bool_value(&mut args, true)?;
531 }
532 "--permissions" => {
533 cli.permissions = Some(next_value(&mut args, &arg)?);
534 }
535 "--subject" => {
536 cli.subject = Some(next_value(&mut args, &arg)?);
537 }
538 "--tokens" => {
539 cli.tokens = Some(next_value(&mut args, &arg)?);
540 }
541 "--grant-token" => {
542 cli.grant_token = Some(next_value(&mut args, &arg)?);
543 }
544 "--revoke-token" => {
545 cli.revoke_token = Some(next_value(&mut args, &arg)?);
546 }
547 "--token" => {
548 cli.token = Some(next_value(&mut args, &arg)?);
549 }
550 "--" => {
551 for value in args.by_ref() {
552 set_positional_query(&mut cli, value)?;
553 }
554 break;
555 }
556 value if value.starts_with('-') => {
557 bail!("unknown option '{value}'");
558 }
559 value => {
560 set_positional_query(&mut cli, value.to_string())?;
561 }
562 }
563 }
564
565 Ok(CliCommand::Run(Box::new(cli)))
566 }
567
568 pub fn print_help() {
569 print!("{}", Self::help_text());
570 }
571
572 pub fn help_text() -> &'static str {
573 concat!(
574 "LiNo CLI Tool for managing links data store\n\n",
575 "Usage: clink [OPTIONS] [QUERY]\n\n",
576 "Arguments:\n",
577 " [QUERY] LiNo query for CRUD operation\n\n",
578 "Options:\n",
579 " -d, --db <DB>, --data-source <DB>, --data <DB>\n",
580 " Path to the links database file [default: db.links]\n",
581 " -q, --query <QUERY>, --apply <QUERY>, --do <QUERY>\n",
582 " LiNo query for CRUD operation\n",
583 " --indexed-lookups\n",
584 " Use optional lookup-index decorators for local links and names (Rust only)\n",
585 " -t, --trace\n",
586 " Enable trace (verbose output)\n",
587 " --auto-create-missing-references\n",
588 " Create missing numeric and named references as self-referential point links\n",
589 " -s, --structure <STRUCTURE>\n",
590 " ID of the link to format its structure\n",
591 " -b, --before\n",
592 " Print the state of the database before applying changes\n",
593 " -c, --changes\n",
594 " Print the changes applied by the query\n",
595 " -a, --after, --links\n",
596 " Print the state of the database after applying changes\n",
597 " --in <IN>, --lino-input <IN>, --import <IN>\n",
598 " Read and import a LiNo file into the database\n",
599 " --out <OUT>, --lino-output <OUT>, --export <OUT>\n",
600 " Write the complete database as a LiNo file\n",
601 " --import-binary <PATH>, --binary-input <PATH>, --binary-in <PATH>\n",
602 " Read and import a store archive in binary links notation\n",
603 " --export-binary <PATH>, --binary-output <PATH>, --binary-out <PATH>\n",
604 " Write the complete database, names included, as a store archive in\n",
605 " binary links notation\n",
606 " --always\n",
607 " Store the query as an always-on persistent transformation trigger\n",
608 " --once\n",
609 " Store the query as a persistent transformation trigger that deletes\n",
610 " itself after it fires\n",
611 " --never\n",
612 " Remove stored persistent transformation triggers matching the query\n",
613 " --triggers\n",
614 " Enable persistent transformation triggers for this command\n",
615 " --triggers-file <FILE>\n",
616 " Path to the persistent transformation trigger links database\n",
617 " (default: <db>.triggers.links)\n",
618 " --embed-triggers\n",
619 " Store persistent transformation triggers directly in the main links\n",
620 " database\n",
621 " --transactions\n",
622 " Enable the transactions layer (default log path: <db>.transitions.links)\n",
623 " --transactions-file <FILE>\n",
624 " Path to the transitions log store (implies --transactions)\n",
625 " --commit-mode <MODE>\n",
626 " Choose 'sync' or 'async' commits (default: sync, implies --transactions)\n",
627 " --retention <SPEC>\n",
628 " Log retention policy: 'infinite', 'sized:<n>', or 'chunked:<n>:<dir>'\n",
629 " (implies --transactions)\n",
630 " --vc\n",
631 " Enable the version-control decorator (implies --transactions)\n",
632 " --vc-file <FILE>\n",
633 " Path to the version-control branches store\n",
634 " (default: <db>.versioncontrol.links)\n",
635 " --branch <NAME>\n",
636 " Switch to a branch (creating it if --branch-from is also passed).\n",
637 " Implies --vc.\n",
638 " --branch-from <SEQ>\n",
639 " When creating a branch with --branch, fork from this sequence point\n",
640 " --checkout <POINT>\n",
641 " Time-travel to a specific transition sequence or named tag.\n",
642 " Implies --vc.\n",
643 " --tag <NAME[=SEQ]>\n",
644 " Create a tag at current head or at the given sequence point.\n",
645 " Implies --vc.\n",
646 " --list-branches\n",
647 " List version-control branches and exit\n",
648 " --list-tags\n",
649 " List version-control tags and exit\n",
650 " --log\n",
651 " Print the transitions log and exit (implies --transactions)\n",
652 " --serve <ADDR>\n",
653 " Serve the database over TCP (e.g. 127.0.0.1:7878) until stopped;\n",
654 " each message may use the text or the binary LiNo protocol\n",
655 " --graphql\n",
656 " With --serve: speak Hasura's GraphQL over HTTP and WebSocket for Deep\n",
657 " --jwt-secret <SECRET>\n",
658 " Signs the tokens of --graphql (default: random for each run)\n",
659 " --graphql-admin-secret <SECRET>\n",
660 " The x-hasura-admin-secret that bypasses --permissions in --graphql\n",
661 " --deep-package <FILE>\n",
662 " Load a Deep package (JSON rows of links) before --graphql serves\n",
663 " --connect <ADDR>\n",
664 " Send the query to a clink server instead of opening a database;\n",
665 " an empty query reads every link\n",
666 " --protocol <PROTOCOL>\n",
667 " 'text' or 'binary' for --connect (default: text); 'text', 'binary'\n",
668 " or 'any' for --serve (default: any)\n",
669 " --external-references\n",
670 " Binary protocol: send numbers and characters as external references\n",
671 " --arity <RANGE>\n",
672 " Binary protocol: link lengths to use, 'n', 'min..max' or 'min..'\n",
673 " (default: 2, doublets only; e.g. 2..3 adds triplets, 1.. any length)\n",
674 " --packed-widths\n",
675 " Binary protocol: give each section of links the narrowest reference\n",
676 " width it needs instead of one width for the whole packet\n",
677 " --triplets\n",
678 " With --serve: read every request as triplets, (create: (type from to)),\n",
679 " (each: (index type from to)) and three-part substitutions, over this\n",
680 " store of doublets; without it, every request is about doublets\n",
681 " --permissions <RULES>\n",
682 " Check every read and write against permission rules stored as links:\n",
683 " 'self' (rules in the data) or a links file of rules; without it,\n",
684 " nothing is checked\n",
685 " --subject <LINK>\n",
686 " The subject (a link index or name) queries run as with --permissions,\n",
687 " and the subject of --serve connections before (auth: token)\n",
688 " (default: 0, a guest)\n",
689 " --tokens <TOKENS>\n",
690 " The token table of (auth: token) for --serve, --grant-token and\n",
691 " --revoke-token: 'self' (tokens in the data) or a links file\n",
692 " --grant-token <TOKEN>\n",
693 " Let a token authenticate as --subject in the --tokens table, then exit\n",
694 " --revoke-token <TOKEN>\n",
695 " Forget a token of the --tokens table, then exit\n",
696 " --token <TOKEN>\n",
697 " With --connect: authenticate with (auth: token) before sending the query\n",
698 " -h, --help\n",
699 " Print help\n",
700 " -V, --version\n",
701 " Print version\n",
702 )
703 }
704
705 pub fn version_text() -> String {
706 format!("clink {}", env!("CARGO_PKG_VERSION"))
707 }
708}
709
710fn parse_arity(value: &str) -> Result<ArityRange> {
711 value
712 .trim()
713 .parse()
714 .map_err(|error| anyhow::anyhow!("invalid value for '--arity': {error}"))
715}
716
717fn inline_value<'a>(arg: &'a str, names: &[&str]) -> Option<&'a str> {
718 names.iter().find_map(|name| {
719 arg.strip_prefix(name)
720 .and_then(|rest| rest.strip_prefix('='))
721 })
722}
723
724fn next_value<I>(args: &mut I, option: &str) -> Result<String>
725where
726 I: Iterator<Item = String>,
727{
728 args.next()
729 .ok_or_else(|| anyhow::anyhow!("missing value for option '{option}'"))
730}
731
732fn next_bool_value<I>(args: &mut std::iter::Peekable<I>, default: bool) -> Result<bool>
733where
734 I: Iterator<Item = String>,
735{
736 if let Some(value) = args.peek().and_then(|value| bool_literal(value)) {
737 args.next();
738 Ok(value)
739 } else {
740 Ok(default)
741 }
742}
743
744fn parse_bool(option: &str, value: &str) -> Result<bool> {
745 bool_literal(value)
746 .ok_or_else(|| anyhow::anyhow!("invalid boolean value '{value}' for {option}"))
747}
748
749fn bool_literal(value: &str) -> Option<bool> {
750 match value.to_ascii_lowercase().as_str() {
751 "true" | "1" | "yes" | "on" => Some(true),
752 "false" | "0" | "no" | "off" => Some(false),
753 _ => None,
754 }
755}
756
757fn parse_link_id(option: &str, value: &str) -> Result<u32> {
758 value
759 .parse()
760 .map_err(|_| anyhow::anyhow!("invalid link id '{value}' for {option}"))
761}
762
763fn parse_seq(option: &str, value: &str) -> Result<i64> {
764 value
765 .parse()
766 .map_err(|_| anyhow::anyhow!("invalid sequence value '{value}' for {option}"))
767}
768
769fn set_positional_query(cli: &mut Cli, value: String) -> Result<()> {
770 if cli.query_arg.is_some() {
771 bail!("unexpected extra positional argument '{value}'");
772 }
773
774 cli.query_arg = Some(value);
775 Ok(())
776}