By David B. Copeland
Speak on to your process. With its basic instructions, flags, and parameters, a well-formed command-line program is the fastest technique to automate a backup, a construct, or a deployment and simplify your life.
As Ruby seasoned David Copeland explains, writing a command-line program that's self-documenting, strong, adaptable and without end necessary is less complicated than it's possible you'll imagine. Ruby is very fitted to this activity, because it combines high-level abstractions with "close to the steel" procedure interplay wrapped up in a concise, readable syntax. in addition, Ruby has the help of a wealthy surroundings of open-source instruments and libraries.
Ten insightful chapters every one clarify and display a command-line most sensible perform. you will see how you can use those instruments to raise the lowliest automation script to a maintainable, polished program.
You'll tips on how to use loose, open resource parsers to create uncomplicated command-line interfaces in addition to command suites. you will see the right way to use defaults to maintain techniques easy for daily clients, whereas giving complicated clients concepts for extra advanced projects.
There's no cause a command-line program should still lack documentation, even if it is a part of a aid command or a guy web page; you will discover out while and the way to exploit either. Your trip from command-line beginner to professional ends with a glance at helpful techniques to checking out your apps, and comprises a few enjoyable thoughts for outside-the-box, colourful interfaces that may pride your users.
With Ruby, the command line isn't lifeless. lengthy reside the command line.
What You Need:
All you will need is Ruby, and the facility to put in a couple of gemstones alongside the best way. Examples written for Ruby 1.9.2, yet 1.8.7 should still paintings simply as well.
Quick preview of Build Awesome Command-Line Applications in Ruby: Control Your Computer, Simplify Your Life PDF
Dirname(__FILE__) + '/.. /lib') require 'rubygems' require 'gli' require 'todo_version' ➤ require 'todo/todo. rb' ➤ comprise Todo the 1st highlighted line isn’t new (GLI integrated it for us after we first generated our project), yet it’s worthy stating simply because this is often how bin/todo should be capable of entry our newly extracted code that lives in lib/todo/task. rb. All we’re doing is putting our lib listing into the weight direction (accessible in Ruby through $LOAD_PATH). the following highlighted line calls for our code, whereas the next contains into the present context.
This colour simply capability “do now not practice certain colours. ” we will use this because the argument to paint, except the duty is file erratum • speak about 158 • bankruptcy 10. upload colour, Formatting, and Interactivity determine 6—Using brilliant ends up in activity names that stand out. accomplished, after which we’ll use :green. we need to swap each line, so learn this code once or twice to ensure you see the variations: break_rules/todo/lib/todo/format/pretty. rb def format(index,task) colour = :default if activity. accomplished? colour = :green finish places sprintf("%2d - %s",index,task.
Even if, it’s great to understand third-party gem can do such a lot of what we need on 1. eight. a. https:// github. com/ahoward/open4 clients of our app can now use our common errors flow to get any blunders messages we would generate. commonly, the normal mistakes of apps we name can be despatched to our general mistakes flow. We now understand how to learn output from and write output to definitely the right errors circulation, and we’ve began to get a feeling of what messages move the place. mistakes messages visit the traditional errors circulate, and “everything else” is going to the normal output flow.
The uncooked worth from the command line should be at index zero, and the extracted values will fill out the remainder of the array. You don’t need to use average expressions for validation, even if. by means of together with an Array within the argument checklist to on, you could point out the whole record of appropriate values. by utilizing a Hash, OptionParser will use the keys because the applicable values and ship the mapped worth to the block, like so: obtain from Wow! e-book
Org/wiki/Nroff obtain from Wow! booklet