当前位置: 首页>>代码示例>>Python>>正文


Python OptionParser.parse_args方法代码示例

本文整理汇总了Python中ClusterShell.CLI.OptionParser.OptionParser.parse_args方法的典型用法代码示例。如果您正苦于以下问题:Python OptionParser.parse_args方法的具体用法?Python OptionParser.parse_args怎么用?Python OptionParser.parse_args使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ClusterShell.CLI.OptionParser.OptionParser的用法示例。


在下文中一共展示了OptionParser.parse_args方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testClushConfigSetRlimitValueError

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testClushConfigSetRlimitValueError(self):
        """test CLI.Config.ClushConfig (setrlimit ValueError)"""
        soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
        f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
        f.write(dedent("""
            [Main]
            fanout: 42
            connect_timeout: 14
            command_timeout: 0
            history_size: 100
            color: auto
            # Use wrong fd_max value to generate ValueError
            fd_max: -1
            verbosity: 1"""))
        f.flush()
        parser = OptionParser("dummy")
        parser.install_config_options()
        parser.install_display_options(verbose_options=True)
        parser.install_connector_options()
        options, _ = parser.parse_args([])
        config = ClushConfig(options, filename=f.name)
        f.close()
        display = Display(options, config)

        class TestException(Exception): pass

        def mock_vprint_err(level, message):
            if message.startswith('Warning: Failed to set max open files'):
                raise TestException()

        display.vprint_err = mock_vprint_err
        self.assertRaises(TestException, set_fdlimit, config.fd_max, display)

        soft2, _ = resource.getrlimit(resource.RLIMIT_NOFILE)
        self.assertEqual(soft, soft2)
开发者ID:cccnam5158,项目名称:clustershell,代码行数:37,代码来源:CLIConfigTest.py

示例2: testClushConfigAlmostEmpty

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testClushConfigAlmostEmpty(self):
        """test CLI.Config.ClushConfig (almost empty)"""

        f = tempfile.NamedTemporaryFile(prefix="testclushconfig")
        f.write(
            """
[Main]
"""
        )

        parser = OptionParser("dummy")
        parser.install_display_options(verbose_options=True)
        parser.install_connector_options()
        options, _ = parser.parse_args([])
        config = ClushConfig(options, filename=f.name)
        self.assert_(config != None)
        self.assertEqual(config.color, WHENCOLOR_CHOICES[-1])
        self.assertEqual(config.verbosity, VERB_STD)
        self.assertEqual(config.node_count, True)
        self.assertEqual(config.fanout, 64)
        self.assertEqual(config.connect_timeout, 30)
        self.assertEqual(config.command_timeout, 0)
        self.assertEqual(config.ssh_user, None)
        self.assertEqual(config.ssh_path, None)
        self.assertEqual(config.ssh_options, None)
        f.close()
开发者ID:bougetq,项目名称:clustershell,代码行数:28,代码来源:CLIConfigTest.py

示例3: testDisplayRegroup

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testDisplayRegroup(self):
        """test CLI.Display (regroup)"""
        f = makeTestFile("""
# A comment

[Main]
default: local

[local]
map: echo hostfoo
#all:
list: echo all
#reverse:
        """)
        res = GroupResolverConfig(f.name)
        set_std_group_resolver(res)
        try:
            parser = OptionParser("dummy")
            parser.install_display_options(verbose_options=True)
            options, _ = parser.parse_args(["-r"])

            disp = Display(options, color=False)
            self.assertEqual(disp.regroup, True)
            disp.out = StringIO()
            disp.err = StringIO()
            self.assertEqual(disp.line_mode, False)

            ns = NodeSet("hostfoo")

            # nodeset.regroup() is performed by print_gather()
            disp.print_gather(ns, "message0\nmessage1\n")
            self.assertEqual(disp.out.getvalue(),
                "---------------\[email protected]\n---------------\nmessage0\nmessage1\n\n")
        finally:
            set_std_group_resolver(None)
