MCPcopy Create free account
hub / github.com/python-cmd2/cmd2 / _looks_like_flag

Function _looks_like_flag

cmd2/argparse_completer.py:72–93  ·  view source on GitHub ↗

Determine if a token looks like a flag. Unless an argument has nargs set to argparse.REMAINDER, then anything that looks like a flag can't be consumed as a value for it. Based on argparse._parse_optional().

(token: str, parser: Cmd2ArgumentParser)

Source from the content-addressed store, hash-verified

70
71
72def _looks_like_flag(token: str, parser: Cmd2ArgumentParser) -> bool:
73 """Determine if a token looks like a flag.
74
75 Unless an argument has nargs set to argparse.REMAINDER, then anything that looks like a flag
76 can't be consumed as a value for it.
77
78 Based on argparse._parse_optional().
79 """
80 # Flags have to be at least characters
81 if len(token) < 2:
82 return False
83
84 # Flags have to start with a prefix character
85 if token[0] not in parser.prefix_chars:
86 return False
87
88 # If it looks like a negative number, it is not a flag unless there are negative-number-like flags
89 if parser._negative_number_matcher.match(token) and not parser._has_negative_number_optionals:
90 return False
91
92 # Flags can't have a space
93 return " " not in token
94
95
96class _ArgumentState:

Callers 3

test_looks_like_flagFunction · 0.90
completeMethod · 0.85
_handle_last_tokenMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_looks_like_flagFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…