From 3d7e866d2ac8287830120c71504b543f3a6fa23d Mon Sep 17 00:00:00 2001 From: Oliver Runge Date: Sun, 1 May 2022 15:02:29 +0200 Subject: [PATCH] Add cljfmt options programmatically to CLI options This further reduces code duplication and automatically sorts the CLI options by name. --- cljfmt/src/cljfmt/main.clj | 53 ++++++++++++++------------------------ 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/cljfmt/src/cljfmt/main.clj b/cljfmt/src/cljfmt/main.clj index 3ee19730..304b1e08 100644 --- a/cljfmt/src/cljfmt/main.clj +++ b/cljfmt/src/cljfmt/main.clj @@ -161,40 +161,25 @@ (def default-paths ["src" "test" "project.clj"]) (def ^:private cli-options - [[nil "--help"] - [nil "--file-pattern FILE_PATTERN" - :default (:file-pattern default-options) - :parse-fn re-pattern] - [nil "--indents INDENTS_PATH" - :parse-fn cli-file-reader] - [nil "--alias-map ALIAS_MAP_PATH" - :parse-fn cli-file-reader] - [nil "--project-root PROJECT_ROOT" - :default (:project-root default-options)] - [nil "--[no-]ansi" - :default (:ansi? default-options) - :id :ansi?] - [nil "--[no-]indentation" - :default (:indentation? cljfmt/default-options) - :id :indentation?] - [nil "--[no-]remove-multiple-non-indenting-spaces" - :default (:remove-multiple-non-indenting-spaces? cljfmt/default-options) - :id :remove-multiple-non-indenting-spaces?] - [nil "--[no-]remove-surrounding-whitespace" - :default (:remove-surrounding-whitespace? cljfmt/default-options) - :id :remove-surrounding-whitespace?] - [nil "--[no-]remove-trailing-whitespace" - :default (:remove-trailing-whitespace? cljfmt/default-options) - :id :remove-trailing-whitespace?] - [nil "--[no-]insert-missing-whitespace" - :default (:insert-missing-whitespace? cljfmt/default-options) - :id :insert-missing-whitespace?] - [nil "--[no-]remove-consecutive-blank-lines" - :default (:remove-consecutive-blank-lines? cljfmt/default-options) - :id :remove-consecutive-blank-lines?] - [nil "--[no-]split-keypairs-over-multiple-lines" - :default (:split-keypairs-over-multiple-lines? cljfmt/default-options) - :id :split-keypairs-over-multiple-lines?]]) + (into + [[nil "--help"] + [nil "--file-pattern FILE_PATTERN" + :default (:file-pattern default-options) + :parse-fn re-pattern] + [nil "--indents INDENTS_PATH" + :parse-fn cli-file-reader] + [nil "--alias-map ALIAS_MAP_PATH" + :parse-fn cli-file-reader] + [nil "--project-root PROJECT_ROOT" + :default (:project-root default-options)] + [nil "--[no-]ansi" + :default (:ansi? default-options) + :id :ansi?]] + (for [[option default] (sort-by (comp name first) cljfmt/default-options)] + [nil (str "--[no-]" (let [s (name option)] + (subs s 0 (-> s count dec)))) + :default default + :id option]))) (defn- file-exists? [path] (.exists (io/as-file path)))