1use crate::parse::{Token, WordSet};
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
7pub enum UnknownTolerance {
8 #[default]
10 Strict,
11 Short,
15 Long,
21 Both,
24}
25
26impl UnknownTolerance {
27 pub const fn allows_short(self) -> bool {
28 matches!(self, Self::Short | Self::Both)
29 }
30 pub const fn allows_long(self) -> bool {
31 matches!(self, Self::Long | Self::Both)
32 }
33}
34
35#[derive(Clone, Copy, Debug, Default)]
41pub struct FlagTolerance {
42 pub unknown: UnknownTolerance,
43 pub numeric_dash: bool,
44 pub single_dash_words: bool,
45}
46
47impl FlagTolerance {
48 pub const fn strict() -> Self {
51 Self { unknown: UnknownTolerance::Strict, numeric_dash: false, single_dash_words: false }
52 }
53}
54
55#[derive(Clone, Copy, Debug, PartialEq, Eq)]
59pub enum PositionalShape {
60 Path,
63 GoPackage,
70}
71
72impl PositionalShape {
73 pub fn matches(self, token: &str) -> bool {
74 match self {
75 Self::Path => looks_like_path(token),
76 Self::GoPackage => is_go_local_package(token),
77 }
78 }
79
80 pub fn from_name(name: &str) -> Option<Self> {
81 match name {
82 "path" => Some(Self::Path),
83 "go-package" => Some(Self::GoPackage),
84 _ => None,
85 }
86 }
87}
88
89pub fn is_go_local_package(token: &str) -> bool {
92 token == "." || token == ".." || token.starts_with("./") || token.starts_with("../") || token.starts_with('/') || token.ends_with(".go")
93}
94
95pub fn looks_like_path(token: &str) -> bool {
109 if token.is_empty() {
110 return false;
111 }
112 if token.starts_with('-') {
113 return token == "-";
114 }
115 token.starts_with('~') || token.contains('/') || token.contains('.')
116}
117
118pub trait FlagSet {
119 fn contains_flag(&self, token: &str) -> bool;
120 fn contains_short(&self, byte: u8) -> bool;
121}
122
123impl FlagSet for WordSet {
124 fn contains_flag(&self, token: &str) -> bool {
125 self.contains(token)
126 }
127 fn contains_short(&self, byte: u8) -> bool {
128 self.contains_short(byte)
129 }
130}
131
132impl FlagSet for [String] {
133 fn contains_flag(&self, token: &str) -> bool {
134 self.iter().any(|f| f.as_str() == token)
135 }
136 fn contains_short(&self, byte: u8) -> bool {
137 self.iter().any(|f| f.len() == 2 && f.as_bytes()[1] == byte)
138 }
139}
140
141impl FlagSet for Vec<String> {
142 fn contains_flag(&self, token: &str) -> bool {
143 self.as_slice().contains_flag(token)
144 }
145 fn contains_short(&self, byte: u8) -> bool {
146 self.as_slice().contains_short(byte)
147 }
148}
149
150pub struct FlagPolicy {
151 pub standalone: WordSet,
152 pub valued: WordSet,
153 pub bare: bool,
154 pub max_positional: Option<usize>,
155 pub tolerance: FlagTolerance,
156}
157
158impl FlagPolicy {
159 pub fn describe(&self) -> String {
160 use crate::docs::wordset_items;
161 let mut lines = Vec::new();
162 let standalone = wordset_items(&self.standalone);
163 if !standalone.is_empty() {
164 lines.push(format!("- Allowed standalone flags: {standalone}"));
165 }
166 let valued = wordset_items(&self.valued);
167 if !valued.is_empty() {
168 lines.push(format!("- Allowed valued flags: {valued}"));
169 }
170 if self.bare {
171 lines.push("- Bare invocation allowed".to_string());
172 }
173 if self.tolerance.unknown != UnknownTolerance::Strict {
174 lines.push("- Hyphen-prefixed positional arguments accepted".to_string());
175 }
176 if self.tolerance.numeric_dash {
177 lines.push("- Numeric shorthand accepted (e.g. -20 for -n 20)".to_string());
178 }
179 if lines.is_empty() && !self.bare {
180 return "- Positional arguments only".to_string();
181 }
182 lines.join("\n")
183 }
184}
185
186pub fn check(tokens: &[Token], policy: &FlagPolicy) -> bool {
187 check_flags(tokens, &policy.standalone, &policy.valued, policy.bare, policy.max_positional, policy.tolerance)
188}
189
190pub(crate) fn consumes_next_value(next: Option<&Token>) -> bool {
191 match next {
192 None => false,
193 Some(t) if crate::cst::opaque::is_flag_probe(t.as_str()) => true,
194 Some(t) => {
195 let b = t.as_bytes();
196 !(b.len() > 1 && b[0] == b'-' && !b[1].is_ascii_digit())
203 }
204 }
205}
206
207pub fn check_flags<S: FlagSet + ?Sized, V: FlagSet + ?Sized>(
208 tokens: &[Token],
209 standalone: &S,
210 valued: &V,
211 bare: bool,
212 max_positional: Option<usize>,
213 tolerance: FlagTolerance,
214) -> bool {
215 if tokens.len() == 1 {
216 return bare;
217 }
218 count_positionals(tokens, standalone, valued, tolerance).is_some_and(|n| max_positional.is_none_or(|max| n <= max))
219}
220
221pub fn count_positionals<S: FlagSet + ?Sized, V: FlagSet + ?Sized>(
223 tokens: &[Token],
224 standalone: &S,
225 valued: &V,
226 tolerance: FlagTolerance,
227) -> Option<usize> {
228 let mut i = 1;
229 let mut positionals: usize = 0;
230 while i < tokens.len() {
231 let t = &tokens[i];
232
233 if *t == "--" {
234 positionals += tokens.len() - i - 1;
235 break;
236 }
237
238 if !t.starts_with('-') {
239 positionals += 1;
240 i += 1;
241 continue;
242 }
243
244 if tolerance.numeric_dash && t.len() > 1 && t[1..].bytes().all(|b| b.is_ascii_digit()) {
245 i += 1;
246 continue;
247 }
248
249 if standalone.contains_flag(t) {
250 i += 1;
251 continue;
252 }
253
254 if valued.contains_flag(t) {
255 if consumes_next_value(tokens.get(i + 1)) {
256 i += 2;
257 } else {
258 i += 1;
259 }
260 continue;
261 }
262
263 if let Some(flag) = t.as_str().split_once('=').map(|(f, _)| f) {
264 if valued.contains_flag(flag) {
265 i += 1;
266 continue;
267 }
268 if tolerance.unknown.allows_long() {
270 positionals += 1;
271 i += 1;
272 continue;
273 }
274 return None;
275 }
276
277 if t.starts_with("--") {
278 if tolerance.unknown.allows_long() {
279 positionals += 1;
280 i += 1;
281 continue;
282 }
283 return None;
284 }
285
286 if tolerance.single_dash_words && t.len() > 1 {
287 if tolerance.unknown.allows_short() {
288 positionals += 1;
289 i += 1;
290 continue;
291 }
292 return None;
293 }
294
295 let bytes = t.as_bytes();
296 let mut j = 1;
297 while j < bytes.len() {
298 let b = bytes[j];
299 let is_last = j == bytes.len() - 1;
300 if standalone.contains_short(b) {
301 j += 1;
302 continue;
303 }
304 if valued.contains_short(b) {
305 if is_last && consumes_next_value(tokens.get(i + 1)) {
306 i += 1;
307 }
308 break;
309 }
310 if tolerance.unknown.allows_short() {
311 positionals += 1;
312 break;
313 }
314 return None;
315 }
316 i += 1;
317 }
318 Some(positionals)
319}
320
321#[cfg(test)]
322mod tests {
323 use super::*;
324
325 static TEST_POLICY: FlagPolicy = FlagPolicy {
326 standalone: WordSet::flags(&[
327 "--color", "--count", "--help", "--recursive", "--version", "-H", "-c", "-i", "-l", "-n", "-o", "-r", "-s", "-v", "-w",
328 ]),
329 valued: WordSet::flags(&["--after-context", "--before-context", "--max-count", "-A", "-B", "-m"]),
330 bare: false,
331 max_positional: None,
332 tolerance: FlagTolerance::strict(),
333 };
334
335 fn toks(words: &[&str]) -> Vec<Token> {
336 words.iter().map(|s| Token::from_test(s)).collect()
337 }
338
339 #[test]
340 fn bare_denied_when_bare_false() {
341 assert!(!check(&toks(&["grep"]), &TEST_POLICY));
342 }
343
344 #[test]
345 fn bare_allowed_when_bare_true() {
346 let policy = FlagPolicy {
347 standalone: WordSet::flags(&[]),
348 valued: WordSet::flags(&[]),
349 bare: true,
350 max_positional: None,
351 tolerance: FlagTolerance::strict(),
352 };
353 assert!(check(&toks(&["uname"]), &policy));
354 }
355
356 #[test]
369 fn a_flag_declared_in_both_lists_already_takes_an_optional_value() {
370 static BOTH: FlagPolicy = FlagPolicy {
371 standalone: WordSet::flags(&["--long", "-r"]),
372 valued: WordSet::flags(&["--long", "-r"]),
373 bare: true,
374 max_positional: Some(0),
375 tolerance: FlagTolerance::strict(),
376 };
377 assert!(check(&toks(&["zstd", "--long"]), &BOTH), "bare long form");
378 assert!(check(&toks(&["zstd", "--long=27"]), &BOTH), "glued long form");
379 assert!(check(&toks(&["zstd", "-r"]), &BOTH), "bare short form");
380
381 assert!(!check(&toks(&["zstd", "--long", "somefile"]), &BOTH), "the bare form must not consume the following token as its value");
384
385 assert!(!check(&toks(&["7z", "-r0"]), &BOTH), "short-glued is NOT handled today");
387 }
388
389 #[test]
390 fn standalone_long_flag() {
391 assert!(check(&toks(&["grep", "--recursive", "pattern", "."]), &TEST_POLICY));
392 }
393
394 #[test]
395 fn standalone_short_flag() {
396 assert!(check(&toks(&["grep", "-r", "pattern", "."]), &TEST_POLICY));
397 }
398
399 #[test]
400 fn valued_long_flag_space() {
401 assert!(check(&toks(&["grep", "--max-count", "5", "pattern"]), &TEST_POLICY));
402 }
403
404 #[test]
405 fn valued_long_flag_eq() {
406 assert!(check(&toks(&["grep", "--max-count=5", "pattern"]), &TEST_POLICY));
407 }
408
409 #[test]
410 fn valued_short_flag_space() {
411 assert!(check(&toks(&["grep", "-m", "5", "pattern"]), &TEST_POLICY));
412 }
413
414 #[test]
415 fn combined_standalone_short() {
416 assert!(check(&toks(&["grep", "-rn", "pattern", "."]), &TEST_POLICY));
417 }
418
419 #[test]
420 fn combined_short_with_valued_last() {
421 assert!(check(&toks(&["grep", "-rnm", "5", "pattern"]), &TEST_POLICY));
422 }
423
424 #[test]
425 fn combined_short_valued_mid_consumes_rest() {
426 assert!(check(&toks(&["grep", "-rmn", "pattern"]), &TEST_POLICY));
427 }
428
429 #[test]
430 fn unknown_long_flag_denied() {
431 assert!(!check(&toks(&["grep", "--exec", "cmd"]), &TEST_POLICY));
432 }
433
434 #[test]
435 fn unknown_short_flag_denied() {
436 assert!(!check(&toks(&["grep", "-z", "pattern"]), &TEST_POLICY));
437 }
438
439 #[test]
440 fn unknown_combined_short_denied() {
441 assert!(!check(&toks(&["grep", "-rz", "pattern"]), &TEST_POLICY));
442 }
443
444 #[test]
445 fn unknown_long_eq_denied() {
446 assert!(!check(&toks(&["grep", "--output=file.txt", "pattern"]), &TEST_POLICY));
447 }
448
449 #[test]
450 fn double_dash_stops_checking() {
451 assert!(check(&toks(&["grep", "--", "--not-a-flag", "file"]), &TEST_POLICY));
452 }
453
454 #[test]
455 fn positional_args_allowed() {
456 assert!(check(&toks(&["grep", "pattern", "file.txt", "other.txt"]), &TEST_POLICY));
457 }
458
459 #[test]
460 fn mixed_flags_and_positional() {
461 assert!(check(&toks(&["grep", "-rn", "--color", "--max-count", "10", "pattern", "."]), &TEST_POLICY,));
462 }
463
464 #[test]
465 fn valued_short_in_explicit_form() {
466 assert!(check(&toks(&["grep", "-A", "3", "-B", "3", "pattern"]), &TEST_POLICY));
467 }
468
469 #[test]
470 fn bare_dash_allowed_as_stdin() {
471 assert!(check(&toks(&["grep", "pattern", "-"]), &TEST_POLICY));
472 }
473
474 #[test]
475 fn valued_flag_at_end_without_value() {
476 assert!(check(&toks(&["grep", "--max-count"]), &TEST_POLICY));
477 }
478
479 #[test]
480 fn single_short_in_wordset_and_byte_array() {
481 assert!(check(&toks(&["grep", "-c", "pattern"]), &TEST_POLICY));
482 }
483
484 static SYNTAX_CHECK_POLICY: FlagPolicy = FlagPolicy {
485 standalone: WordSet::flags(&["--help", "-h"]),
486 valued: WordSet::flags(&["--check", "-c"]),
487 bare: false,
488 max_positional: Some(0),
489 tolerance: FlagTolerance::strict(),
490 };
491
492 #[test]
493 fn valued_flag_consumes_path_value() {
494 assert!(check(&toks(&["node", "--check", "app.js"]), &SYNTAX_CHECK_POLICY));
495 assert!(check(&toks(&["node", "-c", "app.js"]), &SYNTAX_CHECK_POLICY));
496 }
497
498 #[test]
499 fn valued_flag_does_not_swallow_following_long_option() {
500 assert!(!check(&toks(&["node", "--check", "--require=./evil.js"]), &SYNTAX_CHECK_POLICY,));
501 }
502
503 #[test]
504 fn valued_short_does_not_swallow_following_option() {
505 assert!(!check(&toks(&["node", "-c", "-r./evil.js"]), &SYNTAX_CHECK_POLICY));
506 }
507
508 #[test]
509 fn valued_flag_still_consumes_negative_number() {
510 let policy = FlagPolicy {
511 standalone: WordSet::flags(&[]),
512 valued: WordSet::flags(&["-n"]),
513 bare: false,
514 max_positional: Some(1),
515 tolerance: FlagTolerance::strict(),
516 };
517 assert!(check(&toks(&["head", "-n", "-5", "file"]), &policy));
518 }
519
520 static LIMITED_POLICY: FlagPolicy = FlagPolicy {
521 standalone: WordSet::flags(&["--count", "-c", "-d", "-i", "-u"]),
522 valued: WordSet::flags(&["--skip-fields", "-f", "-s"]),
523 bare: true,
524 max_positional: Some(1),
525 tolerance: FlagTolerance::strict(),
526 };
527
528 #[test]
529 fn max_positional_within_limit() {
530 assert!(check(&toks(&["uniq", "input.txt"]), &LIMITED_POLICY));
531 }
532
533 #[test]
534 fn max_positional_exceeded() {
535 assert!(!check(&toks(&["uniq", "input.txt", "output.txt"]), &LIMITED_POLICY));
536 }
537
538 #[test]
539 fn max_positional_with_flags_within_limit() {
540 assert!(check(&toks(&["uniq", "-c", "-f", "3", "input.txt"]), &LIMITED_POLICY));
541 }
542
543 #[test]
544 fn max_positional_with_flags_exceeded() {
545 assert!(!check(&toks(&["uniq", "-c", "input.txt", "output.txt"]), &LIMITED_POLICY));
546 }
547
548 #[test]
549 fn max_positional_after_double_dash() {
550 assert!(!check(&toks(&["uniq", "--", "input.txt", "output.txt"]), &LIMITED_POLICY));
551 }
552
553 #[test]
554 fn max_positional_bare_allowed() {
555 assert!(check(&toks(&["uniq"]), &LIMITED_POLICY));
556 }
557
558 static BOTH_TOLERANCES_POLICY: FlagPolicy = FlagPolicy {
559 standalone: WordSet::flags(&["-E", "-e", "-n"]),
560 valued: WordSet::flags(&[]),
561 bare: true,
562 max_positional: None,
563 tolerance: FlagTolerance { unknown: UnknownTolerance::Both, numeric_dash: false, single_dash_words: false },
564 };
565
566 #[test]
567 fn both_tolerances_accept_unknown_long() {
568 assert!(check(&toks(&["echo", "--unknown", "hello"]), &BOTH_TOLERANCES_POLICY));
569 }
570
571 #[test]
572 fn both_tolerances_accept_unknown_short() {
573 assert!(check(&toks(&["echo", "-x", "hello"]), &BOTH_TOLERANCES_POLICY));
574 }
575
576 #[test]
577 fn both_tolerances_accept_triple_dash() {
578 assert!(check(&toks(&["echo", "---"]), &BOTH_TOLERANCES_POLICY));
579 }
580
581 #[test]
582 fn both_tolerances_known_flags_still_work() {
583 assert!(check(&toks(&["echo", "-n", "hello"]), &BOTH_TOLERANCES_POLICY));
584 }
585
586 #[test]
587 fn both_tolerances_combo_known_short() {
588 assert!(check(&toks(&["echo", "-ne", "hello"]), &BOTH_TOLERANCES_POLICY));
589 }
590
591 #[test]
592 fn both_tolerances_combo_unknown_short_byte() {
593 assert!(check(&toks(&["echo", "-nx", "hello"]), &BOTH_TOLERANCES_POLICY));
594 }
595
596 #[test]
597 fn both_tolerances_unknown_eq_form() {
598 assert!(check(&toks(&["echo", "--foo=bar"]), &BOTH_TOLERANCES_POLICY));
599 }
600
601 static SHORT_ONLY_POLICY: FlagPolicy = FlagPolicy {
608 standalone: WordSet::flags(&["--help"]),
609 valued: WordSet::flags(&[]),
610 bare: false,
611 max_positional: None,
612 tolerance: FlagTolerance { unknown: UnknownTolerance::Short, numeric_dash: false, single_dash_words: false },
613 };
614
615 #[test]
616 fn short_only_accepts_unknown_dash_letter() {
617 assert!(check(&toks(&["sample", "-mayDie"]), &SHORT_ONLY_POLICY));
618 }
619
620 #[test]
621 fn short_only_accepts_single_dash_long_word() {
622 assert!(check(&toks(&["pdftotext", "-layout"]), &SHORT_ONLY_POLICY));
624 }
625
626 #[test]
627 fn short_only_denies_unknown_double_dash() {
628 assert!(!check(&toks(&["sample", "--evil-flag"]), &SHORT_ONLY_POLICY));
631 }
632
633 #[test]
634 fn short_only_denies_unknown_eq_form() {
635 assert!(!check(&toks(&["sample", "--evil=value"]), &SHORT_ONLY_POLICY));
636 }
637
638 #[test]
639 fn short_only_known_long_flag_still_works() {
640 assert!(check(&toks(&["sample", "--help"]), &SHORT_ONLY_POLICY));
641 }
642
643 static LONG_ONLY_POLICY: FlagPolicy = FlagPolicy {
649 standalone: WordSet::flags(&["--help"]),
650 valued: WordSet::flags(&[]),
651 bare: false,
652 max_positional: None,
653 tolerance: FlagTolerance { unknown: UnknownTolerance::Long, numeric_dash: false, single_dash_words: false },
654 };
655
656 #[test]
657 fn long_only_accepts_unknown_double_dash() {
658 assert!(check(&toks(&["aws", "--some-aws-flag"]), &LONG_ONLY_POLICY));
659 }
660
661 #[test]
662 fn long_only_accepts_unknown_eq_form() {
663 assert!(check(&toks(&["aws", "--filter=Name=tag,Values=foo"]), &LONG_ONLY_POLICY,));
664 }
665
666 #[test]
667 fn long_only_denies_unknown_short_dash() {
668 assert!(!check(&toks(&["aws", "-x"]), &LONG_ONLY_POLICY));
669 }
670
671 static STRICT_POLICY: FlagPolicy = FlagPolicy {
674 standalone: WordSet::flags(&["--help"]),
675 valued: WordSet::flags(&[]),
676 bare: false,
677 max_positional: None,
678 tolerance: FlagTolerance::strict(),
679 };
680
681 #[test]
682 fn strict_denies_unknown_short() {
683 assert!(!check(&toks(&["foo", "-evil"]), &STRICT_POLICY));
684 }
685
686 #[test]
687 fn strict_denies_unknown_long() {
688 assert!(!check(&toks(&["foo", "--evil"]), &STRICT_POLICY));
689 }
690
691 #[test]
692 fn strict_known_flag_passes() {
693 assert!(check(&toks(&["foo", "--help"]), &STRICT_POLICY));
694 }
695
696 #[test]
697 fn both_tolerances_with_max_positional() {
698 let policy = FlagPolicy {
699 standalone: WordSet::flags(&["-n"]),
700 valued: WordSet::flags(&[]),
701 bare: true,
702 max_positional: Some(2),
703 tolerance: FlagTolerance { unknown: UnknownTolerance::Both, numeric_dash: false, single_dash_words: false },
704 };
705 assert!(check(&toks(&["echo", "--unknown", "hello"]), &policy));
706 assert!(!check(&toks(&["echo", "--a", "--b", "--c"]), &policy));
707 }
708
709 static NUMERIC_DASH_POLICY: FlagPolicy = FlagPolicy {
710 standalone: WordSet::flags(&["--help", "--quiet", "--verbose", "--version", "-V", "-h", "-q", "-v", "-z"]),
711 valued: WordSet::flags(&["--bytes", "--lines", "-c", "-n"]),
712 bare: true,
713 max_positional: None,
714 tolerance: FlagTolerance { numeric_dash: true, ..FlagTolerance::strict() },
715 };
716
717 #[test]
718 fn numeric_dash_single_digit() {
719 assert!(check(&toks(&["head", "-5"]), &NUMERIC_DASH_POLICY));
720 }
721
722 #[test]
723 fn numeric_dash_multi_digit() {
724 assert!(check(&toks(&["head", "-20"]), &NUMERIC_DASH_POLICY));
725 }
726
727 #[test]
728 fn numeric_dash_large_number() {
729 assert!(check(&toks(&["head", "-1000"]), &NUMERIC_DASH_POLICY));
730 }
731
732 #[test]
733 fn numeric_dash_with_file_arg() {
734 assert!(check(&toks(&["head", "-20", "file.txt"]), &NUMERIC_DASH_POLICY));
735 }
736
737 #[test]
738 fn numeric_dash_with_other_flags() {
739 assert!(check(&toks(&["head", "-q", "-20", "file.txt"]), &NUMERIC_DASH_POLICY));
740 }
741
742 #[test]
743 fn numeric_dash_zero() {
744 assert!(check(&toks(&["head", "-0"]), &NUMERIC_DASH_POLICY));
745 }
746
747 #[test]
748 fn numeric_dash_still_rejects_unknown_flags() {
749 assert!(!check(&toks(&["head", "-x"]), &NUMERIC_DASH_POLICY));
750 }
751
752 #[test]
753 fn numeric_dash_rejects_mixed_alpha_num() {
754 assert!(!check(&toks(&["head", "-20x"]), &NUMERIC_DASH_POLICY));
755 }
756
757 #[test]
758 fn numeric_dash_disabled_rejects_multi_digit() {
759 assert!(!check(&toks(&["grep", "-20", "pattern"]), &TEST_POLICY));
760 }
761
762 static WORDS_POLICY: FlagPolicy = FlagPolicy {
763 standalone: WordSet::flags(&["-a", "-f", "-i", "-ifd0"]),
764 valued: WordSet::flags(&["-ext"]),
765 bare: false,
766 max_positional: None,
767 tolerance: FlagTolerance { single_dash_words: true, ..FlagTolerance::strict() },
768 };
769
770 #[test]
771 fn single_dash_words_never_split_a_word_into_letters() {
772 assert!(!check(&toks(&["exiftool", "-if", "system(1)", "a.jpg"]), &WORDS_POLICY));
773 assert!(!check(&toks(&["exiftool", "-fa", "a.jpg"]), &WORDS_POLICY));
774 assert!(check(&toks(&["exiftool", "-fa", "a.jpg"]), &FlagPolicy { tolerance: FlagTolerance::strict(), ..WORDS_POLICY }));
775 }
776
777 #[test]
778 fn single_dash_words_accept_listed_words_values_and_stdin() {
779 assert!(check(&toks(&["exiftool", "-ifd0", "-a", "-ext", "jpg", "a.jpg"]), &WORDS_POLICY));
780 assert!(check(&toks(&["exiftool", "-i", "-f", "-"]), &WORDS_POLICY));
781 }
782
783 #[test]
784 fn single_dash_words_under_short_tolerance_count_unknowns_as_positionals() {
785 let policy = FlagPolicy {
786 max_positional: Some(1),
787 tolerance: FlagTolerance { unknown: UnknownTolerance::Short, single_dash_words: true, numeric_dash: false },
788 ..WORDS_POLICY
789 };
790 assert!(check(&toks(&["exiftool", "-if"]), &policy));
791 assert!(!check(&toks(&["exiftool", "-if", "-zz"]), &policy));
792 }
793
794 #[test]
795 fn looks_like_path_accepts_relative() {
796 assert!(looks_like_path("./Tiltfile"));
797 assert!(looks_like_path("path/to/file"));
798 }
799
800 #[test]
801 fn looks_like_path_accepts_dotted() {
802 assert!(looks_like_path("Tiltfile.dev"));
803 assert!(looks_like_path("file.rb"));
804 }
805
806 #[test]
807 fn looks_like_path_accepts_stdin_dash() {
808 assert!(looks_like_path("-"));
809 }
810
811 #[test]
812 fn looks_like_path_rejects_flag() {
813 assert!(!looks_like_path("--help"));
814 assert!(!looks_like_path("-x"));
815 }
816
817 #[test]
818 fn looks_like_path_rejects_bare_word() {
819 assert!(!looks_like_path("Tiltfile"));
820 assert!(!looks_like_path("up"));
821 }
822
823 #[test]
824 fn looks_like_path_rejects_empty() {
825 assert!(!looks_like_path(""));
826 }
827
828 #[test]
829 fn positional_shape_path_matches() {
830 assert!(PositionalShape::Path.matches("./file.rb"));
831 assert!(!PositionalShape::Path.matches("--flag"));
832 }
833
834 #[test]
835 fn positional_shape_from_name() {
836 assert_eq!(PositionalShape::from_name("path"), Some(PositionalShape::Path));
837 assert_eq!(PositionalShape::from_name("nope"), None);
838 }
839
840 #[test]
841 fn a_vec_of_flags_answers_short_lookups_as_its_slice_does() {
842 let lists: [&[&str]; 3] = [&[], &["-x", "--long", "-y"], &["--x", "-", "-xy"]];
843 for list in lists {
844 let flags: Vec<String> = list.iter().map(|s| (*s).to_string()).collect();
845 for byte in 0..=u8::MAX {
846 let expected = list.iter().any(|f| f.len() == 2 && f.as_bytes()[1] == byte);
847 assert_eq!(flags.contains_short(byte), expected, "{list:?} byte {byte}");
848 assert_eq!(flags.as_slice().contains_short(byte), expected, "{list:?} byte {byte}");
849 }
850 }
851 }
852
853 #[test]
854 fn an_opaque_probe_is_consumed_as_a_flag_value_where_other_options_are_not() {
855 let next = |w: &str| consumes_next_value(Some(&Token::from_test(w)));
856 assert!(next(crate::cst::opaque::FLAG_PROBE));
857 assert!(next(crate::cst::opaque::SHORT_PROBE));
858 assert!(!next("--require=evil.js"));
859 assert!(!next("-%x"));
860 assert!(next("-5"));
861 assert!(next("-"));
862 assert!(!consumes_next_value(None));
863 }
864}