Skip to content

Commit ee97b8c

Browse files
enhance(cli): Add -h/--help to commands
1 parent 3a9b00f commit ee97b8c

2 files changed

Lines changed: 44 additions & 28 deletions

File tree

deps/cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## 0.4.2
2+
* Add `--help` to all commands as an alternative to `help [command]`
23
* Add `--validate` option to `export-edn` command
34
* Fix cli can't run in CI environments
45
* Fix `import-edn` and `mcp-server` commands not building refs for new or edited nodes

deps/cli/src/logseq/cli.cljs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
:desc "Print version"}})
2525

2626
(declare table)
27-
(defn- help [_m]
27+
(defn- print-general-help [_m]
2828
(println (str "Usage: logseq [command] [options]\n\nOptions:\n"
2929
(cli/format-opts {:spec default-spec})))
3030
(println (str "\nCommands:\n" (format-commands {:table table}))))
@@ -37,7 +37,7 @@
3737
(println (-> (fs/readFileSync package-json)
3838
js/JSON.parse
3939
(aget "version")))))
40-
(help m)))
40+
(print-general-help m)))
4141

4242
(defn- print-command-help [command cmd-map]
4343
(println (str "Usage: logseq " command
@@ -50,24 +50,30 @@
5050
(when (:description cmd-map)
5151
(str "\n\nDescription:\n" (cli-text-util/wrap-text (:description cmd-map) 80))))))
5252

53-
(defn- help-command [{{:keys [command]} :opts}]
53+
(defn- help-command [{{:keys [command help]} :opts}]
5454
(if-let [cmd-map (and command (some #(when (= command (first (:cmds %))) %) table))]
5555
(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"))))
5760

5861
(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"
6064
[fn-sym]
6165
(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)))))))
6975

70-
(def ^:private table
76+
(def ^:private table*
7177
[{:cmds ["list"] :desc "List local graphs"
7278
:fn (lazy-load-fn 'logseq.cli.commands.graph/list-graphs)}
7379
{:cmds ["show"] :desc "Show DB graph(s) info"
@@ -116,30 +122,39 @@
116122
:spec default-spec
117123
:fn default-command}])
118124

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+
119133
(defn- warn-if-db-version-not-installed
120134
[]
121135
(when-not (fs/existsSync (cli-common-graph/get-db-graphs-dir))
122136
(println "[WARN] The database version's desktop app is not installed. Please install per https://github.com/logseq/logseq/#-database-version.")))
123137

124138
(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)
127140
(try
128141
(cli/dispatch table
129142
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)))})
143158
(catch ^:sci/error js/Error e
144159
(nbb.error/print-error-report e)
145160
(js/process.exit 1))))

0 commit comments

Comments
 (0)