No release in over 3 years
see: https://usage.jdx.dev/
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 2.0
 Project Readme

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_spec

This 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.