MCPcopy Create free account
hub / github.com/IBM/mcp-cli / _parse_arguments

Method _parse_arguments

src/mcp_cli/adapters/interactive.py:128–185  ·  view source on GitHub ↗

Parse shell-style arguments into kwargs. Handles: - Flags: --flag or -f - Options: --option value or --option=value - Positional arguments

(command: Any, args: list[str])

Source from the content-addressed store, hash-verified

126
127 @staticmethod
128 def _parse_arguments(command: Any, args: list[str]) -> dict[str, Any]:
129 """
130 Parse shell-style arguments into kwargs.
131
132 Handles:
133 - Flags: --flag or -f
134 - Options: --option value or --option=value
135 - Positional arguments
136 """
137 kwargs: dict[str, Any] = {}
138 i = 0
139 positional: list[str] = []
140
141 while i < len(args):
142 arg = args[i]
143
144 if arg.startswith("--"):
145 # Long option
146 if "=" in arg:
147 # --option=value format
148 option_name, value = arg[2:].split("=", 1)
149 kwargs[option_name] = value
150 else:
151 option_name = arg[2:]
152
153 # Check if this is a flag
154 param = next(
155 (p for p in command.parameters if p.name == option_name), None
156 )
157
158 if param and param.is_flag:
159 kwargs[option_name] = True
160 elif i + 1 < len(args) and not args[i + 1].startswith("-"):
161 # Has a value
162 kwargs[option_name] = args[i + 1]
163 i += 1
164 else:
165 # No value, treat as flag
166 kwargs[option_name] = True
167
168 elif arg.startswith("-") and len(arg) > 1:
169 # Short option(s)
170 for c in arg[1:]:
171 # Map short option to long option if possible
172 # For now, just use the short option as-is
173 kwargs[c] = True
174
175 else:
176 # Positional argument
177 positional.append(arg)
178
179 i += 1
180
181 # Add positional arguments (always as a list for consistency)
182 if positional:
183 kwargs["args"] = positional
184
185 return kwargs

Callers 1

handle_commandMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected