Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make posititional arguments with trollop?

Tags:

ruby

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?

like image 740
Dean Avatar asked Aug 28 '13 00:08

Dean


1 Answers

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"]
like image 105
rambler Avatar answered Nov 15 '22 08:11

rambler