OptionParserUsage
Tooling for creating usage specs from OptionParser definitions.
Installation
Add gem "option_parser_usage" to your gemfile and then bundle install.
Usage
Example:
require 'optparse'
require 'option_parser_usage'
myparser = OptionParser.new do |p|
p.program_name = "myprog"
p.version = "1.2.3"
p.banner = "my test program"
p.on("--foo", "enables foo")
p.on("--bar BAR", "requires argument BAR")
p.on("--baz [BAZ]", "optional argument BAZ")
end
puts OptionParserUsage.usage_for_parser(myparser)
# Alternatively
OptionParserUsage.monkeypatch!
puts myparser.to_usage_specThis will print (twice)
bin myprog
version "1.2.3"
about "my test program"
flag --foo {
help "enables foo"
}
flag --bar {
arg <BAR> required=#true
help "requires argument BAR"
}
flag --baz {
arg "[BAZ]" required=#false
help "optional argument BAZ"
}
Monkeypatching
OptionParserUsage.monkeypatch!
# undo with OptionParserUsage.unmonkeypatch!
This monkeypatch adds #to_usage_spec to OptionParser instances.
OptionParserUsage.document_officious_help!
# undo with OptionParserUsage.undocument_officious_help!
This monkeypatch changes the default -h/--help switch internal to the OptionParser to be documented so it can be exposed in docs
Both monkeypatches alter global state with all the requisite warnings about doing that.
Limitations
OptionParser supports a style of flag that cannot be properly rendered in usage as far as I can tell. --foo=[FOO] will enable either --foo or --foo=val but not --foo val (val will be left as a positional argument). Generating a usage spec will raise an error if one of these is encountered.
Positional arguments and generally anything parsed outside of the OptionParser will not be represented in the generated config.
Development
bundle install for the dev dependencies
bundle exec rspec to run the tests
bundle exec standardrb --fix to lint and autocorrect
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/packrat386/option_parser_usage.