Sha256: 40925bfdaabf443233b7fb0770b900a61b496d00705131aecc94b00a1d204879

Contents?: true

Size: 1.38 KB

Versions: 9

Compression:

Stored size: 1.38 KB

Contents

require 'optparse'
require 'optparse/time'
require 'ostruct'

module Forgitter
  module CLI
    class OptionParser
      attr_accessor :options, :opt_parser

      def initialize
        # The options specified on the command line will be collected in *options*.
        # We set default values here.
        @options = Forgitter::DEFAULT_OPTIONS

        @opt_parser = ::OptionParser.new do |opts|
          opts.banner = 'Usage: forgitter [-l] TAG1 [TAG2 ...]'

          opts.on('-l', '--list',
                  'Instead of generating a .gitignore, list the ignorefiles that match the tags.') do
            options[:list] = true
          end

          # No argument, shows at tail.  This will print an options summary.
          # Try it and see!
          opts.on_tail('-h', '--help', 'Show this message') do
            puts opts
            exit
          end

          # Another typical switch to print the version.
          opts.on_tail('-v', '--version', 'Show version') do
            puts Forgitter::VERSION
            exit
          end
        end

      end

      def help
        opt_parser.help
      end

      ##
      # Return a structure describing the options.
      #
      def parse(args)
        begin      
          opt_parser.parse!(args)
        rescue ::OptionParser::InvalidOption => e
          puts help
          exit(1)
        end
        options
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
forgitter-0.1.4 lib/forgitter/cli/option_parser.rb
forgitter-0.1.3 lib/forgitter/cli/option_parser.rb
forgitter-0.1.2 lib/forgitter/cli/option_parser.rb
forgitter-0.1.1 lib/forgitter/cli/option_parser.rb
forgitter-0.1.0 lib/forgitter/cli/option_parser.rb
forgitter-0.0.12 lib/forgitter/cli/option_parser.rb
forgitter-0.0.11 lib/forgitter/cli/option_parser.rb
forgitter-0.0.10 lib/forgitter/cli/option_parser.rb
forgitter-0.0.9 lib/forgitter/cli/option_parser.rb