BlackHatSamurai
BlackHatSamurai

Reputation: 23503

Command Line Arguments with Sinatra

I have a Sinatra program that I am creating, and I would like to be able to pass in command line arguments to this app when I launch it. The problem that I have is that I'm not sure how to do this. I've tried Trollop and looked at OptParser. Trollop doesn't appear to work with Sinatra because OptParser seems to be "default" parser with Sinatra. Is this true? If so, how can I customize the types of arguments accepted when I launch my app?

Upvotes: 4

Views: 1655

Answers (2)

Luke Knepper
Luke Knepper

Reputation: 961

Alternatively, you can use environment variables.

Example borrowed from here: https://gist.github.com/benlovell/351962

require 'rubygems'
require 'sinatra'

get '/' do
  ENV['envvar']
end

Then run:

envvar=something ruby app.rb

Upvotes: 1

sunnyrjuneja
sunnyrjuneja

Reputation: 6123

ruby app.rb hello
# app.rb
 require 'sinatra'

get '/' do
  ARGV[0]
end

Now when I visit localhost:4567 (where Thin hosts my sinatra app), I see a page that says hello.

Upvotes: 3

Related Questions