OpenSDE Framework (without history before r20070)
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.

172 lines
3.8 KiB

  1. -- --- T2-COPYRIGHT-NOTE-BEGIN ---
  2. -- This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. --
  4. -- T2 SDE: misc/lua/sde/getopt.lua
  5. -- Copyright (C) 2005 - 2006 The T2 SDE Project
  6. -- Copyright (C) 2005 - 2006 Juergen "George" Sawinski
  7. --
  8. -- More information can be found in the files COPYING and README.
  9. --
  10. -- This program is free software; you can redistribute it and/or modify
  11. -- it under the terms of the GNU General Public License as published by
  12. -- the Free Software Foundation; version 2 of the License. A copy of the
  13. -- GNU General Public License can be found in the file COPYING.
  14. -- --- T2-COPYRIGHT-NOTE-END ---
  15. -- TODO:
  16. -- - add boolean option argument types
  17. -- - maybe other types needed ?
  18. -- DESCRIPTION:
  19. -- opts,args,err = getopt(arg-list, option-definition)
  20. -- Parses the "arg-list" according to "option-definition" and
  21. -- returns the parsed options "opts", additional arguments "args" and
  22. -- possibly an error.
  23. --
  24. -- Option definition format:
  25. -- key Option key (e.g. key = "help")
  26. -- option String or list of options (e.g. option = {"-h", "--help"})
  27. -- default Default value (e.g. default = false)
  28. -- argument This option has no argument (ARG.none), a required
  29. -- argument (ARG.required) or an optional argument (ARG.optional)
  30. -- flag The option is flagged:
  31. -- number (OPT.number)
  32. -- incremental (OPT.incremental)
  33. -- Example: see below
  34. ARG = {
  35. none = 0,
  36. required = 1,
  37. optional = 2
  38. }
  39. OPT = {
  40. none = 0,
  41. string = 0,
  42. number = 1,
  43. --boolean = 2,
  44. incremental = 10,
  45. }
  46. function getopt(args,def)
  47. local opt = {}
  48. local arg = {}
  49. local nargs = table.getn(args)
  50. -- first pass: defaults
  51. for _,d in pairs(def) do
  52. if d.default ~= nil then
  53. opt[d.key] = d.default
  54. end
  55. end
  56. -- Second pass: parse arguments
  57. local i=1
  58. repeat
  59. for _,d in pairs(def) do
  60. local found = false
  61. -- FIXME is there some shorter version for this:
  62. if type(d.option) == "string" then
  63. if d.option == args[i] then found=true end
  64. else
  65. for k=1,table.getn(d.option) do
  66. if d.option[k] == args[i] then
  67. found = true
  68. end
  69. end
  70. end
  71. -- process option and arguments
  72. if found then
  73. local val = true
  74. local nextisopt = false
  75. local havearg = false
  76. -- check if next is an argument (not option)
  77. if args[i+1] and string.sub(args[i+1],1,1) == "-" then
  78. nextisopt = true
  79. end
  80. -- option argument ?
  81. if nextisopt and d.argument == ARG.required then
  82. return nil, nil, "Missing option argument for `" .. args[i] .. "'"
  83. end
  84. if not nextisopt and
  85. (d.argument == ARG.required
  86. or d.argument == ARG.optional) then
  87. args[i] = nil
  88. i = i+1
  89. val = args[i]
  90. havearg = true
  91. end
  92. -- flags ?
  93. if d.flag then
  94. if d.flag == OPT.number then
  95. val = tonumber(val)
  96. elseif d.flag == OPT.incremental then
  97. val = opt[d.key] + 1
  98. end
  99. end
  100. -- finally assign value
  101. opt[d.key] = val
  102. -- remove key from list (see third pass)
  103. args[i] = nil
  104. end
  105. end
  106. i=i+1
  107. until i>nargs
  108. -- Third pass: check option, sort arguments
  109. for i=1,nargs do
  110. if args[i] then
  111. if string.sub(args[i],1,1) == "-" then
  112. return nil, nil, "Unrecognized option `" .. args[i] .. "'"
  113. else
  114. table.insert(arg, args[i])
  115. end
  116. end
  117. end
  118. -- done
  119. return opt,arg
  120. end
  121. -- EXAMPLE
  122. if false then
  123. opt,arg,err = getopt(arg, {
  124. {
  125. key = "help",
  126. option = { "-h", "-help", "--help" },
  127. },
  128. {
  129. key = "verbose",
  130. option = { "-v", "-verbose", "--verbose" },
  131. default = 0,
  132. flag = OPT.incremental
  133. },
  134. })
  135. if not opt then
  136. print "ERR:"
  137. print(err)
  138. os.exit(-1)
  139. end
  140. print "OPTS:"
  141. for k,v in pairs(opt) do
  142. print("",k,v)
  143. end
  144. print "ARGS:"
  145. for k,v in pairs(arg) do
  146. print("",k,v)
  147. end
  148. end