Skip to content

Commit

Permalink
Support hidden cli options
Browse files Browse the repository at this point in the history
  • Loading branch information
pnezis committed May 17, 2024
1 parent a56d8d4 commit f6293a9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cli_options/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions cli_options/lib/cli_options/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
]

Expand Down
4 changes: 4 additions & 0 deletions cli_options/lib/cli_options/schema/docs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions cli_options/test/cli_options/schema/docs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
]

Expand Down
3 changes: 2 additions & 1 deletion cli_options/test/cli_options/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f6293a9

Please sign in to comment.