소스 검색

Stop on first unknown option and pass them down

- Close issue #26 (user can now use cdiff -s -U5 --cached)
Matthew Wang 11 년 전
부모
커밋
57e62f2a7c
1개의 변경된 파일20개의 추가작업 그리고 2개의 파일을 삭제
  1. 20
    2
      cdiff.py

+ 20
- 2
cdiff.py 파일 보기

@@ -644,12 +644,30 @@ def decode(line):
644 644
 
645 645
 
646 646
 def main():
647
-    import optparse
647
+    from optparse import (OptionParser, BadOptionError, AmbiguousOptionError)
648
+
649
+    class PassThroughOptionParser(OptionParser):
650
+        """Stop parsing on first unknown option (e.g. --cached, -U10) and pass
651
+        them down.  Note the `opt_str` in exception object does not give us
652
+        chance to take the full option back, e.g. for '-U10' it will only
653
+        contain '-U' and the '10' part will be lost.  Ref: http://goo.gl/IqY4A
654
+        (on stackoverflow).  My hack is to try parse and insert a '--' in place
655
+        and parse again.  Let me know if someone has better solution.
656
+        """
657
+        def _process_args(self, largs, rargs, values):
658
+            left = largs[:]
659
+            right = rargs[:]
660
+            try:
661
+                OptionParser._process_args(self, left, right, values)
662
+            except (BadOptionError, AmbiguousOptionError):
663
+                parsed_num = len(rargs) - len(right) - 1
664
+                rargs.insert(parsed_num, '--')
665
+            OptionParser._process_args(self, largs, rargs, values)
648 666
 
649 667
     supported_vcs = sorted(VCS_INFO.keys())
650 668
 
651 669
     usage = """%prog [options] [file|dir ...]"""
652
-    parser = optparse.OptionParser(
670
+    parser = PassThroughOptionParser(
653 671
         usage=usage, description=META_INFO['description'],
654 672
         version='%%prog %s' % META_INFO['version'])
655 673
     parser.add_option(