##// END OF EJS Templates
Merge pull request #12708 from adityausathe/12107_parse_opts...
Matthias Bussonnier -
r26473:e08519b0 merge
parent child Browse files
Show More
@@ -618,6 +618,9 b' class Magics(Configurable):'
618 posix = kw.get('posix', os.name == 'posix')
618 posix = kw.get('posix', os.name == 'posix')
619 strict = kw.get('strict', True)
619 strict = kw.get('strict', True)
620
620
621 preserve_non_opts = kw.get("preserve_non_opts", False)
622 remainder_arg_str = arg_str
623
621 # Check if we have more than one argument to warrant extra processing:
624 # Check if we have more than one argument to warrant extra processing:
622 odict = {} # Dictionary with options
625 odict = {} # Dictionary with options
623 args = arg_str.split()
626 args = arg_str.split()
@@ -629,10 +632,18 b' class Magics(Configurable):'
629 try:
632 try:
630 opts,args = getopt(argv, opt_str, long_opts)
633 opts,args = getopt(argv, opt_str, long_opts)
631 except GetoptError as e:
634 except GetoptError as e:
632 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
635 raise UsageError(
633 " ".join(long_opts))) from e
636 '%s ( allowed: "%s" %s)' % (e.msg, opt_str, " ".join(long_opts))
637 ) from e
634 for o,a in opts:
638 for o, a in opts:
635 if o.startswith('--'):
639 if mode is "string" and preserve_non_opts:
640 # remove option-parts from the original args-string and preserve remaining-part.
641 # This relies on the arg_split(...) and getopt(...)'s impl spec, that the parsed options are
642 # returned in the original order.
643 remainder_arg_str = remainder_arg_str.replace(o, "", 1).replace(
644 a, "", 1
645 )
646 if o.startswith("--"):
636 o = o[2:]
647 o = o[2:]
637 else:
648 else:
638 o = o[1:]
649 o = o[1:]
@@ -649,7 +660,10 b' class Magics(Configurable):'
649 # Prepare opts,args for return
660 # Prepare opts,args for return
650 opts = Struct(odict)
661 opts = Struct(odict)
651 if mode == 'string':
662 if mode == 'string':
652 args = ' '.join(args)
663 if preserve_non_opts:
664 args = remainder_arg_str.lstrip()
665 else:
666 args = " ".join(args)
653
667
654 return opts,args
668 return opts,args
655
669
@@ -1073,8 +1073,9 b' class ExecutionMagics(Magics):'
1073 does not matter as long as results from timeit.py are not mixed with
1073 does not matter as long as results from timeit.py are not mixed with
1074 those from %timeit."""
1074 those from %timeit."""
1075
1075
1076 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
1076 opts, stmt = self.parse_options(
1077 posix=False, strict=False)
1077 line, "n:r:tcp:qo", posix=False, strict=False, preserve_non_opts=True
1078 )
1078 if stmt == "" and cell is None:
1079 if stmt == "" and cell is None:
1079 return
1080 return
1080
1081
@@ -472,6 +472,23 b' def test_parse_options():'
472 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
472 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
473
473
474
474
475 def test_parse_options_preserve_non_option_string():
476 """Test to assert preservation of non-option part of magic-block, while parsing magic options."""
477 m = DummyMagics(_ip)
478 opts, stmt = m.parse_options(
479 " -n1 -r 13 _ = 314 + foo", "n:r:", preserve_non_opts=True
480 )
481 nt.assert_equal(opts, {"n": "1", "r": "13"})
482 nt.assert_equal(stmt, "_ = 314 + foo")
483
484
485 def test_run_magic_preserve_code_block():
486 """Test to assert preservation of non-option part of magic-block, while running magic."""
487 _ip.user_ns["spaces"] = []
488 _ip.magic("timeit -n1 -r1 spaces.append([s.count(' ') for s in ['document']])")
489 assert _ip.user_ns["spaces"] == [[0]]
490
491
475 def test_dirops():
492 def test_dirops():
476 """Test various directory handling operations."""
493 """Test various directory handling operations."""
477 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
494 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
General Comments 0
You need to be logged in to leave comments. Login now