import config
class MyConfig
super Config
var opt_my = new OptionString("My option", "--my")
init do
super
tool_description = "Usage: MyExample [OPTION]... [ARGS]..."
opts.add_option(opt_my)
end
fun my: String do return opt_my.value or else "Default value"
end
var config = new MyConfig
config.parse_options(["--my", "hello", "arg1", "arg2"])
assert config.my == "hello"
assert config.args == ["arg1", "arg2"]
config :: Config :: defaultinit
config :: Config :: opt_black_exts=
--blacklist-extsconfig :: Config :: opt_black_exts=
--blacklist-extsconfig :: Config :: opt_java_cp
config :: Config :: opt_java_cp=
config :: Config :: opt_stub_man=
Option --stub-manconfig :: Config :: opt_white_exts=
--whitelist-extsconfig :: Config :: opt_white_exts=
--whitelist-extsconfig :: Config :: parse_options
Initializeself
options from args
config :: Config :: tool_description
Name, usage and synopsis of the tool.config :: Config :: tool_description=
Name, usage and synopsis of the tool.vsm :: example_vsm $ Config :: init
nlp :: nlp_server $ Config :: init
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Object :: defaultinit
config :: Config :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
config :: Config :: opt_black_exts=
--blacklist-extsconfig :: Config :: opt_black_exts=
--blacklist-extsconfig :: Config :: opt_java_cp
config :: Config :: opt_java_cp=
config :: Config :: opt_stub_man=
Option --stub-manconfig :: Config :: opt_white_exts=
--whitelist-extsconfig :: Config :: opt_white_exts=
--whitelist-extscore :: Object :: output_class_name
Display class name on stdout (debug only).config :: Config :: parse_options
Initializeself
options from args
config :: Config :: tool_description
Name, usage and synopsis of the tool.config :: Config :: tool_description=
Name, usage and synopsis of the tool.
# Basic configuration class
#
# ~~~
# import config
#
# class MyConfig
# super Config
#
# var opt_my = new OptionString("My option", "--my")
#
# init do
# super
# tool_description = "Usage: MyExample [OPTION]... [ARGS]..."
# opts.add_option(opt_my)
# end
#
# fun my: String do return opt_my.value or else "Default value"
# end
#
# var config = new MyConfig
# config.parse_options(["--my", "hello", "arg1", "arg2"])
# assert config.my == "hello"
# assert config.args == ["arg1", "arg2"]
# ~~~
class Config
# Context used to store and parse options
var opts = new OptionContext
# Help option
var opt_help = new OptionBool("Show this help message", "-h", "-?", "--help")
# Option --stub-man
var opt_stub_man = new OptionBool("Generate a stub manpage in markdown format", "--stub-man")
# Redefine this init to add your options
init do
add_option(opt_help, opt_stub_man)
opt_stub_man.hidden = true
end
# Add an option to `self`
#
# Shortcut to `opts.add_option`.
fun add_option(opt: Option...) do opts.add_option(opt...)
# Initialize `self` options from `args`
fun parse_options(args: Collection[String]) do
opts.parse(args)
if opt_stub_man.value then
stub_man_options
exit 0
end
end
# Return the remaining args once options are parsed by `from_args`
fun args: Array[String] do return opts.rest
# Name, usage and synopsis of the tool.
# It is mainly used in `usage`.
# Should be correctly set by the client before calling `usage`
# A multi-line string is recommended.
#
# eg. `"Usage: tool [OPTION]... [FILE]...\nDo some things."`
var tool_description: String = "Usage: [OPTION]... [ARG]..." is writable
# Was the `--help` option requested?
fun help: Bool do return opt_help.value
# Display `tool_description` and options usage in console
fun usage do
print tool_description
print "\nOptions:"
opts.usage
end
# Generate a manpage stub from `self`
fun stub_man_options do
var lines = tool_description.split("\n")
var name = sys.program_name.basename
var syn = lines.shift
print "# NAME"
print ""
if lines.not_empty then
printn "{name} - "
print lines.join("\n")
print ""
end
print "# SYNOPSIS"
print ""
print syn.replace("Usage: ", "")
print ""
print "# OPTIONS"
for o in opts.options do
if o.hidden then continue
var first = true
print ""
printn "### "
for n in o.names do
if first then first = false else printn ", "
printn "`{n}`"
end
print ""
print "{o.helptext}."
end
exit 0
end
end
lib/config/config.nit:159,1--268,3
redef class Config
# --whitelist-exts
var opt_white_exts = new OptionArray("Allowed file extensions (default is [])",
"-w", "--whitelist-exts")
# --blacklist-exts
var opt_black_exts = new OptionArray("Allowed file extensions (default is [])",
"-b", "--blacklist-exts")
redef init do
opts.add_option(opt_white_exts, opt_black_exts)
end
end
lib/vsm/examples/example_vsm.nit:24,1--37,3
redef class Config
# --whitelist-exts
var opt_white_exts = new OptionArray("Allowed file extensions (default is [])",
"-w", "--whitelist-exts")
# --blacklist-exts
var opt_black_exts = new OptionArray("Allowed file extensions (default is [])",
"-b", "--blacklist-exts")
# --server
var opt_server = new OptionString("StanfordNLP server URI (default is https://localhost:9000)",
"-s", "--server")
# --lang
var opt_lang = new OptionString("Language to use (default is fr)", "-l", "--lang")
redef init do
opts.add_option(opt_server, opt_lang, opt_white_exts, opt_black_exts)
end
end
lib/nlp/examples/nlp_index.nit:21,1--41,3
redef class Config
var opt_java_cp = new OptionString("StanfordNLP java classpath", "-c", "--classpath")
var opt_port = new OptionInt("Server port on localhost (default is 9000)", 9000, "-p", "--port")
redef init do
opts.add_option(opt_java_cp, opt_port)
end
end
lib/nlp/examples/nlp_server.nit:20,1--28,3