Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
api:shell [2016/07/04 17:27]
payonel [Shell API]
api:shell [2018/03/02 10:35]
jayjader [Shell API] Period -> Comma in last `shell.parse()` example result
Line 28: Line 28:
 - `shell.parse(...):​ table, table`  ​ - `shell.parse(...):​ table, table`  ​
   Utility methods intended for programs to parse their arguments. Will return two tables, the first one containing any "​normal"​ parameters, the second containing "​options"​. Options are indicated by a leading `-`, and all options must only be a single character, since multiple characters following a single `-` will be interpreted as multiple options. Options specified with 2 dashes are not split and can have multiple letters. Also, 2-dash options can be given values by using an equal sign. The following examples all assume the script `program` parses the options using `local args, ops = shell.parse(...)`.   Utility methods intended for programs to parse their arguments. Will return two tables, the first one containing any "​normal"​ parameters, the second containing "​options"​. Options are indicated by a leading `-`, and all options must only be a single character, since multiple characters following a single `-` will be interpreted as multiple options. Options specified with 2 dashes are not split and can have multiple letters. Also, 2-dash options can be given values by using an equal sign. The following examples all assume the script `program` parses the options using `local args, ops = shell.parse(...)`.
 +
 +  `program`
 +
 +  # `args` is `{}`
 +
 +  # `ops` is `{}`.
  
   `program -abC -d arg1 arg2`   `program -abC -d arg1 arg2`
-  `args` is `{"​arg1",​ "​arg2"​}`,​ and `options` is `{a=true,​b=true,​C=true,​d=true}`. 
  
-  `program ​arg1 -abC --dog arg2` +  ​`args` is `{"arg1", "​arg2"​}` 
-  `args` is `{"​arg1",​ "​arg2"​}`, and `options` is `{a=true,​b=true,​C=true,​dog=true}`.+ 
 +  # `op` is `{a=true,​b=true,​C=true,​d=true}`. 
 + 
 +  `program ​-abC --dog arg1 arg2` 
 + 
 +  ​`args` is `{"​arg1",​ "​arg2"​}` 
 + 
 +  # `ops` is `{a=true,​b=true,​C=true,​dog=true}`. 
 + 
 +  `program -abC --dog=foo arg1 arg2` 
 + 
 +  # `args` is `{"​arg1",​ "​arg2"​}` 
 + 
 +  # `ops` is `{a=true,​b=true,​C=true,​dog="​foo"​}`. 
 + 
 +  On this next example, notice the single dash before `dog`, this causes all of the token to be parsed as single chars. 
 + 
 +  `program -abC -dog=foo arg1 arg2`
  
-  ​`program -abC arg1 -dog=true arg2` +  ​`args` is `{"​arg1",​ "​arg2"​}`
-  ​`args` is `{"​arg1",​ "​arg2"​}`, and `options` is `{a=true,​b=true,​C=true,​dog="​true"​}`.+
  
-  `program -ab arg1 -C -dog=thing arg2` +  ​`ops` is `{a=true,​b=true,​C=true,​d=true,​g=true,​["="]=true,​f=true,​o=true}`.
-  `args` is `{"​arg1",​ "​arg2"​}`,​ and `options` is `{a=true,​b=true,​C=true,​dog="thing"}`.+