Project

topt

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Thor compatible command line option parser. A replacement for Ruby's OptionParser (optparse).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0
~> 1.3
~> 0.9
~> 3.9
~> 2.3
~> 0.4
 Project Readme

Topt

A Thor compatible replacement for Ruby's OptionParser (optparse).

Provides extended options parsing, compared to optparse, using a Thor compatible option parser.

About

This is still a very fresh extraction. I haven't created any specs yet, so use with caution.

Open an issue if you want to use this but you're scared.

Example

See the examples directory from examples.

class Command
  extend Topt

  option :verbose, type: :boolean, aliases: %w[-v], :default => false, desc: "Be noisy"
  argument :filename

  def initialize(given_args=ARGV)
    @arguments, @options, @extras = self.class.parse_options!(given_args)
  rescue Topt::Error => e
    puts e.message
    puts "Usage: command [--verbose] filename"
    exit 1
  end

  def call
    filename = @arguments[:filename]
    puts "Opening file #{filename}" if @options[:verbose]

    File.open(filename) do |f|
      f.each_line do |line|
        puts "outputting line #{line}" if @options[:verbose]
        puts line
      end
    end

    puts "Done" if @options[:verbose]
  end
end

Command.new(ARGV).call

Credit

A substantial portion of this code is extracted directly from Thor by Yehuda Katz, José Valim, and more.