Skip to content

Commit 38275f5

Browse files
committed
Refactors CLI option parser configuration into separate descriptive methods
1 parent 9431381 commit 38275f5

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

lib/git_commander/cli.rb

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ def run(args = ARGV)
3030
help
3131
end
3232

33-
def help
34-
say "NAME"
35-
say " git-cmd – Git Commander allows running custom git commands from a centralized location."
36-
say "VERSION"
37-
say " #{GitCommander::VERSION}"
38-
say "USAGE"
39-
say " git-cmd command [command options] [arguments...]"
40-
say "COMMANDS"
41-
say registry.commands.keys.join(", ")
42-
end
43-
4433
def say(message)
4534
output.puts message
4635
end
@@ -51,25 +40,7 @@ def say(message)
5140
# @param arguments [Array] the command line arguments
5241
# @return options [Array] the GitCommander::Command options with values
5342
def parse_command_options!(command, arguments)
54-
valid_arguments_for_command = command.arguments.map { |arg| "[#{arg.name}]" }.join(" ")
55-
56-
parser = OptionParser.new do |opts|
57-
opts.banner = "USAGE:\n git-cmd #{command.name} [command options] #{valid_arguments_for_command}"
58-
opts.separator ""
59-
opts.separator "COMMAND OPTIONS:" if command.flags.any? || command.switches.any?
60-
61-
command.flags.each do |flag|
62-
opts.on("-#{flag.name[0]}", command_line_flag_formatted_name(flag), flag.description.to_s) do |f|
63-
flag.value = f
64-
end
65-
end
66-
67-
command.switches.each do |switch|
68-
opts.on("-#{switch.name[0]}", "--[no-]#{switch.name}", switch.description) do |s|
69-
switch.value = s
70-
end
71-
end
72-
end
43+
parser = configure_option_parser_for_command(command)
7344
parser.parse!(arguments)
7445

7546
# Add arguments to options to pass to defined commands
@@ -86,6 +57,46 @@ def parse_command_options!(command, arguments)
8657

8758
private
8859

60+
def help
61+
say "NAME"
62+
say " git-cmd – Git Commander allows running custom git commands from a centralized location."
63+
say "VERSION"
64+
say " #{GitCommander::VERSION}"
65+
say "USAGE"
66+
say " git-cmd command [command options] [arguments...]"
67+
say "COMMANDS"
68+
say registry.commands.keys.join(", ")
69+
end
70+
71+
def configure_option_parser_for_command(command)
72+
valid_arguments_for_command = command.arguments.map { |arg| "[#{arg.name}]" }.join(" ")
73+
74+
OptionParser.new do |opts|
75+
opts.banner = "USAGE:\n git-cmd #{command.name} [command options] #{valid_arguments_for_command}"
76+
opts.separator ""
77+
opts.separator "COMMAND OPTIONS:" if command.flags.any? || command.switches.any?
78+
79+
configure_flags_for_option_parser_and_command(opts, command)
80+
configure_switches_for_option_parser_and_command(opts, command)
81+
end
82+
end
83+
84+
def configure_flags_for_option_parser_and_command(option_parser, command)
85+
command.flags.each do |flag|
86+
option_parser.on("-#{flag.name[0]}", command_line_flag_formatted_name(flag), flag.description.to_s) do |f|
87+
flag.value = f
88+
end
89+
end
90+
end
91+
92+
def configure_switches_for_option_parser_and_command(option_parser, command)
93+
command.switches.each do |switch|
94+
option_parser.on("-#{switch.name[0]}", "--[no-]#{switch.name}", switch.description) do |s|
95+
switch.value = s
96+
end
97+
end
98+
end
99+
89100
def command_line_flag_formatted_name(flag)
90101
"--#{underscore_to_kebab(flag.name)} #{flag.name.upcase}"
91102
end

0 commit comments

Comments
 (0)