

To (partially) recreate the rails command you can do something like this:Ĭlass RailsCLI :boolean, :aliases => "-v"ĭesc "new DIRECTORY", "Create a new rails app"ĭesc "generate THING PARAMETERS", "Generate controller / model / migration" If you are looking for a different approach to option parsing you should take a look at the Thor gem.
MAC UPDATE RUBY TO 2.3 HOW TO
The help menu is automatically built for you, so you don’t have to manually update it every time you add or remove optionsĪll of this is built into Ruby so there is nothing to install! How to Use The Thor Gem.Every option has a description, which will be part of the help menu.You can have the short -v & the long version -verbose of every flag without extra work.P get several benefits when using optparse over your own custom solution. Opts.on("-c", "-color", "Enable syntax highlighting") = true Opts.on("-v", "-verbose", "Show extra information") = true Ruby comes with the OptionParser class that you can use to parse command-line options. This works for simple things, but if you want to write a bigger application you may want to consider other solutions. You can check if a specific option has been passed by implementing an option parser.ĪRGV.each
MAC UPDATE RUBY TO 2.3 CODE
If you have the following code in a file named argv.rb:Īnd if you run this code using ruby argv.rb test abc 1 2 3, you’ll get this: How can you access these options in Ruby? Let’s get started! The Ruby ARGV ConstantĬommand-line applications usually take multiple options or ‘flags’. There are many ways to build a command-line application & in this article we are going to focus on three of them. Here are some command-line applications you may be familiar with:

In this article, I want to show you how to build a command-line application to help remedy that! Many people forget that Ruby can do things that aren’t web applications.
