Advertisement
kd2bwzgen

getopt

Aug 20th, 2017
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. function getopt( arg, options )
  2. local tab = {}
  3. for k, v in ipairs(arg) do
  4. if string.sub( v, 1, 2) == "--" then
  5. local x = string.find( v, "=", 1, true )
  6. if x then tab[ string.sub( v, 3, x-1 ) ] = string.sub( v, x+1 )
  7. else tab[ string.sub( v, 3 ) ] = true
  8. end
  9. elseif string.sub( v, 1, 1 ) == "-" then
  10. local y = 2
  11. local l = string.len(v)
  12. local jopt
  13. while ( y <= l ) do
  14. jopt = string.sub( v, y, y )
  15. if string.find( options, jopt, 1, true ) then
  16. if y < l then
  17. tab[ jopt ] = string.sub( v, y+1 )
  18. y = l
  19. else
  20. tab[ jopt ] = arg[ k + 1 ]
  21. end
  22. else
  23. tab[ jopt ] = true
  24. end
  25. y = y + 1
  26. end
  27. end
  28. end
  29. return tab
  30. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement