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.

173 lines
3.8 KiB

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