Skip to content

Banner, Synopsis and Version

Jason Frey edited this page Aug 24, 2018 · 3 revisions

Synopsis

Synopsis can be used to override the default header of the message. It will also print the version if version is specified.

opts = Optimist::options do
  synopsis "Overall synopsis of this program"
  version "cool-script v0.3 (code-name: apple-cake)"
  opt :juice, "use juice"
  opt :milk, "use milk"
  opt :litres, "quantity of liquid", :default => 2.0
  opt :brand, "brand name of the liquid", :type => :string
end
$ ./banners1.rb -h
Overall synopsis of this program

cool-script v0.3 (code-name: apple-cake)
Options:
  -j, --juice         use juice
  -m, --milk          use milk
  -l, --litres=<f>    quantity of liquid (default: 2.0)
  -b, --brand=<s>     brand name of the liquid
  -v, --version       Print version and exit
  -h, --help          Show this message

Banner overrides synopsis

opts = Optimist::options do
  synopsis "Overall synopsis of this program"
  version "cool-script v0.3.1 (code-name: apple-cake)"
  banner "My Banner"
  opt :juice, "use juice"
  opt :milk, "use milk"
  opt :litres, "quantity of liquid", :default => 2.0
  opt :brand, "brand name of the liquid", :type => :string
end

Note that the banner directive overrode the default message that contained both the synopsis and version.

$ ./banners2.rb -h
My Banner
  -j, --juice         use juice
  -m, --milk          use milk
  -l, --litres=<f>    quantity of liquid (default: 2.0)
  -b, --brand=<s>     brand name of the liquid
  -v, --version       Print version and exit
  -h, --help          Show this message

Multiple banners, re-including the version.

You can re-introduce the version-string into your custom banners using self.version Multiple banners are inserted in the order they are given, before/between/after the options they are adjacent to.

opts = Optimist::options do
  version "cool-script v0.3.2 (code-name: apple-cake)"
  banner self.version  ## print out the version in the banner
  banner "drinks"
  opt :juice, "use juice"
  opt :milk, "use milk"
  banner "drink control"    ## can be used for categories
  opt :litres, "quantity of liquid", :default => 2.0
  opt :brand, "brand name of the liquid", :type => :string
  banner "other controls"
end
$ ./banners3.rb -h
cool-script v0.3.2 (code-name: apple-cake)
drinks
  -j, --juice         use juice
  -m, --milk          use milk
drink control
  -l, --litres=<f>    quantity of liquid (default: 2.0)
  -b, --brand=<s>     brand name of the liquid
other controls
  -v, --version       Print version and exit
  -h, --help          Show this message