MCPcopy Index your code
hub / github.com/ipython/ipython / parse_options

Method parse_options

IPython/core/magic.py:687–778  ·  view source on GitHub ↗

Parse options passed to an argument string. The interface is similar to that of :func:`getopt.getopt`, but it returns a :class:`~IPython.utils.struct.Struct` with the options as keys and the stripped argument string still as a string. arg_str is quoted as a true sys

(
        self, arg_str: str, opt_str: str, *long_opts: str, **kw: Any
    )

Source from the content-addressed store, hash-verified

685 return strng
686
687 def parse_options(
688 self, arg_str: str, opt_str: str, *long_opts: str, **kw: Any
689 ) -> tuple[Any, Any]:
690 """Parse options passed to an argument string.
691
692 The interface is similar to that of :func:`getopt.getopt`, but it
693 returns a :class:`~IPython.utils.struct.Struct` with the options as keys
694 and the stripped argument string still as a string.
695
696 arg_str is quoted as a true sys.argv vector by using shlex.split.
697 This allows us to easily expand variables, glob files, quote
698 arguments, etc.
699
700 Parameters
701 ----------
702 arg_str : str
703 The arguments to parse.
704 opt_str : str
705 The options specification.
706 mode : str, default 'string'
707 If given as 'list', the argument string is returned as a list (split
708 on whitespace) instead of a string.
709 list_all : bool, default False
710 Put all option values in lists. Normally only options
711 appearing more than once are put in a list.
712 posix : bool, default True
713 Whether to split the input line in POSIX mode or not, as per the
714 conventions outlined in the :mod:`shlex` module from the standard
715 library.
716 """
717
718 # inject default options at the beginning of the input line
719 caller = sys._getframe(1).f_code.co_name
720 arg_str = "%s %s" % (self.options_table.get(caller, ""), arg_str)
721
722 mode = kw.get("mode", "string")
723 if mode not in ["string", "list"]:
724 raise ValueError("incorrect mode given: %s" % mode)
725 # Get options
726 list_all = kw.get("list_all", 0)
727 posix = kw.get("posix", os.name == "posix")
728 strict = kw.get("strict", True)
729
730 preserve_non_opts = kw.get("preserve_non_opts", False)
731 remainder_arg_str = arg_str
732
733 # Check if we have more than one argument to warrant extra processing:
734 odict: dict[str, t.Any] = {} # Dictionary with options
735 args = arg_str.split()
736 if len(args) >= 1:
737 # If the list of inputs only has 0 or 1 thing in it, there's no
738 # need to look for options
739 argv = arg_split(arg_str, posix, strict)
740 # Do regular option processing
741 try:
742 opts, args = getopt(argv, opt_str, long_opts)
743 except GetoptError as e:
744 raise UsageError(

Callers 15

cpasteMethod · 0.80
pasteMethod · 0.80
_extract_codeMethod · 0.80
logstartMethod · 0.80
prunMethod · 0.80
runMethod · 0.80
timeitMethod · 0.80
macroMethod · 0.80
rerunMethod · 0.80
saveMethod · 0.80
pastebinMethod · 0.80
loadMethod · 0.80

Calls 5

UsageErrorClass · 0.85
StructClass · 0.85
getMethod · 0.80
replaceMethod · 0.80
arg_splitFunction · 0.50

Tested by 5

test_magic_parse_optionsFunction · 0.64
test_parse_optionsFunction · 0.64
_run_edit_testFunction · 0.64