50b33e105cf1b86dc4642b0a64c660e7535407c6
[project/luci.git] / libs / lpk / luasrc / lpk / util.lua
1 module("luci.lpk.util", package.seeall)
2
3 function getopt( arg, options )
4         options = options or ""
5         local tab = {}
6         local args = {}
7         for k, v in ipairs(arg) do
8                 if v:sub(1, 2) == "--" then
9                         local x = v:find( "=", 1, true )
10                         if x then
11                                 tab[ v:sub( 3, x-1 ) ] = v:sub( x+1 )
12                         else 
13                             tab[ v:sub( 3 ) ] = true
14                         end
15                 elseif v:sub( 1, 1 ) == "-" then
16                         local y = 2
17                         local l = #v
18                         local jopt
19                         while ( y <= l ) do
20                                 jopt = v:sub( y, y )
21                                 if options:find( jopt, 1, true ) then
22                                         if y < l then
23                                                 tab[ jopt ] = v:sub( y+1 )
24                                                 y = l
25                                         else
26                                                 tab[ jopt ] = arg[ k + 1 ]
27                                                 arg[ k + 1 ] = ""
28                                         end
29                                 else
30                                         tab[ jopt ] = true
31                                 end
32                                 y = y + 1
33                         end
34             elseif #v > 0 then
35                 table.insert(args, v)
36             end
37         end
38         return tab, args
39 end