Python / ruby command line script?

Discussion in 'Ruby' started by Paatsch, Bernd, Jan 24, 2006.

  1. ------_=_NextPart_001_01C62108.C3EEA992
    Content-Type: text/plain

    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:])



















    ------_=_NextPart_001_01C62108.C3EEA992--
    Paatsch, Bernd, Jan 24, 2006
    #1
    1. Advertising

  2. 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?




    > """
    > 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


    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
    Schüle Daniel, Jan 24, 2006
    #2
    1. Advertising

  3. Paatsch, Bernd

    Guest

    See this tutorial regarding parsing command line args in Ruby:


    http://www.linuxdevcenter.com/pub/a/linux/2003/09/18/ruby_csv.html?page=1

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


    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?
    >
    >
    > """
    > 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:])
    , Jan 25, 2006
    #3
  4. Paatsch, Bernd

    Jim Freeze Guest

    --Apple-Mail-10-949705876
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain;
    charset=US-ASCII;
    delsp=yes;
    format=flowed

    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


    > """
    >
    > 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:])



    Jim Freeze




    --Apple-Mail-10-949705876--
    Jim Freeze, Jan 25, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    1,706
    Terry Hancock
    Apr 23, 2005
  2. Replies:
    2
    Views:
    186
    Phrogz
    Jun 26, 2006
  3. Swati Sharma
    Replies:
    5
    Views:
    480
    Charles Oliver Nutter
    Apr 7, 2008
  4. Jayson Williams
    Replies:
    28
    Views:
    341
    Mohit Sindhwani
    Nov 30, 2008
  5. Zhan Deaux
    Replies:
    7
    Views:
    165
    Zhan Deaux
    Dec 9, 2010
Loading...

Share This Page