[docs] API docs for Options, Parser

- shorten implementation of print_options
This commit is contained in:
Olle Jonsson 2017-10-14 21:28:02 +02:00
parent ad0d972ed9
commit 1fac4efe47
2 changed files with 27 additions and 13 deletions

View File

@ -2,9 +2,14 @@
require "delegate" require "delegate"
module GitHubChangelogGenerator module GitHubChangelogGenerator
# This class wraps Options, and knows a list of known options. Others options
# will raise exceptions.
class Options < SimpleDelegator class Options < SimpleDelegator
# Raised on intializing with unknown keys in the values hash,
# and when trying to store a value on an unknown key.
UnsupportedOptionError = Class.new(ArgumentError) UnsupportedOptionError = Class.new(ArgumentError)
# List of valid option names
KNOWN_OPTIONS = %i[ KNOWN_OPTIONS = %i[
add_issues_wo_labels add_issues_wo_labels
add_pr_wo_labels add_pr_wo_labels
@ -56,23 +61,31 @@ module GitHubChangelogGenerator
verbose verbose
] ]
# @param values [Hash]
#
# @raise [UnsupportedOptionError] if given values contain unknown options
def initialize(values) def initialize(values)
super(values) super(values)
unsupported_options.any? && raise(UnsupportedOptionError, unsupported_options.inspect) unsupported_options.any? && raise(UnsupportedOptionError, unsupported_options.inspect)
end end
# Set option key to val.
#
# @param key [Symbol]
# @param val [Object]
#
# @raise [UnsupportedOptionError] when trying to set an unknown option
def []=(key, val) def []=(key, val)
supported_option?(key) || raise(UnsupportedOptionError, key.inspect) supported_option?(key) || raise(UnsupportedOptionError, key.inspect)
values[key] = val values[key] = val
end end
# @return [Hash]
def to_hash def to_hash
values values
end end
# Loads the configured Ruby files from the --require option. # Loads the configured Ruby files from the --require option.
#
# @return [void]
def load_custom_ruby_files def load_custom_ruby_files
self[:require].each { |f| require f } self[:require].each { |f| require f }
end end

View File

@ -5,6 +5,7 @@ require "optparse"
require "pp" require "pp"
require_relative "version" require_relative "version"
require_relative "helper" require_relative "helper"
module GitHubChangelogGenerator module GitHubChangelogGenerator
class Parser class Parser
# parse options with optparse # parse options with optparse
@ -26,23 +27,23 @@ module GitHubChangelogGenerator
options options
end end
# If options set to verbose, print the parsed options. # Pretty-print the parsed options.
# #
# The GitHub `:token` key is censored in the output. # The GitHub `:token` key is censored in the output.
# #
# @param options [Hash] The options to display # @param options [Options] The options to display
# @option options [Boolean] :verbose If false this method does nothing # @option options [Boolean] :verbose If false this method does nothing
def self.print_options(options) def self.print_options(options)
if options[:verbose] return unless options[:verbose]
Helper.log.info "Performing task with options:"
options_to_display = options.clone Helper.log.info "Using these options:"
options_to_display[:token] = options_to_display[:token].nil? ? nil : "hidden value" pp(options.clone.tap { |opts| opts[:token] = opts[:token].nil? ? "No token used" : "hidden value" })
pp options_to_display puts ""
puts ""
end
end end
# setup parsing options # Setup parsing options
#
# @param options [Options]
def self.setup_parser(options) def self.setup_parser(options)
parser = OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength parser = OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength
opts.banner = "Usage: github_changelog_generator [options]" opts.banner = "Usage: github_changelog_generator [options]"
@ -199,7 +200,7 @@ module GitHubChangelogGenerator
parser parser
end end
# @return [Hash] Default options # @return [Options] Default options
def self.default_options def self.default_options
Options.new( Options.new(
date_format: "%Y-%m-%d", date_format: "%Y-%m-%d",