Committing before test
This commit is contained in:
+8
-75
@@ -1,11 +1,17 @@
|
||||
require "option_parser"
|
||||
require "./cligen/regex"
|
||||
require "./cligen/parsable"
|
||||
require "./cligen/annotations"
|
||||
require "./cligen/format"
|
||||
require "./cligen/regex"
|
||||
require "./cligen/flag"
|
||||
require "./cligen/command"
|
||||
require "./cligen/command_node"
|
||||
require "./cligen/app"
|
||||
|
||||
module CliGen
|
||||
VERSION = "0.1.0"
|
||||
|
||||
APPNAME = File.basename(PROGRAM_NAME)
|
||||
|
||||
record AdditionalDefaultFlag,
|
||||
short : String,
|
||||
long : String,
|
||||
@@ -26,78 +32,5 @@ module CliGen
|
||||
|
||||
annotation DefaultFlag
|
||||
end
|
||||
|
||||
macro define_section(section_name, parser)
|
||||
{{parser}}.separator ""
|
||||
{{parser}}.separator "{{section_name.id}}".colorize(:green)
|
||||
{{parser}}.separator "-------------------------------------------------------------------------------".colorize(:blue)
|
||||
end
|
||||
|
||||
|
||||
alias OnType = Tuple(String, String) | Tuple(String, String, String)
|
||||
macro define_default_flags(parser)
|
||||
CliGen.define_section("Misc Flags", {{parser}})
|
||||
{{parser}}.on("-h", "--help", "Print out this help output"){ abort {{parser}} }
|
||||
{{parser}}.invalid_option{|opt|
|
||||
abort "#{CliGen::APPNAME} : invalid_option : Inavlid option provided #{opt}"
|
||||
}
|
||||
{{parser}}.invalid_option{|opt|
|
||||
case opt
|
||||
when /^-+[a-z-]+/
|
||||
STDERR.puts "#{CliGen::APPNAME} : ERROR : {{@type.name}} : {{parser}} : invalid_option : Inavlid option provided #{opt}"
|
||||
else
|
||||
abort "#{CliGen::APPNAME} : ERROR : {{@type.name}} : {{parser}} : invalid_option : Inavlid option provided #{opt}"
|
||||
end
|
||||
}
|
||||
{{parser}}.missing_option{|opt|
|
||||
abort "ERROR : {{@type.name}} : {{parser}} : missing_option : Argument was not provided to #{opt}"
|
||||
}
|
||||
{{parser}}.unknown_args{|opt|
|
||||
unless opt.empty?
|
||||
case opt.first
|
||||
when /^-+[a-z-]+$/
|
||||
STDERR.puts "ERROR : {{@type.name}} : {{parser}} : unknown_args : #{opt} not a configured argument"
|
||||
else
|
||||
abort "ERROR : {{@type.name}} : {{parser}} : unknown_args : #{opt} not a configured argument"
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
CliGen::ADDITIONAL_DEFAULT_FLAGS.each do |flag|
|
||||
if [ flag.short, flag.long ].all?(&.!= "")
|
||||
{{parser}}.on(flag.short, flag.long, flag.description){|var| flag.work.call(var) }
|
||||
elsif flag.short != ""
|
||||
{{parser}}.on(flag.short, flag.description){|var| flag.work.call(var) }
|
||||
elsif flag.long != ""
|
||||
{{parser}}.on(flag.long, flag.description){|var| flag.work.call(var) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
macro define_root_parser
|
||||
ROOT_COMMAND = OptionParser.new do |parser|
|
||||
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 %}
|
||||
|
||||
CliGen.define_default_flags(parser)
|
||||
|
||||
abort parser if ARGV.empty?
|
||||
end
|
||||
end
|
||||
|
||||
macro finished
|
||||
define_root_parser
|
||||
end
|
||||
|
||||
def self.parse
|
||||
ROOT_COMMAND.parse
|
||||
end
|
||||
end
|
||||
|
||||
require "./cligen/command"
|
||||
|
||||
Reference in New Issue
Block a user