Quipex

Disk/mem/redworks/programs/cc-get-bin/params.lua

Apr 23rd, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. --[[
  2. Params version 1.0.0
  3.  
  4. Examples:
  5. If you were to run:
  6. cc-get install program -v 1.2 -f
  7.  
  8. With this in the cc-get program:
  9. opts = parse_params({...}, {version = '-v *([^ ]+)', flag = '-f'})
  10.  
  11. Opts would be:
  12. opts = {
  13. 'install',
  14. 'program',
  15. flag = true,
  16. version = '1.2'
  17. }
  18. ]]--
  19. function parse_params(params, patterns)
  20. params = table.concat(params, ' ')
  21. local result = {}
  22. local left = params
  23.  
  24. for k, pattern in pairs(patterns) do
  25. for x in string.gmatch(params, pattern) do
  26. if x == pattern then
  27. result[ k ] = true
  28. else
  29. result[ k ] = x
  30. end
  31. left = left:gsub(pattern, '')
  32. end
  33. end
  34.  
  35. -- Remove extra spaces
  36. left = left:gsub('%s+', ' ')
  37.  
  38. -- Trim before and after
  39. left = left:gsub('^%s*(.+)%s*$', '%1')
  40.  
  41. for param in left:gmatch('[^ ]+') do
  42. table.insert( result, param )
  43. end
  44.  
  45. return result
  46. end
Advertisement
Add Comment
Please, Sign In to add comment