|
24 | 24 | :desc "Print version"}}) |
25 | 25 |
|
26 | 26 | (declare table) |
27 | | -(defn- help [_m] |
| 27 | +(defn- print-general-help [_m] |
28 | 28 | (println (str "Usage: logseq [command] [options]\n\nOptions:\n" |
29 | 29 | (cli/format-opts {:spec default-spec}))) |
30 | 30 | (println (str "\nCommands:\n" (format-commands {:table table})))) |
|
37 | 37 | (println (-> (fs/readFileSync package-json) |
38 | 38 | js/JSON.parse |
39 | 39 | (aget "version"))))) |
40 | | - (help m))) |
| 40 | + (print-general-help m))) |
41 | 41 |
|
42 | 42 | (defn- print-command-help [command cmd-map] |
43 | 43 | (println (str "Usage: logseq " command |
|
50 | 50 | (when (:description cmd-map) |
51 | 51 | (str "\n\nDescription:\n" (cli-text-util/wrap-text (:description cmd-map) 80)))))) |
52 | 52 |
|
53 | | -(defn- help-command [{{:keys [command]} :opts}] |
| 53 | +(defn- help-command [{{:keys [command help]} :opts}] |
54 | 54 | (if-let [cmd-map (and command (some #(when (= command (first (:cmds %))) %) table))] |
55 | 55 | (print-command-help command cmd-map) |
56 | | - (println "Command" (pr-str command) "does not exist"))) |
| 56 | + ;; handle help --help |
| 57 | + (if-let [cmd-map (and help (some #(when (= "help" (first (:cmds %))) %) table))] |
| 58 | + (print-command-help "help" cmd-map) |
| 59 | + (println "Command" (pr-str command) "does not exist")))) |
57 | 60 |
|
58 | 61 | (defn- lazy-load-fn |
59 | | - "Lazy load fn to speed up start time. After nbb requires ~30 namespaces, start time gets close to 1s" |
| 62 | + "Lazy load fn to speed up start time. After nbb requires ~30 namespaces, start time gets close to 1s. |
| 63 | + Also handles --help on all commands" |
60 | 64 | [fn-sym] |
61 | 65 | (fn [& args] |
62 | | - (-> (p/let [_ (require (symbol (namespace fn-sym)))] |
63 | | - (apply (resolve fn-sym) args)) |
64 | | - (p/catch (fn [err] |
65 | | - (if (= :sci/error (:type (ex-data err))) |
66 | | - (nbb.error/print-error-report err) |
67 | | - (js/console.error "Error:" err)) |
68 | | - (js/process.exit 1)))))) |
| 66 | + (if (get-in (first args) [:opts :help]) |
| 67 | + (help-command {:opts {:command (-> args first :dispatch first)}}) |
| 68 | + (-> (p/let [_ (require (symbol (namespace fn-sym)))] |
| 69 | + (apply (resolve fn-sym) args)) |
| 70 | + (p/catch (fn [err] |
| 71 | + (if (= :sci/error (:type (ex-data err))) |
| 72 | + (nbb.error/print-error-report err) |
| 73 | + (js/console.error "Error:" err)) |
| 74 | + (js/process.exit 1))))))) |
69 | 75 |
|
70 | | -(def ^:private table |
| 76 | +(def ^:private table* |
71 | 77 | [{:cmds ["list"] :desc "List local graphs" |
72 | 78 | :fn (lazy-load-fn 'logseq.cli.commands.graph/list-graphs)} |
73 | 79 | {:cmds ["show"] :desc "Show DB graph(s) info" |
|
116 | 122 | :spec default-spec |
117 | 123 | :fn default-command}]) |
118 | 124 |
|
| 125 | +;; Spec shared with all commands |
| 126 | +(def ^:private shared-spec |
| 127 | + {:help {:alias :h |
| 128 | + :desc "Print help"}}) |
| 129 | + |
| 130 | +(def ^:private table |
| 131 | + (mapv (fn [m] (update m :spec #(merge % shared-spec))) table*)) |
| 132 | + |
119 | 133 | (defn- warn-if-db-version-not-installed |
120 | 134 | [] |
121 | 135 | (when-not (fs/existsSync (cli-common-graph/get-db-graphs-dir)) |
122 | 136 | (println "[WARN] The database version's desktop app is not installed. Please install per https://github.com/logseq/logseq/#-database-version."))) |
123 | 137 |
|
124 | 138 | (defn ^:api -main [& args] |
125 | | - (when-not (contains? #{nil "-h" "--help"} (first args)) |
126 | | - (warn-if-db-version-not-installed)) |
| 139 | + (warn-if-db-version-not-installed) |
127 | 140 | (try |
128 | 141 | (cli/dispatch table |
129 | 142 | args |
130 | | - {:error-fn (fn [{:keys [cause msg option] type' :type :as data}] |
131 | | - (if (and (= :org.babashka/cli type') |
132 | | - (= :require cause)) |
133 | | - (do |
134 | | - (println "Error: Command missing required" |
135 | | - (if (get-in data [:spec option]) "option" "argument") |
136 | | - (pr-str (name option))) |
137 | | - (when-let [cmd-m (some #(when (= {:spec (:spec %) |
138 | | - :require (:require %)} |
139 | | - (select-keys data [:spec :require])) %) table)] |
140 | | - (print-command-help (-> cmd-m :cmds first) cmd-m))) |
141 | | - (throw (ex-info msg data))) |
142 | | - (js/process.exit 1))}) |
| 143 | + {:error-fn (fn [{:keys [cause msg option opts] type' :type :as data}] |
| 144 | + ;; Options aren't required when printing help |
| 145 | + (when-not (:help opts) |
| 146 | + (if (and (= :org.babashka/cli type') |
| 147 | + (= :require cause)) |
| 148 | + (do |
| 149 | + (println "Error: Command missing required" |
| 150 | + (if (get-in data [:spec option]) "option" "argument") |
| 151 | + (pr-str (name option))) |
| 152 | + (when-let [cmd-m (some #(when (= {:spec (:spec %) |
| 153 | + :require (:require %)} |
| 154 | + (select-keys data [:spec :require])) %) table)] |
| 155 | + (print-command-help (-> cmd-m :cmds first) cmd-m))) |
| 156 | + (throw (ex-info msg data))) |
| 157 | + (js/process.exit 1)))}) |
143 | 158 | (catch ^:sci/error js/Error e |
144 | 159 | (nbb.error/print-error-report e) |
145 | 160 | (js/process.exit 1)))) |
|
0 commit comments