Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:04:31+00:00 2026-06-12T22:04:31+00:00

I’m using Ruby’s optparse library to parse the options for my command line app

  • 0

I’m using Ruby’s optparse library to parse the options for my command line app but I can’t figure out how to also accept a command.

It would be something like this:

commit -f -d init

init would be the command in this case. It isn’t always required, as there is a default command that should be run if the user hasn’t provided any.

Here’s the code I have as of now:

OptionParser.new do |opts|
  opts.banner  = %Q!Usage:
  pivotal_commit                                          # to commit with a currently started issue
  pivotal_commit -f                                       # to commit with a currently started issue and finish it
  pivotal_commit -d                                       # to commit with a currently started issue and deliver it
  pivotal_commit init -e "me@gmail.com" -p my_password -l #to generate a config file at the current directory!

  opts.on("-e", "--email [EMAIL]", String, "The email to the PT account you want to access") do |v|
    options[:email] = v
  end

  opts.on("-p", "--password [PASSWORD]", String, "The password to the PT account you want to access") do |v|
    options[:password] = v
  end

  opts.on("-f", '--finish', 'Finish the story you were currently working on after commit') do |v|
    options[:finish] = v
  end

  opts.on('-d', '--deliver', 'Deliver the story you were currently working on after commit') do |v|
    options[:deliver] = v
  end

  opts.on_tail('-h', '--help', 'Show this message') do
    puts opts
    exit
  end

  opts.on_tail('-v', '--version', 'Show version') do
    puts "pivotal_committer version: #{PivotalCommitter::VERSION}"
    exit
  end

end.parse!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:04:32+00:00Added an answer on June 12, 2026 at 10:04 pm

    Command-line arguments(not options) are in ARGV after calling OptionParser#parse! because #parse! extracts options from ARGV.
    So, you can get subcommand like this:

    options = {}
    
    OptionParser.new do |opts|
    # definitions of command-line options...
    # ...
    end.parse!
    
    subcommand = ARGV.shift || "init"
    
    print "options: "
    p options
    puts "subcommand: #{subcommand}"
    

    If you have many subcommands, Thor gem might help you.

    And, although this is not answer for your question, brackets([]) in option definition mean that the option’s argument is optional.
    For instance, at your definitions, email and password might be nil even when the options are passed:

    $ pivotal_commit -e
    options: {:email=>nil}
    subcommand: init
    

    If you require argument when the option is passed, remove brackets:

    # ...
      opts.on("-e", "--email EMAIL", String, "The email to the PT account you want to access") do |v|
        options[:email] = v
      end
    # ...
    

    Now argument for email is required:

    $ pivotal_commit -e
    pivotal_commit:6:in `<main>': missing argument: -e (OptionParser::MissingArgument)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am using Paperclip to handle profile photo uploads in my app. They upload
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.