Skip to content

Commit

Permalink
added default libre-man#17
Browse files Browse the repository at this point in the history
  • Loading branch information
digikar99 committed May 23, 2020
1 parent ca3f3d3 commit 7f52367
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
29 changes: 19 additions & 10 deletions tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
:description "option to cause ambiguity with grab-str as well as help test the print function of opts:describe due to this long description"
:short #\s
:long "grab-string"
:arg-parser #'identity)
:arg-parser #'identity
:default 42)
(:name :flag-a
:description "flag with short form only"
:short #\a)
Expand Down Expand Up @@ -147,7 +148,8 @@ recommended to supply them all if you don't want to end in the debugger."
:unknown-option '(skip-option)
:missing-arg '(skip-option)
:arg-parser-failed '(skip-option))
(assert (equalp options '(:grab-int 10 :flag-a t)))
(assert (equalp options '(:grab-int 10 :flag-a t
:grab-string 42)))
(assert (equalp free-args '("11" "foo.txt")))
(assert (equalp *unknown-options* '("--rere")))
(assert (equalp *missing-arg-options* '("-s")))
Expand All @@ -159,7 +161,8 @@ recommended to supply them all if you don't want to end in the debugger."
:unknown-option '(skip-option)
:missing-arg '(skip-option)
:arg-parser-failed '(skip-option))
(assert (equalp options '(:flag-a t :grab-int 13 :flag-b t :flag-b t)))
(assert (equalp options '(:flag-a t :grab-int 13 :flag-b t :flag-b t
:grab-string 42)))
(assert (equalp free-args '("foo.txt" "bar.txt")))
(assert (equalp *unknown-options* '("-r")))
(assert (equalp *missing-arg-options* '("-s")))
Expand All @@ -171,7 +174,8 @@ recommended to supply them all if you don't want to end in the debugger."
:unknown-option '(skip-option)
:missing-arg '(skip-option)
:arg-parser-failed '(skip-option))
(assert (equalp options '(:grab-str "fooba" :grab-int 100)))
(assert (equalp options '(:grab-str "fooba" :grab-int 100
:grab-string 42)))
(assert (equalp free-args '("-")))
(assert (equalp *unknown-options* '("--roro")))
(assert (equalp *missing-arg-options* nil))
Expand All @@ -184,7 +188,8 @@ recommended to supply them all if you don't want to end in the debugger."
:missing-arg '(use-value "my-string")
:arg-parser-failed '(reparse-arg "15"))
(assert (equalp options '(:grab-int 15 :grab-str "my-string"
:grab-int "my-string")))
:grab-int "my-string" ; TODO: should this be the behaviour
:grab-string 42)))
(assert (equalp free-args nil))
(assert (equalp *unknown-options* '("--foobar" "-l")))
(assert (equalp *missing-arg-options* '("-s" "--grab-int")))
Expand All @@ -197,10 +202,11 @@ recommended to supply them all if you don't want to end in the debugger."
:missing-arg '(skip-option)
:arg-parser-failed '(skip-option)
:ambiguous-option '(skip-option))
(assert (equalp options '(:grab-int 10)))
(assert (equalp options '(:grab-int 10
:grab-string 42)))
(assert (equalp free-args '("14")))
(assert (null (set-difference *ambiguous-options* '("--grab" "--grab-s")
:test #'equal)))
:test #'equal)))
(assert (equalp *malformed-arguments* nil))))

(defun miscelleneous-6 ()
Expand All @@ -209,7 +215,8 @@ recommended to supply them all if you don't want to end in the debugger."
:unknown-option '(skip-option)
:missing-arg '(skip-option)
:arg-parser-failed '(skip-option))
(assert (equalp options '(:grab-int 15)))
(assert (equalp options '(:grab-int 15
:grab-string 42)))
(assert (equalp free-args '("--grab-int" "16")))
(assert (equalp *unknown-options* nil))
(assert (equalp *missing-arg-options* nil))
Expand All @@ -222,7 +229,8 @@ recommended to supply them all if you don't want to end in the debugger."
:missing-arg '(skip-option)
:missing-required '(skip-option)
:arg-parser-failed '(skip-option))
(assert (equalp options '(:grab-str "5")))
(assert (equalp options '(:grab-str "5"
:grab-string 42)))
(assert (equalp free-args '()))
(assert (equalp *missing-required-options* '((:grab-int))))
(assert (equalp *unknown-options* nil))
Expand All @@ -236,7 +244,8 @@ recommended to supply them all if you don't want to end in the debugger."
:missing-arg '(skip-option)
:missing-required '(use-value (15))
:arg-parser-failed '(skip-option))
(assert (equalp options '(:grab-str "5" :grab-int 15)))
(assert (equalp options '(:grab-str "5" :grab-int 15
:grab-string 42)))
(assert (equalp free-args '()))
(assert (equalp *missing-required-options* '((:grab-int))))
(assert (equalp *unknown-options* nil))
Expand Down
21 changes: 18 additions & 3 deletions unix-opts.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ parsed with this function")
:initarg :meta-var
:accessor meta-var
:documentation "if this option requires an argument, this is how it will
be printed in option description"))
be printed in option description")
(default
:initarg :default
:initform nil
:accessor default
:documentation "if this option requires an argument, this is the value that will be
used if the option is not supplied"))
(:documentation "representation of an option"))

(define-condition troublesome-option (simple-error)
Expand Down Expand Up @@ -149,7 +155,8 @@ an argument, it's given but cannot be parsed by argument parser."))
(long (getf args :long))
(arg-parser (getf args :arg-parser))
(required (getf args :required))
(meta-var (getf args :meta-var "ARG")))
(meta-var (getf args :meta-var "ARG"))
(default (getf args :default nil)))
(unless (or short long)
(error "at least one form of the option must be provided"))
(check-type name keyword)
Expand All @@ -166,7 +173,8 @@ an argument, it's given but cannot be parsed by argument parser."))
:long long
:required required
:arg-parser arg-parser
:meta-var meta-var)
:meta-var meta-var
:default default)
*options*)))

(defmacro define-opts (&body descriptions)
Expand Down Expand Up @@ -378,6 +386,13 @@ to `nil')"
:for value :in values
:do (push (name option) options)
:do (push value options))))))
;; handle default options
(loop :for option :in *options*
:for name = (name option)
:when (and (default option)
(null (getf options name)))
:do (push name options)
(push (default option) options))
(values (nreverse options)
(nreverse free-args))))
(labels ((push-option (name value)
Expand Down

0 comments on commit 7f52367

Please sign in to comment.