summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2013-02-25 02:00:01 +0100
committerhut <hut@lavabit.com>2013-02-25 02:02:22 +0100
commit93a13f70b6c1ccefade56e8e360498a68db36356 (patch)
tree86ba594a573f7b6a88d4294ec5eae55e5730f997 /ranger
parentca7c55524a4aa8d283accb9b01de00e2a238751f (diff)
downloadranger-93a13f70b6c1ccefade56e8e360498a68db36356.tar.gz
api.commands: added parse_flags()
Diffstat (limited to 'ranger')
-rw-r--r--ranger/api/commands.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/ranger/api/commands.py b/ranger/api/commands.py
index 8a9df1ec..f66be7ee 100644
--- a/ranger/api/commands.py
+++ b/ranger/api/commands.py
@@ -164,6 +164,38 @@ class Command(FileManagerAware):
         self._setting_line = result
         return result
 
+    def parse_flags(self):
+        """Finds and returns flags in the command
+
+        >>> Command("").parse_flags()
+        ('', '')
+        >>> Command("foo").parse_flags()
+        ('', '')
+        >>> Command("shell test").parse_flags()
+        ('', 'test')
+        >>> Command("shell -t ls -l").parse_flags()
+        ('t', 'ls -l')
+        >>> Command("shell -f -- -q test").parse_flags()
+        ('f', '-q test')
+        >>> Command("shell -foo -bar rest of the command").parse_flags()
+        ('foobar', 'rest of the command')
+        """
+        flags = ""
+        args = self.line.split()
+        rest = ""
+        if len(args) > 0:
+            rest = self.line[len(args[0]):].lstrip()
+            for arg in args[1:]:
+                if arg == "--":
+                    rest = rest[2:].lstrip()
+                    break
+                elif len(arg) > 1 and arg[0] == "-":
+                    rest = rest[len(arg):].lstrip()
+                    flags += arg[1:]
+                else:
+                    break
+        return flags, rest
+
     # XXX: Lazy properties? Not so smart? self.line can change after all!
     @lazy_property
     def _tabinsert_left(self):
@@ -343,3 +375,8 @@ class AliasCommand(Command):
     _line = ""
     def execute(self):
         self.fm.execute_console(self._line + ' ' + self.rest(1))
+
+
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()