changeset 26:82dde2f60e46

catching no-xclip errors finally fixed
author catherine@cordelia
date Sat, 17 May 2008 07:23:15 -0400
parents c8efa4369189 (diff) 6948d1f9e4b4 (current diff)
children a76798cde34c
files cmd2.py
diffstat 1 files changed, 11 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Fri May 16 18:09:34 2008 -0400
+++ b/cmd2.py	Sat May 17 07:23:15 2008 -0400
@@ -45,8 +45,10 @@
         optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_'))
         def newFunc(instance, arg):
             try:
-                opts, arg = optionParser.parse_args(arg.split())  
-            except optparse.OptionValueError, e:
+                opts, arg = optionParser.parse_args(arg.split())
+            except (optparse.OptionValueError, optparse.BadOptionError,
+                    optparse.OptionError, optparse.AmbiguousOptionError,
+                    optparse.OptionConflictError), e:
                 print e
                 optionParser.print_help()
                 return 
@@ -141,26 +143,15 @@
         if redirect:
             return newStatement, redirect, 'r'
         return statement, '', ''
-            
+
     def pasteBufferProcess(self, mode='read'):
         if mode == 'read':
             command = 'xc1ip -o -sel clip'
         else:
             command = 'xclip -sel clip'
-        errf = tempfile.TemporaryFile()
-        result = subprocess.Popen(command, shell=True, stderr=errf, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
-        errf.seek(0)
-        if 'not found' in errf.read():
-            msg = """Redirecting to or from paste buffer requires xclip to be installed on operating system.
-            On Debian/Ubuntu, 'sudo apt-get install xclip' will install it."""
-            print msg
-            print '---'
-            errf.seek(0)
-            print errf.read()
-            print '---'
-            raise EnvironmentError, msg
+        result = subprocess.check_call(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
         return result
-        
+    
     def onecmd(self, line):
         """Interpret the argument as though it had been typed in response
         to the prompt.
@@ -189,7 +180,10 @@
                         self.stdout.write(clipcontents)
                 else:
                     statement = '%s %s' % (statement, clipcontents)
-            except EnvironmentError, e:
+            except subprocess.CalledProcessError:
+                print """Redirecting to or from paste buffer requires xclip 
+to be installed on operating system.
+On Debian/Ubuntu, 'sudo apt-get install xclip' will install it."""
                 return 0
         elif redirect:
             if mode in ('w','a'):