Command-Line Programs
Subcommand Dispatch
A subcommand lets one program route to several related actions.
Subcommand Dispatch
subcommand_dispatch.rb
args =
subcommand = args.first || "help"
message = case subcommand
when "init"
"create project"
when "status"
"show status"
else
"show help"
end
handled = ["init", "status"].include?(subcommand)
puts "subcommand=#{subcommand}"
puts "message=#{message}"
puts "handled=#{handled}"
args =
subcommand = args.first || "help"
message = case subcommand
when "init"
"create project"
when "status"
"show status"
else
"show help"
end
handled = ["init", "status"].include?(subcommand)
puts "subcommand=#{subcommand}"
puts "message=#{message}"
puts "handled=#{handled}"
args =
subcommand = args.first || "help"
message = case subcommand
when "init"
"create project"
when "status"
"show status"
else
"show help"
end
handled = ["init", "status"].include?(subcommand)
puts "subcommand=#{subcommand}"
puts "message=#{message}"
puts "handled=#{handled}"
subcommand
`case` is a compact way to map the first argument to a command handler.