Wrapper for command line parser
Singleton
Initialize args for new that we can't have because it's a singleton.
- usage
- top part of message shown for help option
- banner
- message shown for version and copyright info
# File newfile.rb, line 378
def self.set_text(usage, banner)
@@usage = usage
@@banner = banner
end
Set up allowable options for parser.
# File newfile.rb, line 385
def initialize()
@opts = Hash.new
@args = nil
@parser = OptionParser.new(@@usage, Width)
# set default values
$verbose =
@opts["v"] = false
@opts["myname"] = APPNAME
@opts["path"] = []
@opts["defs"] = {}
@opts["template"] = "generic"
@opts["license"] = "default"
@opts["exec"] = false
pw = Etc.getpwuid
@opts["author"] = pw.gecos
@opts["email"] = pw.name + "@" + Socket.gethostname
# define program options
@parser.def_option("-h", "--help",
"Show this help.") {
show_help
}
@parser.def_option("-V", "--version",
"Show version and copyright.") {
show_banner
}
@parser.def_option("-v", "--verbose",
"Turn on verbose messages.") {
|v| $verbose = @opts["v"] = v
}
@parser.def_option("-l", "--license=LICENSE",
"Set license type.") {
|license|
@opts["license"] = license
}
@parser.def_option("-t", "--filetype=TEMPLATE",
"Set file TEMPLATE to use.#{NL}" +
"Do *not* use with -p/--project.") {
|tmpl|
@opts["template"] = tmpl
@opts["project"] = nil
}
@parser.def_option("-p", "--project=TEMPLATE.PROJECTNAME",
"Set project TEMPLATE to use, and#{NL}" +
"PROJECTNAME of project being built.#{NL}" +
"Do *not* use with -t/--filetype.") {
|proj|
arr = proj.split(".")
@opts["template"] = arr[0]
@opts["project"] = arr[1]
}
@parser.def_option("-s", "--show",
"Show installed file templates, project#{NL}" +
"templates, and license types.") {
|show|
@opts["show"] = true
@parser.terminate
}
@parser.def_option("-P", "--pdoc=PROJECT",
"Show embedded docs for PROJECT.") {
|pdoc|
@opts["pdoc"] = pdoc
@parser.terminate
}
@parser.def_option("-T", "--doc=TEMPLATE",
"Show embedded docs for TEMPLATE.") {
|doc|
@opts["doc"] = doc
@parser.terminate
}
@parser.def_option("-x", "--exec-bit",
"Make the resulting file executable.#{NL}" +
"This option has no effect if a project#{NL}" +
"template is selected.") {
|x|
@opts["exec"] = x
}
@parser.def_option("-a", "--author=AUTHOR",
"Set name of author.") {
|auth|
@opts["author"] = auth
}
@parser.def_option("-e", "--email=EMAIL",
"Set author's email address.") {
|email|
@opts["email"] = email
}
@parser.def_option("-o", "--organization=ORGANIZATION",
"Set author's organization name.") {
|org|
@opts["org"] = org
}
@parser.def_option("-c", "--owner=COPYRIGHT_OWNER",
"Set copyright owner's name.") {
|owner|
@opts["owner"] = owner
}
@parser.def_option("-I", "--include=DIR",
"Add DIR to search path.") {
|dir|
@opts["path"].push(File.expand_path(dir)) if File.directory?(dir)
}
@parser.def_option("-D", "--define=VAR[=value]",
"Define a variable. Value defaults to 1.") {
|defn|
vv = defn.split("=")
vv.push("1") if vv.size < 2
@opts["defs"][vv[0]] = vv[1]
}
end
- s
- name of env var to parse for command line options
# File newfile.rb, line 502
def env(s)
envopts = ENV[s] || ENV[s.upcase]
@parser.order(*Shellwords::shellwords(envopts)) if envopts
end
- av
- array of command line options, usually ARGV passed in,
in one form or another
# File newfile.rb, line 510
def parse(av)
env("NEWFILE")
env("NEWFILE_OPTS")
@args = @parser.order(*av)
@opts["org"] = @auth if ! @opts["org"]
case @opts["owner"]
when "org", "organization"
@opts["owner"] = @opts["org"]
when nil, "auth", "author"
@opts["owner"] = @opts["author"]
end
@args
end
# File newfile.rb, line 526
def show_help(rc = 0)
print @parser; exit(rc)
end
# File newfile.rb, line 532
def show_banner(rc = 0)
print @@banner; exit(rc)
end