0

This is homework and I do not expect you to give me the complete answer.

I'm trying to parse a command line entry such as:

 ./apacheReport.rb -u testlog.txt 

When I enter this:

./apacheReport.rb -u testlog.txt 

I get:

Argument required

My code is:

require_relative 'CommonLog'
require 'optparse'

# puts ARGV.inspect

optparser = OptionParser.new

optU = false
optI = false
optS = false
optH = false

optparser.banner = "apacheReport.rb [options] filename"

        optparser.parse!
rescue => m
        puts m.message
        puts optparser
        exit
end

if ARGV.length < 1
        puts  "Argument required"
        exit
end

userInputFile = ARGV[0]
userInputFile.to_s
file = CommonLog.new(userInputFile)

It should parse the leftover portion of the command into ARGV[0] then should store it as userInputFile and then create a CommonLog object using the file as the constructor. At that point I call the methods that were specified in the command.

It seems that for some reason my ARGV is not being returned. I'm not sure what the issue is.

8
  • You mention using optparse, but it doesn't appear in your code. Could you post that as well?
    – August
    Nov 10, 2014 at 22:23
  • I apologize as I'm trying to keep as much code out as I can. I added in the optparsing. Keep in mind that some code has still been left out. (Mainly the options) I have an email in to my teacher, but it has been a few hours so I figured I'd turn to SO to see if they had any answers. Don't want to be late!
    – Destiny
    Nov 10, 2014 at 22:25
  • Does the -u option take an additional argument? E.g. opts.on("-u ARGUMENT") ...?
    – August
    Nov 10, 2014 at 22:37
  • None of the options take additional arguments.
    – Destiny
    Nov 10, 2014 at 22:38
  • While it's nice that you're stripping out stuff, it's still important that your example code be valid, so you can't strip out require statements or definitions that are necessary. Nov 10, 2014 at 22:43

2 Answers 2

0

Ruby's OptionParser is easy to use, but you have to puzzle through the documentation. Here's a little example that'd be useful for your code:

require 'optparse'

options = {}
OptionParser.new do |opt|
  opt.on('-u', '--use_this FILE', 'Use this file') { |o| options[:use_this] = o }
end.parse!

options will contain the flags. In this case, if you pass in -u foo, options[:use_this] will be foo.

Save that and try running it without and with a parameter. Also try running it with just a -h flag.

You can search StackOverflow for more answers where I was dealing with OptionParser.

0

It's hard to tell what's wrong since you code doesn't seem to be working at the moment. The problem may be that the parse! method removes found options from ARGV. So when you write:

optparser.parse!

It removes your two parameters (-u testlog.txt) and this code always fails:

if ARGV.length < 1
        puts  "Argument required"
        exit
end

Instead of looking at ARGV, you need to set up optparser correctly. Perhaps something like:

optparser = OptionParser.new do |opts|
  opts.banner = "apacheReport.rb [options] filename"
  opts.on("-u", "--u-short-for-this", "Whatever u stands for") do |u|
    optU = u
  end
end

Then optU will be true only if the user passed -u and the filename will be in ARGV[0].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.