[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Command Line Parsing?
- From: "Gary V. Vaughan" <gary@...>
- Date: Mon, 11 Apr 2016 23:14:32 +0100
> On 11 Apr 2016, at 21:50, Petite Abeille <petite.abeille@gmail.com> wrote:
>
>
>> On Apr 11, 2016, at 10:38 PM, Coda Highland <chighland@gmail.com> wrote:
>>
>> So the question really was, what style of command line parsing is
>> actually desired?
>
> Ultimately, a combination of short options, long options, and sub commands ala svn.
>
> So, I guess, something along the lines of docopt in term of expressiveness: http://docopt.org
Here’s mine, optparse:
https://github.com/gvvaughan/optparse
https://gvvaughan.github.io/optparse
The idea is that you write the help text output by hand, and feed that to
optparse, which returns an option parser. When you run that option parser
against the command line arguments it raises errors for unrecognised or
otherwise malformed options, and returns an opts table set according to
what arguments it processed.
It’s reasonably configurable too if you have any unusual requirements. But
often enough, you only need the help text string.
From README.md:
The optparse package returns a parser factory when loaded:
```lua
local OptionParser = require "optparse"
local help = [[
parseme (optparse spec) 0α1
Copyright © 2016 Gary V. Vaughan
This test program comes with ABSOLUTELY NO WARRANTY.
Usage: parseme [<options>] <file>...
Banner text.
Long description.
Options:
-h, --help display this help, then exit
--version display version information, then exit
-b a short option with no long option
--long a long option with no short option
--another-long a long option with internal hypen
--true a Lua keyword as an option name
-v, --verbose a combined short and long option
-n, --dryrun, --dry-run several spellings of the same option
-u, --name=USER require an argument
-o, --output=[FILE] accept an optional argument
-- end of options
Footer text.
Please report bugs at <http://github.com/gvvaughan/optparse/issues>.
]]
local parser = OptionParser (help)
local arg, opts = parser:parse (_G.arg)
...
```
HTH,
Gary