# Python / ruby command line script?

**URL:** <https://www.ruby-forum.com/t/python-ruby-command-line-script/53074>\
**Category:** Ruby\
**Created:** [January 24, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/python-ruby-command-line-script/53074 "2006-01-24T18:42:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaatschS\_Bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@PaatschS\_Bernd](https://www.ruby-forum.com/u/PaatschS_Bernd)\
**Post date:** [January 24, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/python-ruby-command-line-script/53074/1 "2006-01-24T18:42:00Z")

</div>

Hello,

I am a newbie to Ruby. However I have used python a little bit.  
I like to convert my python scripts to ruby. One of them is a command  
line  
script taking input and switches.  
Is there something equivalent in ruby?

“”"  
Generate something  
Usage: python xyz.py [options] [source] [destination]  
Options:  
-h, --help show help  
Examples: xyz.py input.csv output.html  
“”"

def main(argv):  
“”“Main Entry Point “””  
try:  
opts, args = getopt.getopt(argv, “h”, [“help”])  
except getopt.GetoptError:  
usage()  
sys.exit(2)

```
for opt, arg in opts: if opt in ("-h", "--help"):
        usage() sys.exit()

try:
    source = sys.argv[1]
    global destination
    destination = sys.argv[2]
except:
    usage()
    sys.exit(2)

seleniumHtmlGenerator(source)

```

if **name** == “ **main** ”:  
main(sys.argv[1:])

---

<div class="post-metadata">

**Author:** ![PaatschS\_Bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@PaatschS\_Bernd](https://www.ruby-forum.com/u/PaatschS_Bernd)\
**Post date:** [January 24, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/python-ruby-command-line-script/53074/2 "2006-01-24T18:50:15Z")

</div>

Paatsch, Bernd wrote:

> Hello,
> 
> I am a newbie to Ruby. However I have used python a little bit.  
> I like to convert my python scripts to ruby. One of them is a command  
> line  
> script taking input and switches.  
> Is there something equivalent in ruby?

[…]

OptionParser, perhaps?

[http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html)

---

<div class="post-metadata">

**Author:** ![PaatschS\_Bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@PaatschS\_Bernd](https://www.ruby-forum.com/u/PaatschS_Bernd)\
**Post date:** [January 24, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/python-ruby-command-line-script/53074/3 "2006-01-24T19:06:16Z")

</div>

Paatsch, Bernd wrote:

> Hello,
> 
> I am a newbie to Ruby. However I have used python a little bit.  
> I like to convert my python scripts to ruby. One of them is a command line  
> script taking input and switches.  
> Is there something equivalent in ruby?

> ```
> try:
> global destination 
> 
> ```

you script is incomplete, there is no “destination”  
at module namespace

> ```
> destination = sys.argv[2]
> except:
> usage()
> sys.exit(2)
>     
> seleniumHtmlGenerator(source)
> 
> ```

and no “seleniumHtmlGenerator” function  
“usage” Function is missed to  
on closer look “import getopt,sys” are missed too

> if **name** == “ **main** ”:  
> main(sys.argv[1:])

well, ARGV (alias to $\*) are you sys.argv[1:]  
sys.argv[0] is in $0

I don’t know whether Ruby has getopt class or file  
(what is it called properly?) which you can “require”

clearly seleniumHtmlGenerator does the main Job  
nobody can know its internals and other things can be  
translated straight forward

try:  
except:

begin  
rescue  
end

hth, Daniel

---

<div class="post-metadata">

**Author:** ![PaatschS\_Bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@PaatschS\_Bernd](https://www.ruby-forum.com/u/PaatschS_Bernd)\
**Post date:** [January 25, 2006, 7:34am UTC](https://www.ruby-forum.com/t/python-ruby-command-line-script/53074/4 "2006-01-25T07:34:24Z")

</div>

Hi  
On Jan 24, 2006, at 11:08 AM, Paatsch, Bernd wrote:

> Hello,
> 
> I am a newbie to Ruby. However I have used python a little bit.  
> I like to convert my python scripts to ruby. One of them is a  
> command line  
> script taking input and switches.  
> Is there something equivalent in ruby?

Yes, but Ruby also has something much better - CommandLine -  
it’s the rails equivalent for commandline apps. 🙂

> Generate something  
> Usage: python xyz.py [options] [source] [destination]  
> Options:  
> -h, --help show help  
> Examples: xyz.py input.csv output.html

#!/usr/bin/env ruby  
require ‘rubygems’  
require ‘commandline’

class App \< CommandLine::Application  
def initialize  
synopsis “[options] [source] [destination]”  
option :help  
expected\_args :source :destination  
end

def main  
# your code here  
end  
end

> ```
> for opt, arg in opts: if opt in ("-h", "--help"):
> seleniumHtmlGenerator(source)
> 
> ```
> 
> if **name** == “ **main** ”:  
> main(sys.argv[1:])

Jim F.

---

<div class="post-metadata">

**Author:** ![PaatschS\_Bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@PaatschS\_Bernd](https://www.ruby-forum.com/u/PaatschS_Bernd)\
**Post date:** [January 25, 2006, 2:25am UTC](https://www.ruby-forum.com/t/python-ruby-command-line-script/53074/5 "2006-01-25T02:25:19Z")

</div>

See this tutorial regarding parsing command line args in Ruby:

> **[Radar - O’Reilly](https://www.oreilly.com/radar/?page=1)**
>
> Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

The “getoptlong” library has apparently been superceded by “optparse”,  
so Google that too.
