This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Admicos's profile picture

getopt - Easy option parsing

Started by Admicos, 12 August 2016 - 06:20 AM
Admicos #1
Posted 12 August 2016 - 08:20 AM
Getopt 1.0.0 – Easy command line option parser

Usage:

table or nil getopt.init(string name, string programDescription, table optionsTable, table args)
returns table (possibly empty) if there was arguments, returns nil if asked for help

Example:

local tbl = getopt.init("colortext", "Prints text with color", optionsTable, { ... })
if tbl ~= nil then
	--Your code
end


Option table (second argument for getopt.init) format:

local optionsTable = {
	["color"] = {"Should it be colored", "c", nil},
	["number"] = {"How many times should it print", "n", "num"},
}

Produces:
	Argument "--color", can be shortened as "-c", boolean (exists or not)
	Argument "--number", can be shortened as "-n", has value "num"

They wll be accessible as:
	tbl["color"] #for argument "--color"
	tbl["number"] #for argument "--number"

shortened options will still return the same variables.

Please note that "--help" or "-h" is a hardcoded option that automatically generates help (and returns nil on getopt.init)


Extra arguments without options:

Example:
	program.lua a b c d
will return:
	tbl["opt-1 to 4"] = "a to d" #opt meaning optional


Downloads:
[indent=1]os.loadAPI-able version: (Examples at the end)[/indent]
[indent=2]pastebin: aLcLbJsA[/indent]
[indent=2]admipaste: 7439f8ffabdffc4[/indent]
[indent=2]packman: admicos/getopt[/indent]
[indent=1]dofile/inline-able/luapp version:[/indent]
[indent=2]pastebin: VyGBMX1d[/indent]
[indent=2]packman: admicos/luapp-includes-getopt[/indent]
[indent=2](installs to /etc/luapp/include/getopt.inc which can be used like: –pp:include getopt.inc inside luapp.)[/indent]
Edited on 06 November 2016 - 07:46 PM
Admicos #2
Posted 21 August 2016 - 07:16 PM
The os.loadAPI version had a bug in the help function, this is fixed by adding a "name" argument to getopt.init (getopt.init(name, desc, …))
the updated version is currently available at the pastebin and packman repo.

the inline-able/dofile version doesn't need anything and will not be updated.
rdalkire #3
Posted 09 September 2016 - 05:22 AM
I've been checking it out; so far so good however: Please specify a license so readers are actually allowed to use it. I want to use it, modify it and include it in my own repo, while attributing and sourcing it to you of course. Would that be OK? Otherwise I'll have to use some other option parser because I like all my code to be legit.

See: http://www.computercraft.info/forums2/index.php?/topic/26096-short-licensing-your-code/
Admicos #4
Posted 09 September 2016 - 05:58 AM
I've been checking it out; so far so good however: Please specify a license so readers are actually allowed to use it. I want to use it, modify it and include it in my own repo, while attributing and sourcing it to you of course. Would that be OK? Otherwise I'll have to use some other option parser because I like all my code to be legit.

See: http://www.computerc...sing-your-code/

I had a "Everything i do is licensed with the MIT license unless said otherwise" or something like that in my signature before, but i don't remember why i deleted it. But yeah, It's licensed with the MIT License. (just added it)
Edited on 09 September 2016 - 04:07 AM
rdalkire #5
Posted 09 September 2016 - 03:07 PM
Thank you Admicos! I think I'm going to be using this one within multiple scripts
NotSwedishFish #6
Posted 10 September 2016 - 12:36 AM
For anyone who's interested in a more Pythonic options parsing module, take a look at: https://github.com/d...ic/optparse.lua

It requires a bit of patching (io.stdout needs to be assigned), but it's a different approach for those who've done a lot with Python scripts.

Some useful features are:
- generates help page
- generates defaults
- custom action
Edited on 09 September 2016 - 10:37 PM
Admicos #7
Posted 05 November 2016 - 06:42 PM
Using luapp? You can easily include getopt in your project just by installing the admicos/luapp-includes-getopt packman package and putting –pp:include getopt.inc in the top of your file

(needs the latest version of luapp, so update that while you're at it)
Admicos #8
Posted 06 November 2016 - 08:47 PM
the inline-able version has been updated to include the "name" argument that the os.loadAPI version had. (so it can be compatible with itself)