From 1fac4efe4799b251873c456f8e429a52eec662ea Mon Sep 17 00:00:00 2001 From: Olle Jonsson Date: Sat, 14 Oct 2017 21:28:02 +0200 Subject: [PATCH 1/2] [docs] API docs for Options, Parser - shorten implementation of print_options --- lib/github_changelog_generator/options.rb | 17 +++++++++++++++-- lib/github_changelog_generator/parser.rb | 23 ++++++++++++----------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/github_changelog_generator/options.rb b/lib/github_changelog_generator/options.rb index 70fa3d6..2d73caa 100644 --- a/lib/github_changelog_generator/options.rb +++ b/lib/github_changelog_generator/options.rb @@ -2,9 +2,14 @@ require "delegate" module GitHubChangelogGenerator + # This class wraps Options, and knows a list of known options. Others options + # will raise exceptions. 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) + # List of valid option names KNOWN_OPTIONS = %i[ add_issues_wo_labels add_pr_wo_labels @@ -56,23 +61,31 @@ module GitHubChangelogGenerator verbose ] + # @param values [Hash] + # + # @raise [UnsupportedOptionError] if given values contain unknown options def initialize(values) super(values) unsupported_options.any? && raise(UnsupportedOptionError, unsupported_options.inspect) end + # Set option key to val. + # + # @param key [Symbol] + # @param val [Object] + # + # @raise [UnsupportedOptionError] when trying to set an unknown option def []=(key, val) supported_option?(key) || raise(UnsupportedOptionError, key.inspect) values[key] = val end + # @return [Hash] def to_hash values end # Loads the configured Ruby files from the --require option. - # - # @return [void] def load_custom_ruby_files self[:require].each { |f| require f } end diff --git a/lib/github_changelog_generator/parser.rb b/lib/github_changelog_generator/parser.rb index 9c8cf88..747e3d9 100755 --- a/lib/github_changelog_generator/parser.rb +++ b/lib/github_changelog_generator/parser.rb @@ -5,6 +5,7 @@ require "optparse" require "pp" require_relative "version" require_relative "helper" + module GitHubChangelogGenerator class Parser # parse options with optparse @@ -26,23 +27,23 @@ module GitHubChangelogGenerator options end - # If options set to verbose, print the parsed options. + # Pretty-print the parsed options. # # 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 def self.print_options(options) - if options[:verbose] - Helper.log.info "Performing task with options:" - options_to_display = options.clone - options_to_display[:token] = options_to_display[:token].nil? ? nil : "hidden value" - pp options_to_display - puts "" - end + return unless options[:verbose] + + Helper.log.info "Using these options:" + pp(options.clone.tap { |opts| opts[:token] = opts[:token].nil? ? "No token used" : "hidden value" }) + puts "" end - # setup parsing options + # Setup parsing options + # + # @param options [Options] def self.setup_parser(options) parser = OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength opts.banner = "Usage: github_changelog_generator [options]" @@ -199,7 +200,7 @@ module GitHubChangelogGenerator parser end - # @return [Hash] Default options + # @return [Options] Default options def self.default_options Options.new( date_format: "%Y-%m-%d", From 5bb4d51b96adb472d8949abd5a9ce33329d39d84 Mon Sep 17 00:00:00 2001 From: Olle Jonsson Date: Sat, 14 Oct 2017 21:52:26 +0200 Subject: [PATCH 2/2] Options#print_options: tell don't ask --- .../generator/generator.rb | 10 ++++---- lib/github_changelog_generator/options.rb | 17 +++++++++++++ lib/github_changelog_generator/parser.rb | 24 ++++--------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/github_changelog_generator/generator/generator.rb b/lib/github_changelog_generator/generator/generator.rb index d5cd207..2d87a8a 100644 --- a/lib/github_changelog_generator/generator/generator.rb +++ b/lib/github_changelog_generator/generator/generator.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative "../octo_fetcher" -require_relative "generator_generation" -require_relative "generator_fetcher" -require_relative "generator_processor" -require_relative "generator_tags" +require "github_changelog_generator/octo_fetcher" +require "github_changelog_generator/generator/generator_generation" +require "github_changelog_generator/generator/generator_fetcher" +require "github_changelog_generator/generator/generator_processor" +require "github_changelog_generator/generator/generator_tags" module GitHubChangelogGenerator # Default error for ChangelogGenerator diff --git a/lib/github_changelog_generator/options.rb b/lib/github_changelog_generator/options.rb index 2d73caa..b68b5da 100644 --- a/lib/github_changelog_generator/options.rb +++ b/lib/github_changelog_generator/options.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true require "delegate" +require "github_changelog_generator/helper" + module GitHubChangelogGenerator # This class wraps Options, and knows a list of known options. Others options # will raise exceptions. @@ -90,12 +92,27 @@ module GitHubChangelogGenerator self[:require].each { |f| require f } end + # Pretty-prints a censored options hash, if :verbose. + def print_options + return unless self[:verbose] + Helper.log.info "Using these options:" + pp(censored_values) + puts "" + end + private def values __getobj__ end + # Returns a censored options hash. + # + # @return [Hash] The GitHub `:token` key is censored in the output. + def censored_values + values.clone.tap { |opts| opts[:token] = opts[:token].nil? ? "No token used" : "hidden value" } + end + def unsupported_options values.keys - KNOWN_OPTIONS end diff --git a/lib/github_changelog_generator/parser.rb b/lib/github_changelog_generator/parser.rb index 747e3d9..4e9fbbb 100755 --- a/lib/github_changelog_generator/parser.rb +++ b/lib/github_changelog_generator/parser.rb @@ -3,8 +3,8 @@ require "optparse" require "pp" -require_relative "version" -require_relative "helper" +require "github_changelog_generator/version" +require "github_changelog_generator/helper" module GitHubChangelogGenerator class Parser @@ -22,30 +22,17 @@ module GitHubChangelogGenerator abort(parser.banner) unless options[:user] && options[:project] - print_options(options) + options.print_options options end - # Pretty-print the parsed options. - # - # The GitHub `:token` key is censored in the output. - # - # @param options [Options] The options to display - # @option options [Boolean] :verbose If false this method does nothing - def self.print_options(options) - return unless options[:verbose] - - Helper.log.info "Using these options:" - pp(options.clone.tap { |opts| opts[:token] = opts[:token].nil? ? "No token used" : "hidden value" }) - puts "" - end - # Setup parsing options # # @param options [Options] + # @return [OptionParser] def self.setup_parser(options) - parser = OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength + OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength opts.banner = "Usage: github_changelog_generator [options]" opts.on("-u", "--user [USER]", "Username of the owner of target GitHub repo") do |last| options[:user] = last @@ -197,7 +184,6 @@ module GitHubChangelogGenerator exit end end - parser end # @return [Options] Default options