From f6293a9fc8b5ee02fc3ffa9df4b21a3ca5e8979e Mon Sep 17 00:00:00 2001 From: pnezis Date: Fri, 17 May 2024 17:47:41 +0300 Subject: [PATCH] Support hidden cli options --- cli_options/CHANGELOG.md | 6 ++++++ cli_options/lib/cli_options/schema.ex | 4 ++++ cli_options/lib/cli_options/schema/docs.ex | 4 ++++ cli_options/test/cli_options/schema/docs_test.exs | 4 ++++ cli_options/test/cli_options/schema_test.exs | 3 ++- 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cli_options/CHANGELOG.md b/cli_options/CHANGELOG.md index 6c2f579..f18e145 100644 --- a/cli_options/CHANGELOG.md +++ b/cli_options/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). +## [Unreleased] + +### Added + +* If an option is declared as `:hidden` it will not be included in the docs. + ## [v0.1.0](https://github.com/sportradar/elixir-workspace/tree/cli_options/v0.1.0) (2024-05-13) Initial release. diff --git a/cli_options/lib/cli_options/schema.ex b/cli_options/lib/cli_options/schema.ex index 0d78eb6..70a81a4 100644 --- a/cli_options/lib/cli_options/schema.ex +++ b/cli_options/lib/cli_options/schema.ex @@ -141,6 +141,10 @@ defmodule CliOptions.Schema do A set of allowed values for the option. If any other value is given an exception will be raised during parsing. """ + ], + hidden: [ + type: :boolean, + doc: "If set to `true` the option will not be included in the generated docs" ] ] diff --git a/cli_options/lib/cli_options/schema/docs.ex b/cli_options/lib/cli_options/schema/docs.ex index 4c1ff61..748db5d 100644 --- a/cli_options/lib/cli_options/schema/docs.ex +++ b/cli_options/lib/cli_options/schema/docs.ex @@ -5,12 +5,16 @@ defmodule CliOptions.Schema.Docs do @spec generate(schema :: keyword(), opts :: keyword()) :: String.t() def generate(schema, opts) do schema + |> remove_hidden_options() |> maybe_sort(Keyword.get(opts, :sort, false)) |> Enum.reduce([], &maybe_option_doc/2) |> Enum.reverse() |> Enum.join("\n") end + defp remove_hidden_options(schema), + do: Enum.reject(schema, fn {_key, opts} -> opts[:hidden] end) + defp maybe_sort(schema, true), do: Enum.sort_by(schema, fn {key, _value} -> key end, :asc) defp maybe_sort(schema, _other), do: schema diff --git a/cli_options/test/cli_options/schema/docs_test.exs b/cli_options/test/cli_options/schema/docs_test.exs index 0aede34..52d5f07 100644 --- a/cli_options/test/cli_options/schema/docs_test.exs +++ b/cli_options/test/cli_options/schema/docs_test.exs @@ -24,6 +24,10 @@ defmodule CliOptions.Schema.DocsTest do with_dash: [ type: :boolean, doc: "a key with a dash" + ], + hidden_option: [ + type: :boolean, + hidden: true ] ] diff --git a/cli_options/test/cli_options/schema_test.exs b/cli_options/test/cli_options/schema_test.exs index 77c5561..50a50c8 100644 --- a/cli_options/test/cli_options/schema_test.exs +++ b/cli_options/test/cli_options/schema_test.exs @@ -16,7 +16,8 @@ defmodule CliOptions.SchemaTest do message = "invalid schema for :foo, unknown options [:missing, :other], valid options are: " <> - "[:type, :default, :long, :short, :aliases, :short_aliases, :doc, :required, :multiple, :allowed]" + "[:type, :default, :long, :short, :aliases, :short_aliases, :doc, :required, :multiple, " <> + ":allowed, :hidden]" assert_raise ArgumentError, message, fn -> CliOptions.Schema.new!(schema)