Args Parser

Parse/Filter/Validate ARGV from command line with DSL.

This project is maintained by shokai

INSTALL:

% gem install args_parser

SYNOPSIS:

% ruby samples/download_webpage.rb --url http://example.com -o out.html

parse ARGV

require 'rubygems'
require 'args_parser'

args = ArgsParser.parse ARGV do
  arg :url, 'URL', :alias => :u
  arg :output, 'output file', :alias => :o, :default => 'out.html'
  arg :verbose, 'verbose mode'
  arg :help, 'show help', :alias => :h

  filter :url do |v|
    v.to_s.strip
  end

 validate :url, "invalid URL" do |v|
    v =~ /^https?:\/\/.+$/
  end
end

if args.has_option? :help or !args.has_param?(:url, :output)
  STDERR.puts args.help
  exit 1
end

require 'open-uri'
puts 'download..' if args[:verbose]
open(args[:output], 'w+') do |f|
  f.write open(args[:url]).read
end
puts "saved! => #{args[:output]}"

equal style

% ruby samples/twitter_timeline.rb --user=shokai --fav --rt

parse equal style ARGV

args = ArgsParser.parse ARGV, :style => :equal do

see more samples