From 68335b180fdf5044453b396208fa7fbf075c6a85 Mon Sep 17 00:00:00 2001 From: Billiam Date: Wed, 10 Jul 2024 16:34:58 -0500 Subject: [PATCH] Add :flag option type (#117) Acts like :boolean, but doesn't imply `--[no-]` prefix --- lib/dry/cli/banner.rb | 2 ++ lib/dry/cli/option.rb | 9 ++++++++- spec/support/fixtures/shared_commands.rb | 1 + spec/support/shared_examples/commands.rb | 9 +++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/dry/cli/banner.rb b/lib/dry/cli/banner.rb index a70a89a..22a284c 100644 --- a/lib/dry/cli/banner.rb +++ b/lib/dry/cli/banner.rb @@ -109,6 +109,8 @@ def self.extended_command_options(command) name = Inflector.dasherize(option.name) name = if option.boolean? "[no-]#{name}" + elsif option.flag? + name elsif option.array? "#{name}=VALUE1,VALUE2,.." else diff --git a/lib/dry/cli/option.rb b/lib/dry/cli/option.rb index 31bc1a9..710de98 100644 --- a/lib/dry/cli/option.rb +++ b/lib/dry/cli/option.rb @@ -59,6 +59,11 @@ def boolean? type == :boolean end + # @api private + def flag? + type == :flag + end + # @since 0.3.0 # @api private def array? @@ -92,6 +97,8 @@ def parser_options if boolean? parser_options << "--[no-]#{dasherized_name}" + elsif flag? + parser_options << "--#{dasherized_name}" else parser_options << "--#{dasherized_name}=#{name}" parser_options << "--#{dasherized_name} #{name}" @@ -112,7 +119,7 @@ def alias_names .compact .uniq .map { |name| name.size == 1 ? "-#{name}" : "--#{name}" } - .map { |name| boolean? ? name : "#{name} VALUE" } + .map { |name| boolean? || flag? ? name : "#{name} VALUE" } end end diff --git a/spec/support/fixtures/shared_commands.rb b/spec/support/fixtures/shared_commands.rb index 4cbbc06..f694cd9 100644 --- a/spec/support/fixtures/shared_commands.rb +++ b/spec/support/fixtures/shared_commands.rb @@ -298,6 +298,7 @@ class Server < Dry::CLI::Command option :daemonize, desc: "Daemonize the server" option :pid, desc: "Path to write a pid file after daemonize" option :code_reloading, desc: "Code reloading", type: :boolean, default: true + option :quiet, desc: "Suppress output to stdout", type: :flag option :deps, desc: "List of extra dependencies", type: :array, default: %w[dep1 dep2] example [ diff --git a/spec/support/shared_examples/commands.rb b/spec/support/shared_examples/commands.rb index 9a90d9a..4522201 100644 --- a/spec/support/shared_examples/commands.rb +++ b/spec/support/shared_examples/commands.rb @@ -72,6 +72,14 @@ expect(output).to eq("server - {:code_reloading=>false, :deps=>[\"dep1\", \"dep2\"]}\n") end + it "with flag param" do + output = capture_output { cli.call(arguments: ["server"]) } + expect(output).to eq("server - {:code_reloading=>true, :deps=>[\"dep1\", \"dep2\"]}\n") + + output = capture_output { cli.call(arguments: %w[server --quiet]) } + expect(output).to eq("server - {:code_reloading=>true, :deps=>[\"dep1\", \"dep2\"], :quiet=>true}\n") + end + context "with array param" do it "allows to omit optional array argument" do output = capture_output { cli.call(arguments: %w[exec test]) } @@ -122,6 +130,7 @@ --daemonize=VALUE # Daemonize the server --pid=VALUE # Path to write a pid file after daemonize --[no-]code-reloading # Code reloading, default: true + --quiet # Suppress output to stdout --deps=VALUE1,VALUE2,.. # List of extra dependencies, default: ["dep1", "dep2"] --help, -h # Print this help