• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Crafting Beautiful CLI Applications in Ruby
 

Crafting Beautiful CLI Applications in Ruby

on

  • 7,312 views

Shishir Das (@shishirdas) and Nikhil Mungel (@hyfather) presented this at RubyConf India 2012.

Shishir Das (@shishirdas) and Nikhil Mungel (@hyfather) presented this at RubyConf India 2012.

Statistics

Views

Total Views
7,312
Views on SlideShare
7,195
Embed Views
117

Actions

Likes
13
Downloads
0
Comments
0

5 Embeds 117

http://rubylive.fr 92
http://news.humancoders.com 10
http://www.linkedin.com 8
https://twitter.com 5
http://www.hanrss.com 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • task oriented v/s feature oriented\n(ssh is feature / rails is tasks)\n\nOther Examples -- knife & cucumber\nTODO: SSH\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • bundle is a bad example\n\nrails/git/knife are good examples\n\n
  • \n
  • \n
  • all commands should be reversible, those that are not should be called out.\n\nSometimes irreversibility is unavoidable >> rm -rf --no-preserve-root /\n\n
  • make an example for rails generate /destroy\n
  • make an example for rails generate /destroy\n
  • make an example for rails generate /destroy\n
  • make an example for rails generate /destroy\n
  • Assume defaults wherever you can.\nIn case of risky operations don’t assume anything.\n\nE. g. -- package managers assume latest version\n
  • \n
  • \n
  • \n
  • git add\n> did you mean `git add .’ ?\n
  • \n
  • \n
  • vi modes\nbehavior should not depend on hidden state\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • git is a very mature cli. no piping or redirection normally used. but it honors everything.\nwrite errors to stderr\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • knife, rails, gem, bundle\n
  • \n
  • Input - command line -> Option Parsing\n\nOutput - status code, stdout, stderr\n
  • Input - command line -> Option Parsing\n\nOutput - status code, stdout, stderr\n
  • Input - command line -> Option Parsing\n\nOutput - status code, stdout, stderr\n
  • Input - command line -> Option Parsing\n\nOutput - status code, stdout, stderr\n
  • Input - command line -> Option Parsing\n\nOutput - status code, stdout, stderr\n
  • Input - command line -> Option Parsing\n\nOutput - status code, stdout, stderr\n
  • Input - command line -> Option Parsing\n\nOutput - status code, stdout, stderr\n
  • Input - command line -> Option Parsing\n\nOutput - status code, stdout, stderr\n
  • Input - command line -> Option Parsing\n\nOutput - status code, stdout, stderr\n
  • Input - command line -> Option Parsing\n\nOutput - status code, stdout, stderr\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Enforces OO\nAutomatic rake style tasks\n
  • Enforces OO\nAutomatic rake style tasks\n
  • Enforces OO\nAutomatic rake style tasks\n
  • Convention -- \nfirst arg to the description becomes the input \nsecond arg is the banner\n
  • Convention -- \nfirst arg to the description becomes the input \nsecond arg is the banner\n
  • Convention -- \nfirst arg to the description becomes the input \nsecond arg is the banner\n
  • Option Parsing\nInbuilt support for help and banners\nSupports standard invocations of help on the shell\n
  • Option Parsing\nInbuilt support for help and banners\nSupports standard invocations of help on the shell\n
  • \n
  • \n
  • \n
  • \n
  • Input - mostly handled by the third party library.\nTesting that would be testing the gem. Not a good idea.\n\n
  • Input - mostly handled by the third party library.\nTesting that would be testing the gem. Not a good idea.\n\n
  • Standard Ruby classes. Libraries like test/unit, rspec.\nMocking and proxy layers for 3rd party services etc.\nLike any other app.\n\n
  • \n
  • Mocking\n  mocha works out well. \n  For filesystem, MockFS lets you mock the entire file system. \n\nTesting CLI apps that manipulate filesystem. Mocking is good. But if we mock every call to FileUtils, test becomes very tightly coupled. So even if behaviour doesn't change but the command changes the test breaks.\n - one use FakeFS\n\n
  • Isolate environment of its own.\nIf its is cheap and scriptable to spin up the environment. Then we can have behaviour testing \n  - powerful machines.\n  - strong virtualizations (inbuilt)\nvagrant, lxc, openvz\n
  • 2 distinct sections -- your and subprocesses\n\nbackticks and system ruby calls not versatile. Doesn’t give you full control over the Input/Output/Error stream\nMixLib::Shellout and POpen3 are better alternatives.\nRespect exit status 0 -for success, rest all failures while writing your CLI.\nYour CLI should write error to stderr and not stdout. ruby provides $stdout, $stdin, $stderr\n\n
  • \n
  • \n
  • \n
  • Compatible with windows.\nUses the select(2) system call. \nGives abstractions over umask, cwd etc.\n
  • Compatible with windows.\nUses the select(2) system call. \nGives abstractions over umask, cwd etc.\n
  • Drop in plugins eg. vagrant, knife\nPick up all .rb files from a predetermined location.\n\nYour CLI app can then be easily extended\nWhen writing an app, you don’t know all the possible use cases\n\n
  • Provide hooks where arbitrary code can be run on failure and success of steps.\nEnsure that you support all executable files and not only .rb files.\n\nCan be filename based (git) or configurable.\n
  • STDOUT could be the default. But should be configurable to a file. \n\nLog at correct level. Apply to all apps but worth mentioning.\n\nyou may want to support -v and -vv for falling back to :info or :debug and -q falls back fatal.\n
  • Give URL in sliduments\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Crafting Beautiful CLI Applications in Ruby Crafting Beautiful CLI Applications in Ruby Presentation Transcript