I'm using trollop to parse command line arguments, and I want to add some required positional arguments like filename1
and filename2
below:
usage: my_script [--help] [--some-option VALUE] [--some-flag] filename1 filename2
How do I do this with trollop?
Trollop also gives you the leftovers
attribute, that provides the same information. Here's how you'd use it:
#!/usr/bin/env ruby
require 'trollop'
require 'pp'
p = Trollop::Parser.new 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.parse
puts 'Leftovers:'
pp p.leftovers
puts 'ARGV:'
pp ARGV
Here's the result:
> ruby se_01.rb foo blarb -m
Leftovers:
["foo", "blarb"]
ARGV:
["foo", "blarb"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With