Skip to content

Commit 68335b1

Browse files
authored
Add :flag option type (#117)
Acts like :boolean, but doesn't imply `--[no-]` prefix
1 parent 9731807 commit 68335b1

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

lib/dry/cli/banner.rb

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def self.extended_command_options(command)
109109
name = Inflector.dasherize(option.name)
110110
name = if option.boolean?
111111
"[no-]#{name}"
112+
elsif option.flag?
113+
name
112114
elsif option.array?
113115
"#{name}=VALUE1,VALUE2,.."
114116
else

lib/dry/cli/option.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ def boolean?
5959
type == :boolean
6060
end
6161

62+
# @api private
63+
def flag?
64+
type == :flag
65+
end
66+
6267
# @since 0.3.0
6368
# @api private
6469
def array?
@@ -92,6 +97,8 @@ def parser_options
9297

9398
if boolean?
9499
parser_options << "--[no-]#{dasherized_name}"
100+
elsif flag?
101+
parser_options << "--#{dasherized_name}"
95102
else
96103
parser_options << "--#{dasherized_name}=#{name}"
97104
parser_options << "--#{dasherized_name} #{name}"
@@ -112,7 +119,7 @@ def alias_names
112119
.compact
113120
.uniq
114121
.map { |name| name.size == 1 ? "-#{name}" : "--#{name}" }
115-
.map { |name| boolean? ? name : "#{name} VALUE" }
122+
.map { |name| boolean? || flag? ? name : "#{name} VALUE" }
116123
end
117124
end
118125

spec/support/fixtures/shared_commands.rb

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ class Server < Dry::CLI::Command
298298
option :daemonize, desc: "Daemonize the server"
299299
option :pid, desc: "Path to write a pid file after daemonize"
300300
option :code_reloading, desc: "Code reloading", type: :boolean, default: true
301+
option :quiet, desc: "Suppress output to stdout", type: :flag
301302
option :deps, desc: "List of extra dependencies", type: :array, default: %w[dep1 dep2]
302303

303304
example [

spec/support/shared_examples/commands.rb

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@
7272
expect(output).to eq("server - {:code_reloading=>false, :deps=>[\"dep1\", \"dep2\"]}\n")
7373
end
7474

75+
it "with flag param" do
76+
output = capture_output { cli.call(arguments: ["server"]) }
77+
expect(output).to eq("server - {:code_reloading=>true, :deps=>[\"dep1\", \"dep2\"]}\n")
78+
79+
output = capture_output { cli.call(arguments: %w[server --quiet]) }
80+
expect(output).to eq("server - {:code_reloading=>true, :deps=>[\"dep1\", \"dep2\"], :quiet=>true}\n")
81+
end
82+
7583
context "with array param" do
7684
it "allows to omit optional array argument" do
7785
output = capture_output { cli.call(arguments: %w[exec test]) }
@@ -122,6 +130,7 @@
122130
--daemonize=VALUE # Daemonize the server
123131
--pid=VALUE # Path to write a pid file after daemonize
124132
--[no-]code-reloading # Code reloading, default: true
133+
--quiet # Suppress output to stdout
125134
--deps=VALUE1,VALUE2,.. # List of extra dependencies, default: ["dep1", "dep2"]
126135
--help, -h # Print this help
127136

0 commit comments

Comments
 (0)