MCPcopy
hub / github.com/mitmproxy/mitmproxy / _parse_setval

Method _parse_setval

mitmproxy/optmanager.py:364–410  ·  view source on GitHub ↗

Convert a string to a value appropriate for the option type.

(self, o: _Option, values: list[str])

Source from the content-addressed store, hash-verified

362 del self.deferred[k]
363
364 def _parse_setval(self, o: _Option, values: list[str]) -> Any:
365 """
366 Convert a string to a value appropriate for the option type.
367 """
368 if o.typespec == Sequence[str]:
369 return values
370 if len(values) > 1:
371 raise exceptions.OptionsError(
372 f"Received multiple values for {o.name}: {values}"
373 )
374
375 optstr: str | None
376 if values:
377 optstr = values[0]
378 else:
379 optstr = None
380
381 if o.typespec in (str, Optional[str]):
382 if o.typespec is str and optstr is None:
383 raise exceptions.OptionsError(f"Option is required: {o.name}")
384 return optstr
385 elif o.typespec in (int, Optional[int]):
386 if optstr:
387 try:
388 return int(optstr)
389 except ValueError:
390 raise exceptions.OptionsError(
391 f"Failed to parse option {o.name}: not an integer: {optstr}"
392 )
393 elif o.typespec is int:
394 raise exceptions.OptionsError(f"Option is required: {o.name}")
395 else:
396 return None
397 elif o.typespec is bool:
398 if optstr == "toggle":
399 return not o.current()
400 if not optstr or optstr == "true":
401 return True
402 elif optstr == "false":
403 return False
404 else:
405 raise exceptions.OptionsError(
406 f'Failed to parse option {o.name}: boolean must be "true", "false", or have the value omitted (a synonym for "true").'
407 )
408 raise NotImplementedError(
409 f"Failed to parse option {o.name}: unsupported option type: {o.typespec}"
410 )
411
412 def make_parser(self, parser, optname, metavar=None, short=None):
413 """

Callers 3

setMethod · 0.95
process_deferredMethod · 0.95
test_required_intFunction · 0.80

Calls 1

currentMethod · 0.45

Tested by 1

test_required_intFunction · 0.64