w_getopt.py :  » Development » comtypes » comtypes-0.6.2 » comtypes » server » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » comtypes 
comtypes » comtypes 0.6.2 » comtypes » server » w_getopt.py
class GetoptError(Exception):
    pass

def w_getopt(args, options):
    """A getopt for Windows.

    Options may start with either '-' or '/', the option names may
    have more than one letter (/tlb or -RegServer), and option names
    are case insensitive.

    Returns two elements, just as getopt.getopt.  The first is a list
    of (option, value) pairs in the same way getopt.getopt does, but
    there is no '-' or '/' prefix to the option name, and the option
    name is always lower case.  The second is the list of arguments
    which do not belong to an option.

    Different from getopt.getopt, a single argument not belonging to an option
    does not terminate parsing.
    """
    opts = []
    arguments = []
    while args:
        if args[0][:1] in "/-":
            arg = args[0][1:] # strip the '-' or '/'
            arg = arg.lower()

            if arg + ':' in options:
                try:
                    opts.append((arg, args[1]))
                except IndexError:
                    raise GetoptError("option '%s' requires an argument" % args[0])
                args = args[1:]
            elif arg in options:
                opts.append((arg, ''))
            else:
                raise GetoptError("invalid option '%s'" % args[0])
            args = args[1:]
        else:
            arguments.append(args[0])
            args = args[1:]

    return opts, arguments

if __debug__:
    if __name__ == "__main__":
        import unittest

        class TestCase(unittest.TestCase):
            def test_1(self):
                args = "-embedding spam /RegServer foo /UnregSERVER blabla".split()
                opts, args = w_getopt(args,
                                      "regserver unregserver embedding".split())
                self.assertEqual(opts,
                                 [('embedding', ''),
                                  ('regserver', ''),
                                  ('unregserver', '')])
                self.assertEqual(args, ["spam", "foo", "blabla"])

            def test_2(self):
                args = "/TLB Hello.Tlb HELLO.idl".split()
                opts, args = w_getopt(args, ["tlb:"])
                self.assertEqual(opts, [('tlb', 'Hello.Tlb')])
                self.assertEqual(args, ['HELLO.idl'])

            def test_3(self):
                # Invalid option
                self.assertRaises(GetoptError, w_getopt,
                                  "/TLIB hello.tlb hello.idl".split(), ["tlb:"])

            def test_4(self):
                # Missing argument
                self.assertRaises(GetoptError, w_getopt,
                                  "/TLB".split(), ["tlb:"])

        unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.