jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.9KB

  1. #!/usr/bin/env python
  2. import getopt
  3. def my_getopt(args, shortopts, longopts = []):
  4. """getopt(args, options[, long_options]) -> opts, args
  5. Parses command line options and parameter list. args is the
  6. argument list to be parsed, without the leading reference to the
  7. running program. Typically, this means "sys.argv[1:]". shortopts
  8. is the string of option letters that the script wants to
  9. recognize, with options that require an argument followed by a
  10. colon (i.e., the same format that Unix getopt() uses). If
  11. specified, longopts is a list of strings with the names of the
  12. long options which should be supported. The leading '--'
  13. characters should not be included in the option name. Options
  14. which require an argument should be followed by an equal sign
  15. ('=').
  16. The return value consists of two elements: the first is a list of
  17. (option, value) pairs; the second is the list of program arguments
  18. left after the option list was stripped (this is a trailing slice
  19. of the first argument). Each option-and-value pair returned has
  20. the option as its first element, prefixed with a hyphen (e.g.,
  21. '-x'), and the option argument as its second element, or an empty
  22. string if the option has no argument. The options occur in the
  23. list in the same order in which they were found, thus allowing
  24. multiple occurrences. Long and short options may be mixed.
  25. """
  26. opts = []
  27. if type(longopts) == type(""):
  28. longopts = [longopts]
  29. else:
  30. longopts = list(longopts)
  31. if args and args[0].startswith('-') and args[0] != '-':
  32. if args[0] == '--':
  33. args = args[1:]
  34. if args[0].startswith('--'):
  35. opts, args = getopt.do_longs(opts, args[0][2:], longopts, args[1:])
  36. else:
  37. opts, args = getopt.do_shorts(opts, args[0][1:], shortopts, args[1:])
  38. return opts, args
  39. else:
  40. return None, args