Build Awesome Command-Line Applications in Ruby: Control Your Computer, Simplify Your Life

Build Awesome Command-Line Applications in Ruby: Control Your Computer, Simplify Your Life

by David B. Copeland


Choose Expedited Shipping at checkout for delivery by Thursday, May 20


Speak directly to your system. With its simple commands, flags, and parameters, a well-formed command-line application is the quickest way to automate a backup, a build, or a deployment and simplify your life.

As Ruby pro David Copeland explains, writing a command-line application that is self-documenting, robust, adaptable and forever useful is easier than you might think. Ruby is particularly suited to this task, since it combines high-level abstractions with "close to the metal" system interaction wrapped up in a concise, readable syntax. Moreover, Ruby has the support of a rich ecosystem of open-source tools and libraries.

Ten insightful chapters each explain and demonstrate a command-line best practice. You'll see how to use these tools to elevate the lowliest automation script to a maintainable, polished application.

You'll learn how to use free, open source parsers to create user-friendly command-line interfaces as well as command suites. You'll see how to use defaults to keep options simple for everyday users, while giving advanced users options for more complex tasks.

There's no reason a command-line application should lack documentation, whether it's part of a help command or a man page; you'll find out when and how to use both. Your journey from command-line novice to pro ends with a look at valuable approaches to testing your apps, and includes some fun techniques for outside-the-box, colorful interfaces that will delight your users.

With Ruby, the command line is not dead. Long live the command line.

What You Need:

All you'll need is Ruby, and the ability to install a few gems along the way. Examples written for Ruby 1.9.2, but 1.8.7 should work just as well.

Product Details

ISBN-13: 9781934356913
Publisher: The Pragmatic Programmers, LLC
Publication date: 03/19/2012
Pages: 225
Product dimensions: 7.50(w) x 8.90(h) x 0.80(d)

About the Author

David Bryant Copeland is a veteran professional software developer who spends most of his time on the command line. He speaks frequently at national and regional Ruby conferences and built many command-line and web applications, using the command-line to productive effect.

Table of Contents

Introduction vii

1 Have a Clear and Concise Purpose 1

1.1 Problem 1: Backing Up Data 2

1.2 Problem 2: Managing Tasks 5

1.3 What Makes an Awesome Command-Line App 10

1.4 Moving On 11

2 Be Easy to Use 13

2.1 Understanding the Command Line: Options, Arguments, and Commands 13

2.2 Building an Easy-to-Use Command-Line Interface 18

2.3 Building an Easy-to-Use Command-Suite Interface 23

2.4 Moving On 31

3 Be Helpful 33

3.1 Documenting a Command-Line Interface 33

3.2 Documenting a Command Suite 38

3.3 Including a Man Page 42

3.4 Writing Good Help Text and Documentation 47

3.5 Moving On 50

4 Play Well with Others 53

4.1 Using Exit Codes to Report Success or Failure 54

4.2 Using the Standard Output and Error Streams Appropriately 59

4.3 Formatting Output for Use as Input to Another Program 63

4.4 Trapping Signals Sent from Other Apps 68

4.5 Moving On 69

5 Delight Casual Users 71

5.1 Choosing Names for Options and Commands 72

5.2 Choosing Default Values for Flags and Arguments 76

5.3 Deciding Default Behavior 86

5.4 Moving On 86

6 Make Configuration Easy 89

6.1 Why External Configuration? 89

6.2 Reading External Configuration from Files 90

6.3 Using Configuration Files with Command Suites 94

6.4 Design Considerations When Using Configuration 98

6.5 Moving On 99

7 Distribute Painlessly 101

7.1 Distributing with RubyGems 101

7.2 Distributing Without RubyGems 108

7.3 Collaborating with Other Developers 109

7.4 Moving On 115

8 Test, Test, Test 117

8.1 Testing User Behavior with Acceptance Tests 118

8.2 Testing in Isolation with Unit Tests 131

8.3 A Word About Test-Driven Development 139

8.4 Moving On 139

9 Be Easy to Maintain 141

9.1 Dividing Code into Multiple Files 141

9.2 Designing Code for Maintainability 146

9.3 Moving On 151

10 Add Color, Formatting, and Interactivity 153

10.1 Adding Color Using ANSI Escape Sequences 154

10.2 Formatting Output with Tables 159

10.3 Providing Interactive User Input with readline 164

10.4 Moving On 173

Al Common Command-Line Gems and Libraries 175

A1.1 Alternatives for Simple Command-Line Apps 176

A1.2 Alternatives for Command Suites 184

A1.3 Other Relevant Libraries 189

A2 Bibliography 193

Index 195

Customer Reviews