|
|
(One intermediate revision by the same user not shown) |
Line 1: |
Line 1: |
| local Arg = {}
| |
| Arg.__index = Arg
| |
|
| |
| function Arg.new( value )
| |
| return setmetatable( {
| |
| __value = value
| |
| }, Arg )
| |
| end
| |
|
| |
| -- get the Arg node's value, if it has one
| |
| function Arg:val()
| |
| return self.__value
| |
| end
| |
|
| |
| -- get the value at the given key of this Arg node
| |
| function Arg:get( key )
| |
| return self[key] and self[key]:val()
| |
| end
| |
|
| |
| -- get the values of the Arg node's direct children
| |
| -- this is used to re-flatten/unwrap a particular piece of an Arg tree
| |
| function Arg:values()
| |
| local vals = {}
| |
| for k, v in pairs( self ) do
| |
| if type( v ) == 'table' and v.val then
| |
| vals[k] = v:val()
| |
| end
| |
| end
| |
|
| |
| return vals
| |
| end
| |
|
| |
| local p = {} | | local p = {} |
|
| |
| function insertInto( tbl, key, value )
| |
| local first, rest = string.match( key, '^(.-)_(.*)$' )
| |
| local myKey = first or key
| |
| myKey = tonumber( myKey ) or myKey
| |
|
| |
| -- make sure the node exists
| |
| if not tbl[myKey] then tbl[myKey] = Arg.new() end
| |
|
| |
| if first then -- this is an internal node so insert children
| |
| insertInto( tbl[myKey], rest, value )
| |
| else -- this is a leaf so set value
| |
| tbl[myKey].__value = value
| |
| end
| |
| end
| |
|
| |
| function p.parse( args )
| |
| local parsedArgs = Arg.new()
| |
| for k, v in pairs( args ) do
| |
| insertInto( parsedArgs, k, v )
| |
| end
| |
|
| |
| return parsedArgs
| |
| end
| |
|
| |
|
| -- place the value in the right spot in the arg tree, determined by underscores in the key | | -- place the value in the right spot in the arg tree, determined by underscores in the key |
Line 83: |
Line 27: |
|
| |
|
| return expandedArgs | | return expandedArgs |
| | end |
| | |
| | -- coerce the given node into a table, in case it is a standalone value |
| | function p.getTable( val ) |
| | return type( val ) == 'table' and val or { __value = val } |
| end | | end |
|
| |
|
Line 92: |
Line 41: |
| -- expand the arg tree from the given frame and its parent | | -- expand the arg tree from the given frame and its parent |
| function p.fromFrame( frame ) | | function p.fromFrame( frame ) |
| local parsedArgs = Arg.new() | | local parsedArgs = {} |
|
| |
|
| for k, v in pairs( frame.args ) do | | for k, v in pairs( frame.args ) do |