#!/usr/bin/env ruby require "bundler/setup" require "md2site" require "optparse" require "simpleoptparse" opts = {} banner_command_part = %q(Usage: bundle exec ruby bin/md2site ) banner_option_part = <<~_BANNER_END #init(scafold) [-i dir] [--init dir] [--url url] [--yaml ] #setup(get contents from a web site, copy files to project directory) [-g dir] [--getfiles dir] [-w x] [-zcontents dir] [-u] [--zupdate] opts["contentUpdate"] #make(make html file) [-t targetCommand] [--target targetCommand] [-s subTargetCommand] [--suttarget subTargetCommand] #info(display information) [-z] [--zlist] [-x] [--zindex] #help #testx(execute all process for test) [-a dir] [--all dir] #(common) [-c dir] [--confpath dir] # opts['confPath'] [-d d|v] [--debug d|v] [-l file] [--logfile file] #(independent) [-v] [--version] [-h] [--help] _BANNER_END middle_state_lines = banner_option_part.split("\n").map do |y| y.split("#").first end ary = middle_state_lines.select {|x| !x.nil? && !/^\s*$/.match?(x) } BANNER = banner_command_part + " " + ary.join(" ") SUBCMDS = { "init" => "i", "setup" => "gwu", "make" => "ts", "info" => "zx", "help" => "", "testx" => "a" } def usage(exit_code) puts BANNER exit(exit_code) end def need_help?(params, subcmd) musts = SUBCMDS[subcmd] musts ? musts.each_char.none? {|must| params[must] } : false end def check_file_exist(*args) args.shift unless args.first path = File.join(*args) File.file?(path) ? path : nil end def check_option_confpath(dir=nil) if (path = check_file_exist(dir)) # return path elsif (path = check_file_exist(dir, Md2site::CONF_FILE)) # return path elsif (path = check_file_exist(dir, Md2site::CONF_DIR, Md2site::CONF_FILE)) # return path else puts "Error: Not specified -c dir or -c file correctory" puts usage(0) end path end if ARGV && ARGV[0] && (ARGV[0][0] != "-") cmd = ARGV.shift end opts["debug"] = "" Simpleoptparse::Simpleoptparse.parse(ARGV, opts, BANNER, Md2site::VERSION, nil) do |parser| parser.on("-i dir", "--init") {|x| opts["init"] = opts["i"] = x } parser.on("--url url") {|x| opts["url"] = x } parser.on("-w dir", "--zcontents") {|x| opts["zcontents"] = opts["w"] = x } parser.on("-g dir", "--getfiles") {|x| opts["getfiles"] = opts["g"] = x } parser.on("-u", "--zupdate") {|_x| opts["contentUpdate"] = opts["u"] = true } parser.on("-t target", "--target") {|x| opts["targetCommand"] = opts["t"] = x } parser.on("-s subtarget", "--subtarget") {|x| opts["subTargetCommand"] = opts["s"] = x } parser.on("-z", "--zlist") {|_x| opts["zlist"] = opts["z"] = true } parser.on("-x", "--zindex") {|_x| opts["zindex"] = opts["x"] = true } parser.on("-a dir", "--all") {|x| opts["all"] = opts["a"] = x } parser.on("-c confpath", "--confpath") {|x| opts["confPath"] = opts["c"] = x } parser.on("-d [d|v]", "--debug") {|x| opts["debug"] = opts["d"] = x } parser.on("-l file", "--logfile") {|x| opts["logfile"] = opts["l"] = x } parser.on("-h", "--help") {|_x| opts["help"] = opts["h"] = true } end if opts["debug"] == "d" opts["debug"] = "debug" elsif opts["debug"] == "v" opts["debug"] = "verbose" elsif opts["debug"] == "" opts["debug"] = nil else opts["debug"] = "debug" end usage(0) if (cmd == "help") || (cmd == "-h") || opts["h"] || opts["help"] unless cmd puts "Error: No subcommand" puts usage(0) end if (cmd != "init") && (cmd != "testx") opts["confPath"] = check_option_confpath(opts["confPath"]) else unless opts["url"] puts "Error: Not specified --url url" puts usage(0) end end unless SUBCMDS.keys.include?(cmd) puts "Error: Invalid subcommand" puts usage(0) end if need_help?(opts, cmd) puts "Error: Some needed parameters are ignored" puts usage(1) end md2 = Md2site::Md2site.new(opts) md2.execute_command(cmd)