Doing test
This commit is contained in:
+16
-15
@@ -3,17 +3,17 @@ require "option_parser"
|
||||
module CliGen
|
||||
VERSION = "0.1.0"
|
||||
|
||||
APPNAME = File.basename(PROGRAM_NAME)
|
||||
record AdditionalDefaultFlag,
|
||||
short : String?,
|
||||
long : String?,
|
||||
short : String,
|
||||
long : String,
|
||||
description : String,
|
||||
work : Proc(Nil)
|
||||
work : String ->
|
||||
|
||||
private ADDITIONAL_DEFAULT_FLAGS = [] of AdditionalDefaultFlag
|
||||
ADDITIONAL_DEFAULT_FLAGS = [] of AdditionalDefaultFlag
|
||||
|
||||
def self.add_default_flag(short : String? = nil, long : String? = nil, description : String? = nil, &work : -> )
|
||||
def self.add_default_flag(short : String, long : String, description : String, &work : String -> )
|
||||
raise "ERROR : add_default_flag : You must provide a description" unless description
|
||||
raise "ERROR : add_default_flag : You must provide at least long or short" unless [short, long].any?
|
||||
ADDITIONAL_DEFAULT_FLAGS << AdditionalDefaultFlag.new(
|
||||
short: short,
|
||||
long: long,
|
||||
@@ -32,18 +32,19 @@ module CliGen
|
||||
end
|
||||
|
||||
|
||||
alias OnType = Tuple(String, String) | Tuple(String, String, String)
|
||||
macro define_default_flags(parser)
|
||||
define_section("Misc Flags", {{parser}})
|
||||
CliGen.define_section("Misc Flags", {{parser}})
|
||||
{{parser}}.on("-h", "--help", "Print out this help output"){ abort {{parser}} }
|
||||
{{parser}}.invalid_option{|opt|
|
||||
abort "#{PROGRAM_NAME} : invalid_option : Inavlid option provided #{opt}"
|
||||
abort "#{CliGen::APPNAME} : invalid_option : Inavlid option provided #{opt}"
|
||||
}
|
||||
{{parser}}.invalid_option{|opt|
|
||||
case opt
|
||||
when /^-+[a-z-]+/
|
||||
Logger.debug "#{PROGRAM_NAME} : ERROR : {{parser}} : invalid_option : Inavlid option provided #{opt}"
|
||||
Logger.debug "#{CliGen::APPNAME} : ERROR : {{parser}} : invalid_option : Inavlid option provided #{opt}"
|
||||
else
|
||||
abort "#{PROGRAM_NAME} : ERROR : {{parser}} : invalid_option : Inavlid option provided #{opt}"
|
||||
abort "#{CliGen::APPNAME} : ERROR : {{parser}} : invalid_option : Inavlid option provided #{opt}"
|
||||
end
|
||||
}
|
||||
{{parser}}.missing_option{|opt|
|
||||
@@ -60,23 +61,23 @@ module CliGen
|
||||
end
|
||||
}
|
||||
|
||||
ADDITIONAL_DEFAULT_FLAGS.each do |flag|
|
||||
args = [flag.short, flag.long, flag.description].reject(&.nil?)
|
||||
{{parser}}.on(*args) &flag.work
|
||||
CliGen::ADDITIONAL_DEFAULT_FLAGS.each do |flag|
|
||||
{{parser}}.on(flag.short, flag.long, flag.description){|var| flag.work.call(var) }
|
||||
end
|
||||
end
|
||||
|
||||
macro define_root_parser
|
||||
ROOT_COMMAND = OptionParser.new do |parser|
|
||||
parser.banner = "#{PROGRAM_NAME} [command] [flags]"
|
||||
parser.banner = "#{CliGen::APPNAME} [command] [flags]"
|
||||
|
||||
{% commands = ::CliGen::Command.subclasses %}
|
||||
{% raise "ERROR : No commands defined (no subclasses of ::CliGen::Command were found)" if commands.empty? %}
|
||||
CliGen.define_section("Commands", parser)
|
||||
{% for command in commands %}
|
||||
{{command.name}}.make_parser(parser)
|
||||
{% end %}
|
||||
|
||||
define_default_flags(parser)
|
||||
CliGen.define_default_flags(parser)
|
||||
|
||||
abort parser if ARGV.empty?
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user