Build Awesome Command-Line Applications in Ruby: Control Your Computer, Simplify Your Life
David B. Copeland
Format: PDF / Kindle (mobi) / ePub
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.
maintaining documentation. We now know the nuts and bolts of creating help and documentation, but it’s worth having a brief discussion on style. There remain a few aspects of help that are “fuzzy” but nevertheless important, and knowledge of a few more documentation conventions will help you write great documentation without being too verbose. 3.4 Writing Good Help Text and Documentation That your app has any help text at all is great and puts it, sadly, ahead of many apps in terms of ease of
provides access only to the exit status of the application. We can get access to the standard output by using the built-in backtick operator (`) or the %x construct, as in stdout = %x[ls -l]. Unfortunately, neither of these constructs provides access to the standard error stream. To get access to both the standard output and the standard error independently, we need to use a module from the standard library called Open3. Open3 has several useful methods, but the most straightforward is
list Naming can be difficult, but our guidelines around mnemonics, descriptive long-form options, and judicious use of short-form names can help. Now it’s time to go one level deeper into a command-line app and talk about the default values for flags and arguments. An example of what we mean is the --filename global option to our to-do list management app, todo. Why did we choose ~/.todo.txt as a default; should we have chosen a default, and is that the best default value we could’ve chosen?
Hellesøy, Bryan Helmkamp, and Dan North. The RSpec Book. The Pragmatic Bookshelf, Raleigh, NC and Dallas, TX, 2009. [GHJV95] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, Reading, MA, 1995. [Swi08] Travis Swicegood. Pragmatic Version Control Using Git. The Pragmatic Bookshelf, Raleigh, NC and Dallas, TX, 2008. [TFH09] David Thomas, Chad Fowler, and Andrew Hunt. Programming Ruby: The Pragmatic
Since the rest of this book is about what makes an awesome command-line app, it’s worth seeing a broad overview of what we’re talking about. In general, an awesome command-line app has the following characteristics: Easy to use The command-line can be an unforgiving place to be, so the easier an app is to use, the better. Helpful Being easy to use isn’t enough; the user will need clear direction on how to use an app and how to fix things they might’ve done wrong. Plays well with others The more