Miscellaneous

Argument Parser

argument-parser

:super = ** propertied-object**
:slots flaglst docstring parsed-p

command line argument parser.

:init &key prog description epilog (add-help t)

instantiates argument-parser class object from the name of the program, description text to display before the argument help and text to display after them as an epilog.

:add-argument flags &key (action :store) const default choices check read required help dest

defines the command-line argument to be parsed.

flag specify the options strings such as “–foo” or “-b”. It also takes list values i.e. ’(“–bar” “-b”).

action defines the behavior when this argument is passed to the program. Current supported actions are :store, :store-true, :store-false, :store-const, :append, and custom functions. help defines the help document text. Most of the arguments are designed according to https://docs.python.org/3/library/argparse.html.

:parse-args **

parses command line argument from lisp::*eustop-argument*. This method must be called before sending an argument name method to argument-parser instance.

The following shows an example of argument-parser.

(require :argparse "argparse.l")

(defvar argparse (instance argparse:argument-parser :init
                           :description "Program Description (optional)"))
(send argparse :add-argument "--foo" :default 10 :read t
      :help "the foo description")
(send argparse :add-argument '("--bar" "-b") :action :store-true
      :help "the bar description")

(send argparse :parse-args)
(format t "foo: ~A~%" (send argparse :foo))
(format t "bar: ~A~%" (send argparse :bar))
(exit)

The following is an output of the example program above.

$ eus argparse-example.l
foo: 10
bar: t
$ eus argparse-example.l --foo=100 --bar
foo: 100
bar: t
$ eus argparse-example.l -h
usage: [-h] [--foo=FOO] [-b]

Program Description (optional)

optional arguments:
  -h, --help    show this help message and exit
  --foo=FOO the foo description (default: 10)
  -b, --bar the bar description