diff options
Diffstat (limited to 'tests/argtable.lua')
-rw-r--r-- | tests/argtable.lua | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/argtable.lua b/tests/argtable.lua new file mode 100644 index 0000000..5ed5d4e --- /dev/null +++ b/tests/argtable.lua | |||
@@ -0,0 +1,38 @@ | |||
1 | -- | ||
2 | -- ARGTABLE.LUA Copyright (c) 2007, Asko Kauppi <akauppi@gmail.com> | ||
3 | -- | ||
4 | -- Command line parameter parsing | ||
5 | -- | ||
6 | -- NOTE: Wouldn't hurt having such a service built-in to Lua...? :P | ||
7 | -- | ||
8 | |||
9 | local m= {} | ||
10 | |||
11 | -- tbl= argtable(...) | ||
12 | -- | ||
13 | -- Returns a table with 1..N indices being 'value' parameters, and any | ||
14 | -- "-flag[=xxx]" or "--flag[=xxx]" parameters set to { flag=xxx/true }. | ||
15 | -- | ||
16 | -- In other words, makes handling command line parameters simple. :) | ||
17 | -- | ||
18 | -- 15 --> { 15 } | ||
19 | -- -20 --> { -20 } | ||
20 | -- -a --> { ['a']=true } | ||
21 | -- --some=15 --> { ['some']=15 } | ||
22 | -- --more=big --> { ['more']='big' } | ||
23 | -- | ||
24 | function m.argtable(...) | ||
25 | local ret= {} | ||
26 | for i=1,select('#',...) do | ||
27 | local v= select(i,...) | ||
28 | local flag,val= string.match( v, "^%-+([^=]+)%=?(.*)" ) | ||
29 | if flag and not tonumber(v) then | ||
30 | ret[flag]= (val=="") and true or tonumber(val) or val | ||
31 | else | ||
32 | table.insert( ret, v ) -- 1..N | ||
33 | end | ||
34 | end | ||
35 | return ret | ||
36 | end | ||
37 | |||
38 | return m | ||