# Command line parameters testing

**URL:** <https://www.ruby-forum.com/t/command-line-parameters-testing/74298>\
**Category:** Ruby\
**Created:** [November 4, 2006, 3:09am UTC](https://www.ruby-forum.com/t/command-line-parameters-testing/74298 "2006-11-04T03:09:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![apparna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@apparna](https://www.ruby-forum.com/u/apparna)\
**Post date:** [November 4, 2006, 3:09am UTC](https://www.ruby-forum.com/t/command-line-parameters-testing/74298/1 "2006-11-04T03:09:06Z")

</div>

Hello,  
I’m using Runit to write unit test for a class that takes command line  
parameters.

How do I pass commandline parameters from Runit class to my actual class  
that it’s testing?

I’m a new bee to ruby and any help will be appreciated.

Thanks,  
App

---

<div class="post-metadata">

**Author:** ![apparna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@apparna](https://www.ruby-forum.com/u/apparna)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/command-line-parameters-testing/74298/2 "2006-11-07T11:00:53Z")

</div>

App Ra wrote:

> Hello,  
> I’m using Runit to write unit test for a class that takes command line  
> parameters.
> 
> How do I pass commandline parameters from Runit class to my actual class  
> that it’s testing?

By typing the Ruby script name and then the arguments. Perhaps a bit  
more  
detail on your problem would help us help you. Do you want to know how  
to  
create the command-line arguments, or how to read them?

* * *

#!/usr/bin/ruby -w

n = 0

ARGV.each do |arg|  
puts “Argument #{n}: #{arg}”  
n += 1  
end

* * *

$ (ruby script name) This is a test.

Output:

Argument 0: This  
Argument 1: is  
Argument 2: a  
Argument 3: test.

---

<div class="post-metadata">

**Author:** ![apparna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@apparna](https://www.ruby-forum.com/u/apparna)\
**Post date:** [November 7, 2006, 11:01am UTC](https://www.ruby-forum.com/t/command-line-parameters-testing/74298/3 "2006-11-07T11:01:03Z")

</div>

On 11/4/06, App Ra [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> App  
> Passing arguments to your class instead of runit: (actually I don’t  
> know much about Runit, but this is common style, and test/unit does it  
> as well, so): The trick is to deparate parameters to your class with  
> – i.e. when I’m using test/unit I might do:

tc\_whatever.rb -n /test\_name/ – -a -b --c --defgh ijkl

Then -n /test\_name/ will be processed by test/unit and -a -b --c  
–defgh ijkl will be passed to the test.

Now to testing parameters: It’s better to put the command line  
processing stuff into a method that you’ll call with ARGV rather than  
using ARGV inside the method.  
That way you’ll be able to test the parameter parsing without actually  
messing with ARGV:

class Main  
def parse\_params(argv)  
…  
end  
def run  
@params = parse\_params(ARGV)  
end  
end

class TestMain \< Test::Unit::TestCase  
def setup  
@main = Main.new  
end  
def test\_parse\_params  
assert\_something xxx, @main.parse\_params(%w[-c -a -b -addsa -das  
–d-dsada])  
assert\_something yyyy, @main.parse\_params(%w[-c -a -b --d-dsada])  
assert\_something\_other @main.parse\_params(%w[-c -addsa -das  
–d-dsada])  
end  
end
