Skip to content

Commit c9b15e5

Browse files
authored
Merge pull request #478 from shmpwk/warn-parse-args
Add warning for argparse.l when not calling :parse-args
2 parents 030e596 + 258a8dd commit c9b15e5

File tree

5 files changed

+146
-3
lines changed

5 files changed

+146
-3
lines changed

doc/jlatex/jmanual.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ \part{EusLisp 拡張}
169169
\input{jimage}
170170
\input{jmanipulator}
171171
\input{jmars-pre}
172+
\input{jmisc}
172173
%
173174
\begin{thebibliography}{99}
174175
\bibitem{Hirukawa:1991a}{T.Matsui H.Hirukawa and K.Takase. A general algorithm for derivation and analysis of constraint for motion of polyhedra in contact. In IEEE/RSJ International Workshop on Intelligent Robots and Systems'91, pages 38-43, 1991.}

doc/jlatex/jmisc.tex

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
\section{その他の機能}
2+
3+
\subsection{Argument Parser}
4+
5+
\begin{refdesc}
6+
7+
\classdesc{argument-parser}{propertied-object}{flaglst docstring parsed-p}{
8+
コマンドライン引数のパーサを定義する。}
9+
10+
\methoddesc{:init}{\&key prog description epilog (add-help t)}{
11+
プログラム名,自動生成されるヘルプテキストの前後に表示するテキストから,
12+
オブジェクトを生成する。}
13+
14+
\methoddesc{:add-argument}{flags \&key (action :store) const default choices check read required help dest}{
15+
コマンドライン引数を定義する。
16+
17+
{\tt flag} は {\tt "--foo"} や {\tt "-b"} の引数オプションを定義する。
18+
引数は{\tt '("--bar" "-b")}等のリストで与えることも可能である。
19+
20+
{\tt action} はプログラムに引数が渡されたときの挙動を指定する。
21+
現在サポートされている{\tt action} は {\tt :store}, {\tt :store-true},
22+
{\tt :store-false}, {\tt :store-const}, {\tt :append}, そしてカスタム関数である。
23+
{\tt help} はヘルプドキュメントを指定する。
24+
ほとんどのパラメータはhttps://docs.python.org/3/library/argparse.htmlに習って設計されている。}
25+
26+
\methoddesc{:parse-args}{}{
27+
{\tt lisp::*eustop-argument*} を用いてコマンドライン引数をパースする。
28+
このメソッドは{\tt argument-parser} インスタンスに引数オプション名メ
29+
ソッドを与える前に呼ばなければならない。}
30+
31+
{\tt argument-parser}のサンプルプログラムを以下に示す。
32+
33+
\begin{verbatim}
34+
(require :argparse "argparse.l")
35+
36+
(defvar argparse (instance argparse:argument-parser :init
37+
:description "Program Description (optional)"))
38+
(send argparse :add-argument "--foo" :default 10 :read t
39+
:help "the foo description")
40+
(send argparse :add-argument '("--bar" "-b") :action :store-true
41+
:help "the bar description")
42+
43+
(send argparse :parse-args)
44+
(format t "foo: ~A~%" (send argparse :foo))
45+
(format t "bar: ~A~%" (send argparse :bar))
46+
(exit)
47+
\end{verbatim}
48+
49+
このサンプルプログラムを実行したときの出力は以下のようになる。
50+
51+
\begin{verbatim}
52+
$ eus argparse-example.l
53+
foo: 10
54+
bar: t
55+
$ eus argparse-example.l --foo=100 --bar
56+
foo: 100
57+
bar: t
58+
$ eus argparse-example.l -h
59+
usage: [-h] [--foo=FOO] [-b]
60+
61+
Program Description (optional)
62+
63+
optional arguments:
64+
-h, --help show this help message and exit
65+
--foo=FOO the foo description (default: 10)
66+
-b, --bar the bar description
67+
\end{verbatim}
68+
69+
\end{refdesc}

doc/latex/manual.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ \part{EusLisp Extensions}
183183
\input{database}
184184
\input{http}
185185
% \input{sound}
186+
\input{misc}
186187
%
187188
\begin{thebibliography}{99}
188189
\bibitem{Hirukawa:1991a}{T.Matsui H.Hirukawa and K.Takase. A general algorithm for derivation and analysis of constraint for motion of polyhedra in contact. In IEEE/RSJ International Workshop on Intelligent Robots and Systems'91, pages 38-43, 1991.}

doc/latex/misc.tex

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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}

lib/llib/argparse.l

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167

168168
(defclass argument-parser
169169
:super propertied-object
170-
:slots (flaglst docstring))
170+
:slots (flaglst docstring parsed-p))
171171
(defmethod argument-parser
172172
(:init (&key prog description epilog (add-help t))
173173
(setq docstring (instance argparse-docstring :init
@@ -209,7 +209,7 @@
209209
((not (argparse-argument-flagp arg)) name)))
210210
;; add method
211211
(if (keywordp name)
212-
(eval `(defmethod argument-parser (,name () (send (get self ,name) :value)))))
212+
(eval `(defmethod argument-parser (,name () (if (not parsed-p) (warning-message 1 "Not parsed! Call :parse-args!~%")) (send (get self ,name) :value)))))
213213
;; return name
214214
name))
215215
(:parse-args ()
@@ -248,4 +248,6 @@
248248
(cli-error "Argument ~S not found!~%" flag)))))
249249
;; check required arguments
250250
(when required-args
251-
(cli-error "Argument ~A is required!" (caar (last required-args))))))))
251+
(cli-error "Argument ~A is required!" (caar (last required-args))))))
252+
;; set parsed-p
253+
(setq parsed-p t)))

0 commit comments

Comments
 (0)