Giter Site home page Giter Site logo

lua_cliargs's Introduction

cliargs

travis-ci status

cliargs is a command-line argument parser for Lua. It supports 4 types of arguments:

  1. required arguments
  2. optional arguments with different notations: -short-key VALUE and/or --expanded-key=VALUE
  3. optional "flag" arguments (on/off options) with notations: -short-key and/or --expanded-key
  4. a single optional "splat" argument which can be repeated (must be the last argument)

Optional arguments can have default values (strings), flags always default to 'true'.

Usage Example

See example.lua for an example on how to use the parser. Try it with the following sample command lines;

example.lua --help
example.lua -o myfile -d --compress=gzip inputfile
example.lua --__DUMP__ -o myfile -d --compress=gzip inputfile

Accessing arguments

All types of arguments must specify a key. In the case of required arguments, the keys are only used in the help listings. However, for optional arguments, they are mandatory (either --key or --extended-key must be specified, the other is optional).

The parse() method will parse the command line and return a table with results. Accessing argument or option values in this table can be done using the key with the leading dashes omitted (- or --). When defining an option (or a flag) , you can access it using either key or expanded-key; they'll both be defined.

Help listings --help

A help listing will be automatically generated and accessed using the --help argument. You can also force its display in the code using cli:print_help().

This is the result for our example:

Usage: cli_example.lua [OPTIONS]  INPUT  [OUTPUT-1 [OUTPUT-2 [...]]]

ARGUMENTS:
  INPUT                 path to the input file (required)
  OUTPUT                multiple output paths (optional, default:
                        /dev/stdout)

OPTIONS:
  -c, --compress=FILTER the filter to use for compressing output: gzip,
                        lzma, bzip2, or none (default: gzip)
  -o FILE               path to output file (default: /dev/stdout)
  -d                    script will run in DEBUG mode
  -v, --version         prints the program's version and exits
  --verbose             the script output will be very verbose

Validations

Runtime argument validation

From a parsing point of view, there are 3 cases that need to be handled which are outlined below. If I missed something, please open a ticket!

Missing a required argument

>> lua example.lua
cli_example.lua: error: bad number of arguments; 1-4 argument(s) must be specified, not 0; re-run with --help for usage.

Missing value for an optional argument

>> example.lua --compress inputfile
cli_example.lua: error: option --compress requires a value to be set; re-run with --help for usage.

Unknown arguments

>> example.lua -f inputfile
cli_example.lua: error: unknown/bad flag; -f; re-run with --help for usage.

Some sanity guards

In the following cases, cliargs will report an error to you and terminate the running script:

  1. flag options can not accept a value. For example: cli:add_flag('-v VERSION') will return an error
  2. duplicate keys are not allowed: defining two options with the key --input will be rejected

Tests

Running test specs is done using busted. You can install it using LuaRocks, and then just call it with the spec folder:

luarocks install busted
cd /path/to/lua_cliargs/
busted spec

Contributions

If you come across a bug and you'd like to patch it, please fork the repository, commit your patch, and request a pull.

For collaborators

To deploy a new version, you must bump the rockspec and do a few things:

  1. rename the rockspec to reflect the new version (by incrementing the minor version, for example)
  2. edit the rockspec to point to the tarball that contains the new version (which also must follow the same naming convention)
  3. bump the version stored in the variable cli._VERSION in the bottom of the script src/cliargs.lua
  4. create the tarball using the helper bash script tarballs/create_tarball.sh: invoke it with two parameters: the MAJOR version and the MINOR one, ie: ./create_tarball.sh 1 4 to create a 1.4 versioned tarball of the repository
  5. add the new tarball to the Downloads of the repository so Luarocks can find it

Thanks to

Many thanks to everyone who reported bugs, provided fixes, and added entirely new features:

  1. Thijs Schreijer
  2. Jack Lawson
  3. Robert Andrew Ditthardt

If I missed you, don't hesitate to update this file or just email me.

Reference

A function reference was generated using LunaDoc which can be found here.

Changelog

Changes in 2.0.0 from 1.x.x

  1. added the 'splat' argument, an optional repetitive argument for which a maximum number of occurrences can be set
  2. removed the reference, arguments are now solely returned by their key/expanded-key (BREAKING!)
  3. removed object overhead and the new() method as the library will only be used once on program start-up (BREAKING!)
  4. after parsing completed successfully, the library will effectively delete itself to free resources (BREAKING!)
  5. option/flag is now allowed with only an expanded-key defined
  6. Debug aid implemented; adding a first option --__DUMP__, will dump the results of parsing the command line. Especially for testing how to use the commandline with arguments containing spaces either quoted or not.
  7. the print_usage() and print_help() now have a 'noprint' parameter that will not print the message, but return it as an error string (nil + errmsg)

License

The code is released under the MIT terms. Feel free to use it in both open and closed software as you please.

Copyright (c) 2011-2012 Ahmad Amireh

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

lua_cliargs's People

Contributors

ajacksified avatar amireh avatar doriangray avatar tieske avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.