this new command takes a sub-command name as its first positional argument
and passes the rest of arguments to the corresponding sub-module.
for example, 'run' sub-command is an equivalent to bin/ryu-manager.

this commit does not replace the existing commands like ryu-manager.
this merely adds new ways to invoke the functionalities.

Signed-off-by: YAMAMOTO Takashi <yamam...@valinux.co.jp>
---
 bin/ryu             | 20 ++++++++++++++
 ryu/cmd/ryu_base.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 setup.cfg           |  1 +
 3 files changed, 96 insertions(+)
 create mode 100755 bin/ryu
 create mode 100644 ryu/cmd/ryu_base.py

diff --git a/bin/ryu b/bin/ryu
new file mode 100755
index 0000000..d726b48
--- /dev/null
+++ b/bin/ryu
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+
+# Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
+# Copyright (C) 2014 YAMAMOTO Takashi <yamamoto at valinux co jp>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from ryu.cmd.ryu_base import main
+main()
diff --git a/ryu/cmd/ryu_base.py b/ryu/cmd/ryu_base.py
new file mode 100644
index 0000000..9ea75c2
--- /dev/null
+++ b/ryu/cmd/ryu_base.py
@@ -0,0 +1,75 @@
+# Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
+# Copyright (C) 2014 YAMAMOTO Takashi <yamamoto at valinux co jp>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import ryu.contrib
+
+from ryu import cfg
+from ryu import utils
+from ryu import version
+import argparse
+import os.path
+import sys
+
+
+subcommands = {
+    'run': 'ryu.cmd.manager',
+    'of-config-cli': 'ryu.cmd.of_config_cli',
+    'rpc-cli': 'ryu.cmd.rpc_cli',
+}
+
+
+class RemainderOpt(cfg.MultiStrOpt):
+    def _get_argparse_kwargs(self, group, **kwargs):
+        kwargs = cfg.MultiStrOpt._get_argparse_kwargs(self, group, **kwargs)
+        kwargs['nargs'] = argparse.REMAINDER
+        return kwargs
+
+
+base_conf = cfg.ConfigOpts()
+base_conf.register_cli_opt(cfg.StrOpt('subcommand', positional=True,
+                                      required=True,
+                                      help='[%s]' % '|'.join(
+                                          subcommands.keys())))
+base_conf.register_cli_opt(RemainderOpt('subcommand_args', default=[],
+                                        positional=True,
+                                        help='subcommand specific arguments'))
+
+
+class SubCommand(object):
+    def __init__(self, name, entry):
+        self.name = name
+        self.entry = entry
+
+    def run(self, args):
+        prog = '%s %s' % (os.path.basename(sys.argv[0]), self.name,)
+        self.entry(args=args, prog=prog)
+
+
+def main():
+    try:
+        base_conf(project='ryu', version='ryu %s' % version)
+    except cfg.RequiredOptError, e:
+        base_conf.print_help()
+        raise SystemExit(1)
+    subcmd_name = base_conf.subcommand
+    try:
+        subcmd_mod_name = subcommands[subcmd_name]
+    except KeyError:
+        base_conf.print_help()
+        raise SystemExit('Unknown subcommand %s' % subcmd_name)
+    subcmd_mod = utils.import_module(subcmd_mod_name)
+    subcmd = SubCommand(name=subcmd_name, entry=subcmd_mod.main)
+    subcmd.run(base_conf.subcommand_args)
diff --git a/setup.cfg b/setup.cfg
index b0a0d17..42b457d 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -49,4 +49,5 @@ setup-hooks =
 console_scripts =
     neutron-ofagent-agent = ryu.cmd.ofa_neutron_agent:main
     ryu-manager = ryu.cmd.manager:main
+    ryu = ryu.cmd.ryu_base:main
     rpc-cli = ryu.cmd.rpc_cli:main
-- 
1.8.3.1


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Ryu-devel mailing list
Ryu-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to