Building a Sub-command Ruby CLI with just OptionParser

10-Oct-2025 573
I’ve thought deeply about building CLIs and built a lot of them over the years. I’ve used Rake, Thor, my own gem GLI and many others. After all that, the venerable OptionParser—part of Ruby’s standard library—is the best choice for scripting and sub-command (git-like) CLIs. I want to show you how. What is a Sub-Command CLI? At first glance, OptionParser doesn’t seem to support a sub-command CLI, like so (I’ll explain what each part is below):.
Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle