项目作者: libre-man

项目描述 :
Unix-style command line options parser
高级语言: Common Lisp
项目地址: git://github.com/libre-man/unix-opts.git
创建时间: 2015-02-22T16:18:17Z
项目社区:https://github.com/libre-man/unix-opts

开源协议:MIT License

下载


Unix-style command line options parser

License MIT
Build Status
Quicklisp

This is a minimalistic parser of command line options. The main advantage of
the library is the ability to concisely define command line options once and
then use this definition for parsing and extraction of command line
arguments, as well as printing description of command line options (you get
--help for free). This way you don’t need to repeat yourself. Also,
unix-opts doesn’t depend on anything and allows to precisely control
behavior of the parser via Common Lisp restarts.

Inspired by Haskell’s optparse-applicative and Python’s argparse.

It is portable accross implementations.

Installation

Copy files of this library in any place where ASDF can find them. Then you
can use it in system definitions and ASDF will take care of the rest.

Via Quicklisp (recommended):

  1. (ql:quickload "unix-opts")

Now you can also use its shorter nickname opts.

Functions

  1. option condition

Take a condition condition (unknown-option, missing-arg, or
arg-parser-failed) and return a string representing the option in
question.


  1. raw-arg condition

Take a condition of type arg-parser-failed and return the raw argument
string.


  1. define-opts &rest descriptions

Define command line options. Arguments of this macro must be plists
containing various parameters. Here we enumerate all allowed parameters:

  • :name—keyword that will be included in list returned by get-opts
    function if actual option is supplied by user.

  • :description—description of the option (it will be used in describe
    function). This argument is optional, but it’s recommended to supply it.

  • :short—single character, short variant of the option. You may omit this
    argument if you supply :long variant of option.

  • :long—string, long variant of option. You may omit this argument if you
    supply :short variant of option.

  • :arg-parser—if actual option must take an argument, supply this
    argument, it must be a function that takes a string and parses it.

  • :meta-var—if actual option requires an argument, this is how it will be
    printed in option description.

  • :required—whether the option is required. This only makes sense if the
    option takes an argument.

  • :default—the default value used if the option was not found. This can either
    be a function (which will be called to generate the default value) or a
    literal value. This option cannot be combined with :required. The default
    value will not be provided to the arg-parser.


  1. argv

Return a list of the program’s arguments, including the command used to
execute the program as the first element of the list. Portable across
implementations.


  1. get-opts &optional options

Parse command line options. If options is given, it should be a list to
parse. If it’s not given, the function will use the argv function to get
the list of command line arguments.

Returns two values:

  • a list that contains keywords associated with command line options with
    define-opts macro, and
  • a list of free arguments.

If some option requires an argument, you can use getf to test the presence
of the option and get its argument if the option is present.

The parser may signal various conditions. Let’s list them all specifying
which restarts are available for every condition, and what kind of
information the programmer can extract from the conditions.

  • unknown-option is thrown when the parser encounters an unknown (not
    previously defined with define-opts) option. Use the option reader to
    get the name of the option (string). Available restarts: use-value
    (substitute the option and try again), skip-option (ignore the option).

  • missing-arg is thrown when some option wants an argument, but there is
    no such argument given. Use the option reader to get the name of the
    option (string). Available restarts: use-value (supplied value will be
    used), skip-option (ignore the option).

  • arg-parser-failed is thrown when some option wants an argument, it’s
    given but cannot be parsed by argument parser. Use the option reader to
    get name of the option (string) and raw-arg to get raw string
    representing the argument before parsing. Available restarts: use-value
    (supplied value will be used), skip-option (ignore the option),
    reparse-arg (supplied string will be parsed instead).

  • missing-required-option is thrown when a required option cannot be
    found. Use the missing-options reader to get all option objects that are
    missing. Available restarts: use-value (supplied list of values will be
    used), skip-option (ignore all these options, effectively binding them
    to nil)

  1. describe &key prefix suffix usage-of args stream

Return a string describing all the options of the program that were defined
with the previous define-opts macro. You can supply prefix and suffix
arguments that will be printed before and after the options respectively. If
usage-of is supplied, it should be a string, the name of the program for
an “Usage: “ section. This section is only printed if this name is given. If
your program takes arguments (apart from options), you can specify how to
print them in “Usage: “ section with an args option (should be a string
designator). Output goes to stream (default value is *standard-output*).


  1. exit &optional (status 0)

Exit the program returning status.

Example

Go to the example directory. Now, you can use example.lisp file to see
if unix-opts is cool enough for you to use. SBCL users can use
example.sh file.

Take a look at example.lisp and you will see that the library is pretty
sexy! Basically, we have defined all the options just like this:

  1. (opts:define-opts
  2. (:name :help
  3. :description "print this help text"
  4. :short #\h
  5. :long "help")
  6. (:name :verbose
  7. :description "verbose output"
  8. :short #\v
  9. :long "verbose")
  10. (:name :level
  11. :description "the program will run on LEVEL level"
  12. :short #\l
  13. :long "level"
  14. :required t
  15. :arg-parser #'parse-integer
  16. :meta-var "LEVEL")
  17. (:name :output
  18. :description "redirect output to file FILE"
  19. :short #\o
  20. :long "output"
  21. :arg-parser #'identity
  22. :meta-var "FILE"))

and we read them with (opts:get-opts) which returns two values, a list of
parsed options and the remaining arguments, so:

  1. (multiple-value-bind (options free-args)
  2. (opts:get-opts)
  3. (if (getf options :verbose)
  4. ...

See the example for helpers and how to handle malformed or incomplete arguments.

And here is some action:

  1. $ sh example.sh --help
  2. exampleprogram to demonstrate unix-opts library
  3. Usage: example.sh [-h|--help] [-v|--verbose] [-l|--level LEVEL]
  4. [-o|--output FILE] [FREE-ARGS]
  5. Available options:
  6. -h, --help print this help text
  7. -v, --verbose verbose output
  8. -l, --level LEVEL the program will run on LEVEL level
  9. -o, --output FILE redirect output to file FILE
  10. so that's how it works…
  11. free args:
  12. $ sh example.sh --level 1 -v file1.txt file2.txt
  13. OK, running in verbose mode…
  14. I see you've supplied level option, you want 1 level!
  15. free args: file1.txt, file2.txt
  16. $ sh example.sh --level 10 --output foo.txt bar.txt
  17. I see you've supplied level option, you want 10 level!
  18. I see you want to output the stuff to "foo.txt"!
  19. free args: bar.txt
  20. $ sh example.sh --level kitty foo.txt
  21. fatal: cannot parse "kitty" as argument of "--level"
  22. free args:
  23. $ sh example.sh --hoola-boola noola.txt
  24. warning: "--hoola-boola" option is unknown!
  25. fatal: missing required options: "--level"
  26. $ sh example.sh -vgl=10
  27. warning: "-g" option is unknown!
  28. OK, running in verbose mode…
  29. I see you've supplied level option, you want 10 level!
  30. free args:

License

Copyright © 2015–2018 Mark Karpov

Distributed under MIT License.