Jump to content




Argser - parsing program arguments with ease

api

5 replies to this topic

#1 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 20 June 2016 - 02:39 PM

Argser

pastebin get 3jkDVune argser


This is an API I wrote to help with parsing program arguments. It's made to process such things like switches (myprogram -a -b valueforb anothervalueforb) and named arguments (myprogram --named1=valuefor1 --named2), as well as simple "normal" arguments (myprogram arg1 arg2 arg3). It can also automatically convert the passed arguments to certain Lua types and preprocess them using custom functions.

Loading the API

To load the API you can use dofile() function (os.loadAPI() will not work):
local argser = dofile("argser")

Documentation

The API returns a single function to which you pass the program's arguments, setup all the switches, named arguments, value types, defaults and aliases, and parse the arguments. The parsed arguments are then returned in a table which you can then use to get the arguments.

Example

Parser methods and variables

Changelog

Licensing
You can use and edit this API as much as you want. I only ask that you don't start redistributing it as if it was your own API.

Edited by MKlegoman357, 21 June 2016 - 07:57 PM.


#2 Emma

  • Members
  • 216 posts
  • Locationtmpim

Posted 21 June 2016 - 12:29 AM

Nice! I'm always too lazy to implement actual terminal arguments like this. :P

#3 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 21 June 2016 - 11:41 AM

View PostIncinirate, on 21 June 2016 - 12:29 AM, said:

Nice! I'm always too lazy to implement actual terminal arguments like this. :P

That's the reason I made this. Got tired of writing the same argument handling logic in every program.




I updated the API. There was a bug where if the API was given an undefined named argument it would crash. And yes, it will parse any undefined arguments. By default switches can have no values and named arguments can optionally have a string value. Running this program:

print(textutils.serialize(
  dofile("argser")(...):parse().args
))

...using these arguments:

1 bar -switch --named1 --named2=foo 3

...would print this onto the screen:

{
  "1",
  "bar",
  "3",
  switch = true,
  named1 = true,
  named2 = "foo",
}


#4 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 21 June 2016 - 05:25 PM

Very nice! :D
My suggestion would be that you make some small code changes so people can copy+paste this into their program (I know this is very simple, but it's nice :P), so they don't have to have an external file.

#5 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 21 June 2016 - 07:34 PM

View PostCrazedProgrammer, on 21 June 2016 - 05:25 PM, said:

Very nice! :D
My suggestion would be that you make some small code changes so people can copy+paste this into their program (I know this is very simple, but it's nice :P), so they don't have to have an external file.

I was actually thinking of doing this, but was too lazy to actually do something myself. You know, literally change two lines in this API :P. But I guess that's what makes a good programmer - being lazy :D.




Anyway, I updated the code so now it only contains a single function which you can copy-paste into your own program. The function is called "argser()" by the way and it does exactly the same as if you were to load the API like this:

local argser = dofile("argser")


#6 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 21 June 2016 - 09:48 PM

Nice!
Thanks for implementing my suggestion!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users