Skip to content

Commit

Permalink
Fix parsing switch/flag command line values when no help doc provided
Browse files Browse the repository at this point in the history
  • Loading branch information
codenamev committed Feb 20, 2022
1 parent d99afd1 commit fbaee2b
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/git_reflow/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def command(name, **params, &block)
self.commands[name] = params
self.command_docs[name] = params

logger.debug "adding new command '#{name}' with #{defaults.inspect}"
self.define_singleton_method(name) do |args = {}|
args_with_defaults = {}
args.each do |name, value|
Expand Down Expand Up @@ -310,13 +311,19 @@ def parse_command_options!(name)
opts.separator "COMMAND OPTIONS:" if docs[:flags].any? || docs[:switches].any?

self.commands[name][:flags].each do |flag_name, flag_default|
opts.on("-#{flag_name[0]}", "--#{flag_name} #{flag_name.upcase}", command_docs[name][:flags][flag_name]) do |f|
# There is a bug in Ruby that will not parse the flag value if no
# help text is provided. Fallback to the flag name.
flag_help = command_docs[name][:flags][flag_name] || flag_name
opts.on("-#{flag_name[0]}", "--#{flag_name} #{flag_name.upcase}", flag_help) do |f|
options[kebab_to_underscore(flag_name)] = f || flag_default
end
end

self.commands[name][:switches].each do |switch_name, switch_default|
opts.on("-#{switch_name[0]}", "--[no-]#{switch_name}", command_docs[name][:switches][switch_name]) do |s|
# There is a bug in Ruby that will not parse the switch value if no
# help text is provided. Fallback to the switch name.
switch_help = command_docs[name][:switches][switch_name] || switch_name
opts.on("-#{switch_name[0]}", "--[no-]#{switch_name}", switch_help) do |s|
options[kebab_to_underscore(switch_name)] = s || switch_default
end
end
Expand Down

0 comments on commit fbaee2b

Please sign in to comment.