24
Building CLIs in Ruby By Daniel Marvin

Building CLIs with Ruby

  • Upload
    drizzlo

  • View
    149

  • Download
    5

Embed Size (px)

Citation preview

Building CLIs in RubyBy Daniel Marvin

Overview

Background and Ramp Up− The What

The Progression of a Script− The Why

The Quick and Dirty− Getting from A to Z quickly.

Getting Deeper− Handling IO

Background and Ramp Up

BASH (Bourne Again SHell) Shell was released in 1989 as the default shell of the GNU OS.

Bash continues to be one of the most widely used scripting languages for OS-related tools.

But many developers don't feel nearly as comfortable coding in BASH compared to other languages.

And with Ruby, Python, and Perl, there's really no need for bash scripts any longer.

But.. why?

You have scripts for Application installs Data management / manipulation Filesystem/OS maintenance Time-saving scripts for tasks like SSH tunnel

management or verifying server uptime. And more ...

Why NOT?

Easier for a Ruby-centric team to maintain, which means low cost.

Ruby can do everything BASH can do and more.

With OOP integration, code re-usability becomes more realistic.

Other benefits of Ruby, including the community, gems, etc.

The Progression of a Script

The Progression of a Script

I need to do something.

The Progression of a Script

I write a script to do that something and hard code the configurables.

The Progression of a Script

If someone else wants to use this,

then they run the script.

The Progression of a Script

If the input changes, then we modify the hardcoded configurables, until I realize I'm far

too lazy for this and I build an interface.

The Quick and Dirty

Main Pieces of a CLI

Parsing options (input) Routing commands to actions Displaying output

Command line tools can be fairly MVC conceptually:

Router => controller, IO => view, Logic => model

Parsing Options

The point should be to enable the user to achieve the end result, without requiring the user to know how to go about it, or how it was accomplished.

What configurables are important?− Defaults versus configurables.

The Rails mantra of “Convention Over Configuration” works for CLIs too.− What is common in terminal-based IO?

Routing Commands to Actions

Is this a wrapper or a controller? The interface should stay as clean as possible

and handle routing at the top level. Don't messy it up with a bunch of app-specific

code! Using metaprogramming methods such as

method_missing are very helpful here depending on the size of your application.

Displaying Output

Use printf for structured output. It's helpful to have one method used for

passing output to $stdout. Any output can be passed to the method through arguments or yielded through a block depending on the activity.

This also makes logging extremely simple.

Keep text short and valid. Ask yourself, is this important?

Cool Gems

Methadone Main GLI Thor

Going Deeper

Specific Ways to Handle Input

ARGV: An array of arguments passed with the script at run time:

myscript.rb arg1 arg2 #=> ['arg1', 'arg2']

Specific Ways to Handle Input

ARGV: An array of arguments passed with the script at run time:

myscript.rb arg1 arg2 #=> ['arg1', 'arg2']

ARGF: Stream that reads files from ARGV or STDIN.

This is how we do pipe magic!

Specific Ways to Handle Input

somefile.txt

abcd

somescript.rb

puts ARGF.read

ruby somescript.rb somefile.txt #=> abcd

Specific Ways to Handle Input

Handling a constant stream from STDIN.

ARGF.each_line do |line|

# code goes here...

end

Final Notes

CLIs are a great tool which can and will make your life much better.

Unfortunately this is a HUGE topic, but I hope that this talk has given you a nice introduction to a couple aspects of it and perhaps inspired you to explore further!

Thank You!