Add create_sub_parser method

This commit is contained in:
kojix2 2020-09-15 18:51:32 +09:00
parent b1df2ed544
commit 50cb8d7463

View File

@ -105,8 +105,7 @@ module Uplot
end end
end end
def parse_options(argv = ARGV) def create_sub_parser
main_parser = create_default_parser
parsers = Hash.new { |h, k| h[k] = create_default_parser } parsers = Hash.new { |h, k| h[k] = create_default_parser }
parsers[:barplot] = \ parsers[:barplot] = \
@ -200,6 +199,12 @@ module Uplot
# Preventing the generation of new sub-commands # Preventing the generation of new sub-commands
parsers.default = nil parsers.default = nil
parsers
end
def parse_options(argv = ARGV)
main_parser = create_default_parser
sub_parsers = create_sub_parser
# Usage and help messages # Usage and help messages
main_parser.banner = \ main_parser.banner = \
@ -209,7 +214,7 @@ module Uplot
Usage: uplot <command> [options] Usage: uplot <command> [options]
Command: #{parsers.keys.join(' ')} Command: #{sub_parsers.keys.join(' ')}
Options: Options:
MSG MSG
@ -223,7 +228,7 @@ module Uplot
@command = argv.shift&.to_sym @command = argv.shift&.to_sym
unless parsers.has_key?(command) unless sub_parsers.has_key?(command)
if command.nil? if command.nil?
warn main_parser.help warn main_parser.help
else else
@ -231,7 +236,7 @@ module Uplot
end end
exit 1 exit 1
end end
parser = parsers[command] parser = sub_parsers[command]
begin begin
parser.parse!(argv) unless argv.empty? parser.parse!(argv) unless argv.empty?