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