MCPcopy Index your code
hub / github.com/idank/explainshell / sanitize_option

Function sanitize_option

explainshell/extraction/postprocess.py:33–83  ·  view source on GitHub ↗

Fix universally-invalid field combinations. - positional + flags: clear positional - prefix without positional: clear prefix - prefix outside the sigil allowlist: clear prefix - nested_cmd without has_argument: set has_argument = True

(opt: Option)

Source from the content-addressed store, hash-verified

31
32
33def sanitize_option(opt: Option) -> Option:
34 """Fix universally-invalid field combinations.
35
36 - positional + flags: clear positional
37 - prefix without positional: clear prefix
38 - prefix outside the sigil allowlist: clear prefix
39 - nested_cmd without has_argument: set has_argument = True
40 """
41 positional = opt.positional
42 prefix = opt.prefix
43 has_argument = opt.has_argument
44 changed = False
45
46 if positional and (opt.short or opt.long):
47 logger.debug(
48 "clearing positional=%r on flagged option %s/%s",
49 positional,
50 opt.short,
51 opt.long,
52 )
53 positional = None
54 changed = True
55
56 if prefix and not positional:
57 logger.debug("clearing prefix=%r on non-positional option", prefix)
58 prefix = None
59 changed = True
60
61 if prefix and prefix not in OPTION_PREFIX_SIGILS:
62 logger.debug(
63 "dropping prefix=%r on positional %r: not in sigil allowlist",
64 prefix,
65 positional,
66 )
67 prefix = None
68 changed = True
69
70 if opt.nested_cmd and not has_argument:
71 has_argument = True
72 changed = True
73
74 if not changed:
75 return opt
76
77 return opt.model_copy(
78 update={
79 "has_argument": has_argument,
80 "positional": positional,
81 "prefix": prefix,
82 }
83 )
84
85
86def strip_trailing_blanks(opt: Option) -> Option:

Calls

no outgoing calls