Sinatra Command Line Arguments

I have a Sinatra program that I am creating and I would like to be able to pass command line arguments to this application at startup. The problem is that I'm not sure how to do this. I tried Trollop and looked at OptParser . Trollop does not seem to work with Sinatra because OptParser seems to be the "default parser" with Sinatra . It's true? If so, how can I customize the types of arguments accepted when the application starts?

+4
source share
2 answers
 ruby app.rb hello # app.rb require 'sinatra' get '/' do ARGV[0] end 

Now when I visit localhost:4567 (where Thin contains my Sinatra application), I see a page that welcomes.

+3
source

Alternatively, you can use environment variables.

An example borrowed here: https://gist.github.com/benlovell/351962

 require 'rubygems' require 'sinatra' get '/' do ENV['envvar'] end 

Then run:

 envvar=something ruby app.rb 
0
source

All Articles