开发者ID:cccnam5158,项目名称:clustershell,代码行数:37,代码来源:CLIDisplayTest.py

示例4: testClushConfigFull

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testClushConfigFull(self):
        """test CLI.Config.ClushConfig (full)"""

        f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
        f.write("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: auto
node_count: yes
verbosity: 1
ssh_user: root
ssh_path: /usr/bin/ssh
ssh_options: -oStrictHostKeyChecking=no
""")

        f.flush()
        parser = OptionParser("dummy")
        parser.install_display_options(verbose_options=True)
        parser.install_connector_options()
        options, _ = parser.parse_args([])
        config = ClushConfig(options, filename=f.name)
        self.assert_(config != None)
        self.assertEqual(config.color, WHENCOLOR_CHOICES[2])
        self.assertEqual(config.verbosity, VERB_STD)
        self.assertEqual(config.node_count, True)
        self.assertEqual(config.fanout, 42)
        self.assertEqual(config.connect_timeout, 14)
        self.assertEqual(config.command_timeout, 0)
        self.assertEqual(config.ssh_user, "root")
        self.assertEqual(config.ssh_path, "/usr/bin/ssh")
        self.assertEqual(config.ssh_options, "-oStrictHostKeyChecking=no")
        f.close()
开发者ID:jasonshih,项目名称:clustershell,代码行数:37,代码来源:CLIConfigTest.py

示例5: testDisplay

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testDisplay(self):
        """test CLI.Display"""
        parser = OptionParser("dummy")
        parser.install_display_options(verbose_options=True)
        options, _ = parser.parse_args([])

        ns = NodeSet("hostfoo")
        mtree = MsgTree()
        mtree.add("hostfoo", "message0")
        mtree.add("hostfoo", "message1")

        for whencolor in WHENCOLOR_CHOICES: # test whencolor switch
            for label in [True, False]:     # test no-label switch
                options.label = label
                options.whencolor = whencolor
                disp = Display(options)
                # inhibit output
                disp.out = StringIO()
                disp.err = StringIO()
                # test print_* methods...
                disp.print_line(ns, "foo bar")
                disp.print_line_error(ns, "foo bar")
                disp.print_gather(ns, list(mtree.walk())[0][0])
                # test also string nodeset as parameter
                disp.print_gather("hostfoo", list(mtree.walk())[0][0])
                # test line_mode property
                self.assertEqual(disp.line_mode, False)
                disp.line_mode = True
                self.assertEqual(disp.line_mode, True)
                disp.print_gather("hostfoo", list(mtree.walk())[0][0])
                disp.line_mode = False
                self.assertEqual(disp.line_mode, False)
开发者ID:cccnam5158,项目名称:clustershell,代码行数:34,代码来源:CLIDisplayTest.py

示例6: testClushConfigDefaultWithOptions

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testClushConfigDefaultWithOptions(self):
        """test CLI.Config.ClushConfig (default with options)"""

        f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
        f.write(dedent("""
            [Main]
            fanout: 42
            connect_timeout: 14
            command_timeout: 0
            history_size: 100
            color: auto
            verbosity: 1""").encode())
        f.flush()
        parser = OptionParser("dummy")
        parser.install_clush_config_options()
        parser.install_display_options(verbose_options=True)
        parser.install_connector_options()
        options, _ = parser.parse_args(["-f", "36", "-u", "3", "-t", "7",
                                        "--user", "foobar", "--color",
                                        "always", "-d", "-v", "-q", "-o",
                                        "-oSomething"])
        config = ClushConfig(options, filename=f.name)
        display = Display(options, config)
        display.vprint(VERB_STD, "test")
        display.vprint(VERB_DEBUG, "test")
        self.assertEqual(config.color, WHENCOLOR_CHOICES[1])
        self.assertEqual(config.verbosity, VERB_DEBUG) # takes biggest
        self.assertEqual(config.fanout, 36)
        self.assertEqual(config.connect_timeout, 7)
        self.assertEqual(config.command_timeout, 3)
        self.assertEqual(config.ssh_user, "foobar")
        self.assertEqual(config.ssh_path, None)
        self.assertEqual(config.ssh_options, "-oSomething")
        f.close()
开发者ID:cea-hpc,项目名称:clustershell,代码行数:36,代码来源:CLIConfigTest.py

示例7: testClushConfigDefault

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testClushConfigDefault(self):
        """test CLI.Config.ClushConfig (default)"""

        f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
        f.write(dedent("""
            [Main]
            fanout: 42
            connect_timeout: 14
            command_timeout: 0
            history_size: 100
            color: auto
            verbosity: 1
            #ssh_user: root
            #ssh_path: /usr/bin/ssh
            #ssh_options: -oStrictHostKeyChecking=no""").encode())
        f.flush()
        parser = OptionParser("dummy")
        parser.install_clush_config_options()
        parser.install_display_options(verbose_options=True)
        parser.install_connector_options()
        options, _ = parser.parse_args([])
        config = ClushConfig(options, filename=f.name)
        display = Display(options, config)
        display.vprint(VERB_STD, "test")
        display.vprint(VERB_DEBUG, "shouldn't see this")
        self.assertEqual(config.color, WHENCOLOR_CHOICES[2])
        self.assertEqual(config.verbosity, VERB_STD)
        self.assertEqual(config.node_count, True)
        self.assertEqual(config.fanout, 42)
        self.assertEqual(config.connect_timeout, 14)
        self.assertEqual(config.command_timeout, 0)
        self.assertEqual(config.ssh_user, None)
        self.assertEqual(config.ssh_path, None)
        self.assertEqual(config.ssh_options, None)
        f.close()
开发者ID:cea-hpc,项目名称:clustershell,代码行数:37,代码来源:CLIConfigTest.py

示例8: testClushConfigSetRlimit

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testClushConfigSetRlimit(self):
        """test CLI.Config.ClushConfig (setrlimit)"""
        soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
        hard2 = min(32768, hard)
        f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
        f.write("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: auto
fd_max: %d
verbosity: 1
""" % hard2)

        f.flush()
        parser = OptionParser("dummy")
        parser.install_display_options(verbose_options=True)
        parser.install_connector_options()
        options, _ = parser.parse_args([])
        config = ClushConfig(options, filename=f.name)
        self.assert_(config != None)
        display = Display(options, config)
        self.assert_(display != None)

        # force a lower soft limit
        resource.setrlimit(resource.RLIMIT_NOFILE, (hard2/2, hard))
        # max_fdlimit should increase soft limit again
        set_fdlimit(config.fd_max, display)
        # verify
        soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
        self.assertEqual(soft, hard2)
        f.close()
开发者ID:jasonshih,项目名称:clustershell,代码行数:36,代码来源:CLIConfigTest.py

示例9: testDisplayRegroup

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testDisplayRegroup(self):
        """test CLI.Display (regroup)"""
        parser = OptionParser("dummy")
        parser.install_display_options(verbose_options=True)
        options, _ = parser.parse_args(["-r"])

        mtree = MsgTree()
        mtree.add("localhost", "message0")
        mtree.add("localhost", "message1")

        disp = Display(options)
        self.assertEqual(disp.regroup, True)
        disp.out = open("/dev/null", "w")
        disp.err = open("/dev/null", "w")
        self.assert_(disp != None)
        self.assertEqual(disp.line_mode, False)

        f = makeTestFile("""
# A comment

[Main]
default: local

[local]
map: echo localhost
#all:
list: echo all
#reverse:
        """)
        res = GroupResolverConfig(f.name)
        ns = NodeSet("localhost", resolver=res)

        # nodeset.regroup() is performed by print_gather()
        disp.print_gather(ns, list(mtree.walk())[0][0])
开发者ID:ChristianKniep,项目名称:clustershell,代码行数:36,代码来源:CLIDisplayTest.py

示例10: testOptionParser

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
 def testOptionParser(self):
     """test CLI.OptionParser (1)"""
     parser = OptionParser("dummy")
     parser.install_nodes_options()
     parser.install_display_options(verbose_options=True)
     parser.install_filecopy_options()
     parser.install_ssh_options()
     options, _ = parser.parse_args([])
开发者ID:ChristianKniep,项目名称:clustershell,代码行数:10,代码来源:CLIOptionParserTest.py

示例11: testOptionParser2

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
 def testOptionParser2(self):
     """test CLI.OptionParser (2)"""
     parser = OptionParser("dummy")
     parser.install_nodes_options()
     parser.install_display_options(verbose_options=True, separator_option=True)
     parser.install_filecopy_options()
     parser.install_connector_options()
     options, _ = parser.parse_args([])
开发者ID:cea-hpc,项目名称:clustershell,代码行数:10,代码来源:CLIOptionParserTest.py

示例12: testClushConfigError

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testClushConfigError(self):
        """test CLI.Config.ClushConfig (error)"""

        f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
        f.write(dedent("""
            [Main]
            fanout: 3.2
            connect_timeout: foo
            command_timeout: bar
            history_size: 100
            color: maybe
            node_count: 3
            verbosity: bar
            ssh_user: root
            ssh_path: /usr/bin/ssh
            ssh_options: -oStrictHostKeyChecking=no
            """).encode())

        f.flush()
        parser = OptionParser("dummy")
        parser.install_clush_config_options()
        parser.install_display_options(verbose_options=True)
        parser.install_connector_options()
        options, _ = parser.parse_args([])
        config = ClushConfig(options, filename=f.name)
        try:
            c = config.color
            self.fail("Exception ClushConfigError not raised (color)")
        except ClushConfigError:
            pass
        self.assertEqual(config.verbosity, 0) # probably for compatibility
        try:
            f = config.fanout
            self.fail("Exception ClushConfigError not raised (fanout)")
        except ClushConfigError:
            pass
        try:
            f = config.node_count
            self.fail("Exception ClushConfigError not raised (node_count)")
        except ClushConfigError:
            pass
        try:
            f = config.fanout
        except ClushConfigError as e:
            self.assertEqual(str(e)[0:20], "(Config Main.fanout)")

        try:
            t = config.connect_timeout
            self.fail("Exception ClushConfigError not raised (connect_timeout)")
        except ClushConfigError:
            pass
        try:
            m = config.command_timeout
            self.fail("Exception ClushConfigError not raised (command_timeout)")
        except ClushConfigError:
            pass
        f.close()
开发者ID:cea-hpc,项目名称:clustershell,代码行数:59,代码来源:CLIConfigTest.py

示例13: testClushConfigUserOverride

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
    def testClushConfigUserOverride(self):
        """test CLI.Config.ClushConfig (XDG_CONFIG_HOME user config)"""

        # XXX Test should be improved when CLUSTERSHELL_CONFIG is available
        # Improvement: override CLUSTERSHELL_CONFIG and set a sys clush config
        # then verify that user config overrides CLUSTERSHELL_CONFIG as
        # expected...
        # For now, it has been tested manually. This test only really only
        # ensures that user config is taken into account.

        xdg_config_home_save = os.environ.get('XDG_CONFIG_HOME')

        # Create fake XDG_CONFIG_HOME
        dname = make_temp_dir()
        try:
            os.environ['XDG_CONFIG_HOME'] = dname

            # create $XDG_CONFIG_HOME/clustershell/clush.conf
            usercfgdir = os.path.join(dname, 'clustershell')
            os.mkdir(usercfgdir)
            cfgfile = open(os.path.join(usercfgdir, 'clush.conf'), 'w')
            cfgfile.write("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: never
verbosity: 2
ssh_user: trump
ssh_path: ~/bin/ssh
ssh_options: -oSomeDummyUserOption=yes
""")

            cfgfile.flush()
            parser = OptionParser("dummy")
            parser.install_config_options()
            parser.install_display_options(verbose_options=True)
            parser.install_connector_options()
            options, _ = parser.parse_args([])
            config = ClushConfig(options) # filename=None to use defaults!
            self.assertEqual(config.color, WHENCOLOR_CHOICES[0])
            self.assertEqual(config.verbosity, VERB_VERB) # takes biggest
            self.assertEqual(config.fanout, 42)
            self.assertEqual(config.connect_timeout, 14)
            self.assertEqual(config.command_timeout, 0)
            self.assertEqual(config.ssh_user, 'trump')
            self.assertEqual(config.ssh_path, '~/bin/ssh')
            self.assertEqual(config.ssh_options, '-oSomeDummyUserOption=yes')
            cfgfile.close()

        finally:
            if xdg_config_home_save:
                os.environ['XDG_CONFIG_HOME'] = xdg_config_home_save
            else:
                del os.environ['XDG_CONFIG_HOME']
            shutil.rmtree(dname, ignore_errors=True)
开发者ID:LaHaine,项目名称:ohpc,代码行数:59,代码来源:CLIConfigTest.py

示例14: clubak

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
def clubak():
    """script subroutine"""

    # Argument management
    parser = OptionParser("%prog [options]")
    parser.install_display_options(verbose_options=True,
                                   separator_option=True,
                                   dshbak_compat=True,
                                   msgtree_mode=True)
    options = parser.parse_args()[0]

    if options.interpret_keys == THREE_CHOICES[-1]: # auto?
        enable_nodeset_key = None # AUTO
    else:
        enable_nodeset_key = (options.interpret_keys == THREE_CHOICES[1])

    # Create new message tree
    if options.trace_mode:
        tree_mode = MODE_TRACE
    else:
        tree_mode = MODE_DEFER
    tree = MsgTree(mode=tree_mode)
    fast_mode = options.fast_mode
    if fast_mode:
        if tree_mode != MODE_DEFER or options.line_mode:
            parser.error("incompatible tree options")
        preload_msgs = {}

    # Feed the tree from standard input lines
    for line in sys.stdin:
        try:
            linestripped = line.rstrip('\r\n')
            if options.verbose or options.debug:
                print "INPUT %s" % linestripped
            key, content = linestripped.split(options.separator, 1)
            key = key.strip()
            if not key:
                raise ValueError("no node found")
            if enable_nodeset_key is False: # interpret-keys=never?
                keyset = [ key ]
            else:
                try:
                    keyset = NodeSet(key)
                except NodeSetParseError:
                    if enable_nodeset_key: # interpret-keys=always?
                        raise
                    enable_nodeset_key = False # auto => switch off
                    keyset = [ key ]
            if fast_mode:
                for node in keyset:
                    preload_msgs.setdefault(node, []).append(content)
            else:
                for node in keyset:
                    tree.add(node, content)
        except ValueError, ex:
            raise ValueError("%s (\"%s\")" % (ex, linestripped))
开发者ID:ChristianKniep,项目名称:clustershell,代码行数:58,代码来源:Clubak.py

示例15: testClushConfigWithInstalledConfig

# 需要导入模块: from ClusterShell.CLI.OptionParser import OptionParser [as 别名]
# 或者: from ClusterShell.CLI.OptionParser.OptionParser import parse_args [as 别名]
 def testClushConfigWithInstalledConfig(self):
     """test CLI.Config.ClushConfig (installed config required)"""
     # This test needs installed configuration files (needed for
     # maximum coverage).
     parser = OptionParser("dummy")
     parser.install_display_options(verbose_options=True)
     parser.install_connector_options()
     options, _ = parser.parse_args([])
     config = ClushConfig(options)
     self.assert_(config != None)
开发者ID:jasonshih,项目名称:clustershell,代码行数:12,代码来源:CLIConfigTest.py


注:本文中的ClusterShell.CLI.OptionParser.OptionParser.parse_args方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。