Created
May 15, 2009 08:19
-
-
Save susieyy/112115 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##################################################################### | |
# samples/a.rb | |
##################################################################### | |
require 'main' | |
ARGV.replace %w( 42 ) if ARGV.empty? | |
Main { | |
argument('foo'){ | |
required # this is the default | |
cast :int # value cast to Fixnum | |
validate{|foo| foo == 42} # raises error in failure case | |
description 'the foo param' # shown in --help | |
} | |
def run | |
p params['foo'].given? | |
p params['foo'].value | |
end | |
} | |
##################################################################### | |
# samples/b.rb | |
##################################################################### | |
require 'main' | |
ARGV.replace %w( 40 1 1 ) if ARGV.empty? | |
Main { | |
argument('foo'){ | |
arity 3 # foo will given three times | |
cast :int # value cast to Fixnum | |
validate{|foo| [40,1].include? foo} # raises error in failure case | |
description 'the foo param' # shown in --help | |
} | |
def run | |
p params['foo'].given? | |
p params['foo'].values | |
end | |
} | |
##################################################################### | |
# samples/c.rb | |
##################################################################### | |
require 'main' | |
ARGV.replace %w( foo=40 foo=2 bar=false ) if ARGV.empty? | |
Main { | |
keyword('foo'){ | |
required # by default keywords are not required | |
arity 2 | |
cast :float | |
} | |
keyword('bar'){ | |
cast :bool | |
} | |
def run | |
p params['foo'].given? | |
p params['foo'].values | |
p params['bar'].given? | |
p params['bar'].value | |
end | |
} | |
##################################################################### | |
# samples/d.rb | |
##################################################################### | |
require 'main' | |
ARGV.replace %w( --foo=40 -f2 ) if ARGV.empty? | |
Main { | |
option('foo', 'f'){ | |
required # by default options are not required, we could use 'foo=foo' | |
# above as a shortcut | |
argument_required | |
arity 2 | |
cast :float | |
} | |
option('bar=[bar]', 'b'){ # note shortcut syntax for optional args | |
# argument_optional # we could also use this method | |
cast :bool | |
default false | |
} | |
def run | |
p params['foo'].given? | |
p params['foo'].values | |
p params['bar'].given? | |
p params['bar'].value | |
end | |
} | |
##################################################################### | |
# samples/e.rb | |
##################################################################### | |
require 'main' | |
Main { | |
argument 'global-argument' | |
option 'global-option' | |
def run() puts 'global-run' end | |
mode 'a' do | |
option 'a-option' | |
end | |
mode 'b' do | |
option 'b-option' | |
def run() puts 'b-run' end | |
end | |
} | |
##################################################################### | |
# samples/f.rb | |
##################################################################### | |
require 'main' | |
Main { | |
argument('directory'){ description 'the directory to operate on' } | |
option('force'){ description 'use a bigger hammer' } | |
def run | |
puts 'this is how we run when no mode is specified' | |
end | |
mode 'compress' do | |
option('bzip'){ description 'use bzip compression' } | |
def run | |
puts 'this is how we run in compress mode' | |
end | |
end | |
mode 'uncompress' do | |
option('delete-after'){ description 'delete orginal file after uncompressing' } | |
def run | |
puts 'this is how we run in un-compress mode' | |
end | |
end | |
} | |
##################################################################### | |
# samples/g.rb | |
##################################################################### | |
require 'main' | |
ARGV.replace %w( 42 ) if ARGV.empty? | |
Main { | |
argument( 'foo' ) | |
option( 'bar' ) | |
run { puts "This is what to_options produces: #{params.to_options.inspect}" } | |
} | |
##################################################################### | |
# samples/f.rb | |
##################################################################### | |
require 'main' | |
# | |
# ./h.rb #=> forty-two | |
# ./h.rb a #=> 42 | |
# ./h.rb b #=> 42.0 | |
# | |
Main { | |
fattr :default_for_foobar => 'forty-two' | |
option(:foobar) do | |
default{ default_for_foobar } | |
end | |
mixin :foo do | |
fattr :default_for_foobar => 42 | |
end | |
mixin :bar do | |
fattr :default_for_foobar => 42.0 | |
end | |
run{ p params[:foobar].value } | |
mode :a do | |
mixin :foo | |
end | |
mode :b do | |
mixin :bar | |
end | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment