cmdline.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # cmdline - command line utilities.
  2. import sys
  3. import win32ui
  4. import string
  5. def ParseArgs( str ):
  6. import string
  7. ret=[]
  8. pos = 0
  9. length=len(str)
  10. while pos<length:
  11. try:
  12. while str[pos] in string.whitespace: pos = pos+1
  13. except IndexError:
  14. break
  15. if pos>=length:
  16. break
  17. if str[pos]=='"':
  18. pos=pos+1
  19. try:
  20. endPos = str.index('"', pos)-1
  21. nextPos = endPos+2
  22. except ValueError:
  23. endPos=length
  24. nextPos=endPos+1
  25. else:
  26. endPos = pos
  27. while endPos<length and not str[endPos] in string.whitespace: endPos = endPos+1
  28. nextPos=endPos+1
  29. ret.append(str[pos:endPos+1].strip())
  30. pos = nextPos
  31. return ret
  32. def FixArgFileName(fileName):
  33. """Convert a filename on the commandline to something useful.
  34. Given an automatic filename on the commandline, turn it a python module name,
  35. with the path added to sys.path. """
  36. import os
  37. path, fname = os.path.split(fileName)
  38. if len(path)==0:
  39. path = os.curdir
  40. path=os.path.abspath(path)
  41. # must check that the command line arg's path is in sys.path
  42. for syspath in sys.path:
  43. if os.path.abspath(syspath)==path:
  44. break
  45. else:
  46. sys.path.append(path)
  47. return os.path.splitext(fname)[0]