Sha256: 30b234c34027258d0c3df93ab17ac9b1ade1090f1dedbb13a185f2e99a6dbf26

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require 'trollop'

module ArticleFixtureGen
  module Exe
    module Trollop
      # Builds an encapsulation of Trollop option menu and default values.
      class OptionsWithDefaults
        USAGE_BANNER = \
          "Generates article/blog post fixture data based on settings.\n" \
          "Usage:\n\n" \
          "  #{File.basename($PROGRAM_NAME)} [options]\n\n" \
          'where [options] are any of:'
        private_constant :USAGE_BANNER

        VERSION_BANNER = "#{$PROGRAM_NAME} Version " \
          "#{ArticleFixtureGen::VERSION}\n" \
          'Copyright (c)2016, Jeff Dickey and Prolog Systems (S) Pte Ltd'
        private_constant :VERSION_BANNER

        # Reek says this method has :reek:TooManyStatements. Such is a DSL.
        def self.call(all_specs)
          defaults = {}
          options = ::Trollop.options do
            version VERSION_BANNER
            banner USAGE_BANNER
            all_specs.map { |spec| spec.add_option self }
            # Reminder: `all_specs` is our data. `Trollop.specs` is valid only
            #           inside the `.options` block
            specs.each { |attrib, spec| defaults[attrib] = spec[:default] }
          end
          Result.new defaults: defaults, options: options
        end

        # Encapsulates Trollop option menu and default values.
        class Result < Dry::Struct
          attribute :defaults, Types::Strict::Hash
          attribute :options, Types::Strict::Hash
        end
        private_constant :Result
      end # class ArticleFixtureGen::Exe::Trollop::OptionsWithDefaults
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
article_fixture_gen-0.1.2 lib/article_fixture_gen/exe/option_parser/trollop/options_with_defaults.rb
article_fixture_gen-0.1.1 lib/article_fixture_gen/exe/option_parser/trollop/options_with_defaults.rb