Skip to content

make docopt as function instead of macros #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/docopt/core.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns docopt.core
(ns docopt.core
(:require [clojure.string :as s])
(:require [docopt.match :as m])
(:require [docopt.optionsblock :as o])
Expand All @@ -18,18 +18,18 @@
(osplitfn [options-block] (re-seq #"(?<=^|\n)\s*-.*(?:\s+[^- \t\n].*)*" options-block))]
(u/parse (section "usage" s/split-lines) (o/parse (section "options" osplitfn)))))

(defmacro docopt
(defn docopt
"Parses doc string at compile-time and matches command line arguments at run-time.
The doc string may be omitted, in which case the metadata of '-main' is used"
([args]
([args]
(let [doc (:doc (meta (find-var (symbol (pr-str (ns-name *ns*)) "-main"))))]
(if (string? doc)
`(m/match-argv ~(parse doc) ~args)
(m/match-argv (parse doc) args)
(throw (Exception. "Docopt with one argument requires that #'-main have a doc string.\n")))))
([doc args]
`(m/match-argv ~(parse doc) ~args)))
(m/match-argv (parse doc) args)))

(defn -docopt
(defn -docopt
"Java-capable run-time equivalent to 'docopt';
argument 'doc' can be either a doc string or the result of a call to 'parse'.
Returns a java.util.HashMap of the matched values provided by the 'args' sequence."
Expand All @@ -39,4 +39,3 @@ Returns a java.util.HashMap of the matched values provided by the 'args' sequenc
(doseq [[k v] cljmap]
(.put javamap k (if (vector? v) (into-array v) v)))
javamap)))