|
| 1 | +\section{Miscellaneous} |
| 2 | + |
| 3 | +\subsection{Argument Parser} |
| 4 | + |
| 5 | +\begin{refdesc} |
| 6 | + |
| 7 | +\classdesc{argument-parser}{propertied-object}{flaglst docstring parsed-p}{ |
| 8 | +command line argument parser.} |
| 9 | + |
| 10 | +\methoddesc{:init}{\&key prog description epilog (add-help t)}{ |
| 11 | +instantiates argument-parser class object from the name of the program, |
| 12 | +description text to display before the argument help and text to |
| 13 | +display after them as an epilog.} |
| 14 | + |
| 15 | +\methoddesc{:add-argument}{flags \&key (action :store) const default choices check read required help dest}{ |
| 16 | +defines the command-line argument to be parsed. |
| 17 | + |
| 18 | +{\tt flag} specify the options strings such as {\tt "--foo"} or {\tt |
| 19 | + "-b"}. It also takes list values i.e. {\tt '("--bar" "-b")}. |
| 20 | + |
| 21 | +{\tt action} defines the behavior when this argument is passed to the |
| 22 | +program. Current supported actions are {\tt :store}, {\tt :store-true}, |
| 23 | +{\tt :store-false}, {\tt :store-const}, {\tt :append}, and custom functions. |
| 24 | +{\tt help} defines the help document text. |
| 25 | +Most of the arguments are designed according to https://docs.python.org/3/library/argparse.html.} |
| 26 | + |
| 27 | +\methoddesc{:parse-args}{}{ |
| 28 | +parses command line argument from {\tt lisp::*eustop-argument*}. This |
| 29 | +method must be called before sending an argument name method to {\tt |
| 30 | + argument-parser} instance.} |
| 31 | + |
| 32 | +The following shows an example of {\tt argument-parser}. |
| 33 | + |
| 34 | +\begin{verbatim} |
| 35 | +(require :argparse "argparse.l") |
| 36 | +
|
| 37 | +(defvar argparse (instance argparse:argument-parser :init |
| 38 | + :description "Program Description (optional)")) |
| 39 | +(send argparse :add-argument "--foo" :default 10 :read t |
| 40 | + :help "the foo description") |
| 41 | +(send argparse :add-argument '("--bar" "-b") :action :store-true |
| 42 | + :help "the bar description") |
| 43 | +
|
| 44 | +(send argparse :parse-args) |
| 45 | +(format t "foo: ~A~%" (send argparse :foo)) |
| 46 | +(format t "bar: ~A~%" (send argparse :bar)) |
| 47 | +(exit) |
| 48 | +\end{verbatim} |
| 49 | + |
| 50 | +The following is an output of the example program above. |
| 51 | + |
| 52 | +\begin{verbatim} |
| 53 | +$ eus argparse-example.l |
| 54 | +foo: 10 |
| 55 | +bar: t |
| 56 | +$ eus argparse-example.l --foo=100 --bar |
| 57 | +foo: 100 |
| 58 | +bar: t |
| 59 | +$ eus argparse-example.l -h |
| 60 | +usage: [-h] [--foo=FOO] [-b] |
| 61 | +
|
| 62 | +Program Description (optional) |
| 63 | +
|
| 64 | +optional arguments: |
| 65 | + -h, --help show this help message and exit |
| 66 | + --foo=FOO the foo description (default: 10) |
| 67 | + -b, --bar the bar description |
| 68 | +\end{verbatim} |
| 69 | + |
| 70 | +\end{refdesc} |
0 commit comments