Build Awesome Command - Line Applications in Ruby, David Bryant Copeland

E-books Shop
Build Awesome Command - Line Applications in Ruby

Control Your Computer, Simplify Your Life

Introduction
Graphical user interfaces (GUIs) are great for a lot of things; they are typically
much kinder to newcomers than the stark glow of a cold, blinking cursor.
This comes at a price: you can get only so proficient at a GUI before you have
to learn its esoteric keyboard shortcuts. Even then, you will hit the limits of
productivity and efficiency. GUIs are notoriously hard to script and automate,
and when you can, your script tends not to be very portable.
This is all beside the point; we are software developers, and we write programs.
What could be more natural than using code to get our work done? Consider
the following command sequence:
> cd ~/Projects/cli
> vi chapter2.md
While these two commands might strike you as opaque, they are a highly
efficient means of editing a file.
For most of my career, the command line meant a UNIX shell, like bash. The
bash shell provides some basic built-in commands, as well as access to many
other standard (and nonstandard) commands that are shipped with any UNIX
system. These commands are single-purpose, require no user interaction,
and come with easy-to-use (but hard-to-learn) user interfaces. These attributes
let you piece them together in a near-infinite number of ways. Automating
sophisticated behavior, performing complicated analysis, and parsing a myriad
of text files can be done easily and expediently. This was life for me early on
in my career. And it was good.
Then, in the mid-1990s, as Java grew in popularity, the idea of stringing
together UNIX command-line utilities to get things done came to be seen as
archaic. Java programs eschewed simple text-based configuration and filebased
input/output (I/O) for complex hierarchies of XML driven by RPC and
HTTP I/O. This allowed for very sophisticated systems to be built, and GUI
tools sprang up to abstract away the complexity of building and configuring
these systems. Even the act of writing and building code got swallowed up

Who This Book Is For
This book is aimed at both developers and system administrators who have
some familiarity with Ruby and who find themselves automating things on
the command line (or who wish they could).
• If you’re a developer who finds yourself faced with automation tasks but
aren’t familiar with the various conventions and techniques around the
command line, this book will help you. A problem you might have is the
maintenance of a “quick hack” script you wrote that has lived long past
its prime. This book will give you the tools and techniques to make your
next script longer-lived, polished, and bulletproof…all without spending
a lot of time on it.
• If you’re a sysadmin, you might find shell scripting limiting or frustrating.
If you’re pushing bash to the limit in your automation tasks, this book will
open up a whole new world for you. Writing command-line apps in Ruby
is also a great way to really learn Ruby and become a better programmer,
since you can apply it directly to your day-to-day tasks.

Contents

Introduction
1. Have a Clear and Concise Purpose
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
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
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
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
5.1 Choosing Names for Options and Commands 72
5.2 Choosing Default Values for Flags and Arguments 76
5.3 Deciding Default Behavior 82
5.4 Moving On 86
6. Make Configuration Easy
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
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
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 
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
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
A1. Common Command-Line Gems and Libraries
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


How This Book Is Organized
 In the next ten chapters, we’ll discuss every detail of command-line application
development, from user input, program output, and code organization to error
handling, testing, and distribution. We’ll learn about this by building and
enhancing two example applications. Over the course of the book, we’ll make
them better and better to learn what an awesome command-line app is. We’ll
see that Ruby makes it very easy to do, thanks to its great syntax and features,
as well as several open source libraries.
The first thing we’ll learn—in Chapter 1, Have a Clear and Concise Purpose,
on page 1—is what sort of applications are right for the command line. We’ll
then learn—in Chapter 2, Be Easy to Use, on page 13—the nuts and bolts of
making an awesome application that’s easy for both users and the system to interact with.
That chapter is all about the user interface of command-line
apps and introduces the two main styles of app: a simple UNIX-like style and
the more complex “command-suite” style, as exemplified by commands like git or gem.
In Chapter 3, Be Helpful, on page 33, we’ll learn how to provide excellent help
and usage documentation; command-line apps are harder to discover and
learn compared to GUIs, so this is one of the most important things to get
right. We’ll follow that up with Chapter 4, Play Well with Others, on page 53,
where we’ll learn how to make our apps interoperable with any other system.
At this point, we’ll know how to make a good command-line app. Chapter 5,
Delight Casual Users, on page 71 is where we take things to the next level
and learn how easy it is to add polish to our apps. We’ll continue this trend
in Chapter 6, Make Configuration Easy, on page 89, where we’ll learn how to
make our apps easy to use for users with many different tastes and preferences.
Chapter 7, Distribute Painlessly, on page 101 will cover everything you need
to distribute your application with RubyGems so that others can use it (we’ll
also cover installation in tightly controlled environments where RubyGems isn’t an option).
In Chapter 8, Test, Test, Test, on page 117, we’ll learn all about testing command-
line apps, including some techniques to keep your tests from making
a mess of your system. With the ability to test our apps comes the ability to
refactor them so they are easier to maintain and enhance. Chapter 9, Be Easy
to Maintain, on page 141 will cover some conventions around code organization,
as well as some design patterns that are most useful to command-line apps.
We’ll finish by pushing the envelope of what command-line apps should do
in Chapter 10, Add Color, Formatting, and Interactivity, on page 153. We’ll learn
all about colored, formatted output, as well as interacting with the user using Readline.
Many open source libraries and tools help make command-line apps in Ruby.
We’ll look at some of them, such as OptionParser, GLI, and Cucumber, in great
detail. But you don’t have to limit yourself to just these tools. Appendix 1,
Common Command-Line Gems and Libraries, on page 175 will go over many of
the other popular libraries so you can use the best tool for you.

 Screenshot 

E-books Shop

Purchase Now !
Just with Paypal



Product details
 Price
 File Size
 5,582 KB
 Pages
 214 p
 File Type
 PDF format
 ISBN-13
 978-1-934356-91-3
 Copyright
 2012 Pragmatic Programmers, LLC  
●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●


═════ ═════

Previous Post Next Post