daily pastebin goal
92%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. How do I use optparse to just split the command-line arguments into options and positional args?
  2. test.py -a SOMETHING 1 2 3
  3.    
  4. >> print opt
  5. >> ['-a', 'SOMETHING']
  6.  
  7. >> print args
  8. >> ['1', '2', '3']
  9.    
  10. import optparse
  11.  
  12. parser = optparse.OptionParser()
  13. parser.add_option("-a", action="store", type="string", dest="a")
  14.  
  15. (opt, arg) = parser.parse_args()
  16. print "Opt:", opt
  17. print "Arg:", arg
  18.    
  19. Opt: {'a': 'SOMETHING'}
  20. Arg: ['1', '2', '3']
  21.    
  22. o = list()
  23. for k in vars(opt):
  24.     o.append(k)
  25.     o.append(getattr(opt, k))
  26. print "List Opt:", o
  27.    
  28. List Opt: ['a', 'SOMETHING']
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top