Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding some examples #161

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/a_basic_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env ruby
require_relative '../lib/optimist'

opts = Optimist::options do
opt :monkey, "Use monkey mode" # flag --monkey, default false
opt :name, "Monkey name", :type => :string # string --name <s>, default nil
opt :num_limbs, "Number of limbs", :default => 4 # integer --num-limbs <i>, default to 4
end
p opts

11 changes: 11 additions & 0 deletions examples/banners1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
require_relative '../lib/optimist'

Optimist::options do
synopsis "Overall synopsis of this program"
version "cool-script v0.3 (code-name: apple-cake)"
opt :juice, "use juice"
opt :milk, "use milk"
opt :litres, "quantity of liquid", :default => 2.0
opt :brand, "brand name of the liquid", :type => :string
end
12 changes: 12 additions & 0 deletions examples/banners2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env ruby
require_relative '../lib/optimist'

Optimist::options do
synopsis "Overall synopsis of this program"
version "cool-script v0.3.1 (code-name: apple-cake)"
banner "My Banner"
opt :juice, "use juice"
opt :milk, "use milk"
opt :litres, "quantity of liquid", :default => 2.0
opt :brand, "brand name of the liquid", :type => :string
end
14 changes: 14 additions & 0 deletions examples/banners3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby
require_relative '../lib/optimist'

Optimist::options do
version "cool-script v0.3.2 (code-name: apple-cake)"
banner self.version ## print out the version in the banner
banner "drinks"
opt :juice, "use juice"
opt :milk, "use milk"
banner "drink control" ## can be used for categories
opt :litres, "quantity of liquid", :default => 2.0
opt :brand, "brand name of the liquid", :type => :string
banner "other controls"
end
15 changes: 15 additions & 0 deletions examples/medium_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby
require_relative '../lib/optimist'

opts = Optimist::options do
version "cool-script v0.1 (code-name: bananas foster)"
banner "This script is pretty cool."
opt :juice, "use juice"
opt :milk, "use milk"
opt :litres, "quantity of liquid", :default => 2.0
opt :brand, "brand name of the liquid", :type => :string
opt :config, "config file path", :type => String, :required => true
opt :drinkers, "number of people drinking the liquid", :default => 6
end
Optimist::die :drinkers, "must be value a greater than zero" if opts[:drinkers] < 1
Optimist::die :config, "must point to an existing file" unless File.exist?(opts[:config]) if opts[:config]
18 changes: 18 additions & 0 deletions examples/partialmatch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
require_relative '../lib/optimist'

opts = Optimist::options(ARGV, exact_match: false) do
opt :apple, "An apple"
opt :apple_sauce, "Cooked apple puree"
opt :atom, "Smallest unit of ordinary matter"
opt :anvil, "Heavy metal"
opt :anteater, "Eats ants"
end
p opts

# $ ./partialmatch.rb --anv 1
# {:apple=>false, :apple_sauce=>false, :atom=>false, :anvil=>true, :anteater=>false, :help=>false, :anvil_given=>true}
#
# $ ./partialmatch.rb --an 1
# Error: ambiguous option '--an' matched keys (anvil,anteater).
# Try --help for help.
29 changes: 29 additions & 0 deletions examples/permitted.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby
require_relative '../lib/optimist'

opts = Optimist::options do
opt :french, "starts with french", type: String,
permitted: %w(fries toast),
permitted_response: "option %{arg} must be something that starts " +
"with french, e.g. %{permitted} but you gave '%{given}'"
opt :dog, "starts with dog", permitted: %r/(house|bone|tail)/, type: String
opt :zipcode, "zipcode", permitted: %r/^[0-9]{5}$/, default: '39759',
permitted_response: "option %{arg} must be a zipcode, a five-digit number from 00000..99999"
opt :adult, "adult age", permitted: (18...99), type: Integer
opt :minor, "minor age", permitted: (0..18), type: Integer
opt :letter, "a letter", permitted: ('a'...'z'), type: String
end

p opts

# $ ./permitted.rb -z 384949
# Error: option -z must be a zipcode, a five-digit number from 00000..99999.
# Try --help for help.
#
# $ ./permitted.rb --minor 19
# Error: option '--minor' only accepts value in range of: 0..18.
# Try --help for help.
#
# $ ./permitted.rb -f frog
# Error: option -f must be something that starts with french, e.g. ["fries", "toast"] but you gave 'frog'.
# Try --help for help.
43 changes: 43 additions & 0 deletions examples/types_custom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env ruby
require_relative '../lib/optimist'

class ZipCode
REGEXP = %r/^(?<zip>[0-9]{5})(\-(?<plusfour>[0-9]{4}))?$/
def initialize(zipstring)
matcher = REGEXP.match(zipstring)
raise "Invalid zip-code" unless matcher
@zip = matcher[:zip]
@plusfour = matcher[:plusfour]
end
end

#module Optimist
class ZipCodeOption < Optimist::Option
# register_alias registers with the global parser.
register_alias :zipcode
def type_format ; "=<zip>" ; end # optional for use with help-message
def parse(paramlist, _neg_given)
paramlist.map do |plist|
plist.map do |param_string|
raise Optimist::CommandlineError, "option '#{self.name}' should be formatted as a zipcode" unless param_string=~ ZipCode::REGEXP
ZipCode.new(param_string)
end
end
end
end

opts = Optimist::options do
opt :zipcode, "United states postal code", :type => :zipcode
end

p opts[:zipcode]

# $ ./types_custom.rb --zipcode 39759
# <ZipCode:0x0000000000a34968 @zip="39759", @plusfour=nil>
#
# $ ./types_custom.rb --zipcode 39759-0001
# <ZipCode:0x000000000117a100 @zip="39759", @plusfour="0001">
#
# $ ./types_custom.rb --zipcode 384134
# Error: option 'zipcode' should be formatted as a zipcode.
# Try --help for help.