Anonymous avatar Anonymous committed 4d35295

added catch all remaining args for ext commands. Thanks to miracle2k

Comments (0)

Files changed (2)

flaskext/script.py

                 remaining_args.append(arg)
 
         command_parser = command.create_parser(prog + " " + name)
-        command_namespace = command_parser.parse_args(remaining_args)
+        if getattr(command, 'capture_all_args', False):
+            command_namespace, unparsed_args = \
+                command_parser.parse_known_args(remaining_args)
+            positional_args = [unparsed_args]
+        else:
+            command_namespace = command_parser.parse_args(remaining_args)
+            positional_args = []
         
         app = self.create_app(**app_namespace.__dict__)
 
         with app.test_request_context():
-            command.run(**command_namespace.__dict__)
+            command.run(*positional_args, **command_namespace.__dict__)
 
     def run(self, commands=None):
         
         print name
 
 
+class CommandWithCatchAll(Command):
+    "command with catch all args"
+
+    capture_all_args = True
+
+    def get_options(self):
+        return (Option('--foo', dest='foo',
+                       action='store_true'),)
+    def run(self, remaining_args, foo):
+        print remaining_args
+
+
 class TestCommands(unittest.TestCase):
 
     TESTING = True
             assert e.code == 0
         assert "Fred" in sys.stdout.getvalue()
 
+    def test_run_catch_all(self):
+        manager = Manager(self.app)
+        manager.add_command("catch", CommandWithCatchAll())
+        sys.argv = ["manage.py", "catch", "pos1", "--foo", "pos2", "--bar"]
+        try:
+            manager.run()
+        except SystemExit, e:
+            assert e.code == 0
+        assert "['pos1', 'pos2', '--bar']" in sys.stdout.getvalue()
+
     def test_run_bad_options(self):
 
         manager = Manager(self.app)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.