Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Introducing Command Line Applications with Ruby
1. Ruby on Rails In Practice
Apartment List and Thoughtbot
San Francisco, September 2012
Getting started with
Command Line Applications
2. Ruby on Rails In Practice
Apartment List and Thoughtbot
San Francisco, September 2012
Getting started with
Command Line Applications
Nikhil Mungel
@hyfather
35. class Test < Thor
desc " FILE", "an example task"
method_option :delete,
:aliases => "-d",
:desc => "Delete the file"
def example(file)
end
end
36. class Test < Thor
desc " FILE", "an example task"
method_option :delete,
:aliases => "-d",
:desc => "Delete the file"
def example(file)
end
end
~ > myapp help test:example
Usage:
thor test:example FILE
Options:
-d, [--delete=DELETE] # Delete the file after parsing it
an example task
37. class Test < Thor
desc " FILE", "an example task"
method_option :delete,
:aliases => "-d",
:desc => "Delete the file"
def example(file)
end
end
~ > myapp help test:example
Usage:
thor test:example FILE
Options:
-d, [--delete=DELETE] # Delete the file after parsing it
an example task
38. class Test < Thor
desc " FILE", "an example task"
method_option :delete,
:aliases => "-d",
:desc => "Delete the file"
def example(file)
end
end
~ > myapp help test:example
Usage:
thor test:example FILE
Options:
-d, [--delete=DELETE] # Delete the file after parsing it
an example task
39. class Test < Thor
desc " FILE", "an example task"
method_option :delete,
:aliases => "-d",
:desc => "Delete the file"
def example(file)
end
end
~ > myapp help test:example
Usage:
thor test:example FILE
Options:
-d, [--delete=DELETE] # Delete the file after parsing it
an example task
40. class Test < Thor
desc " FILE", "an example task"
method_option :delete,
:aliases => "-d",
:desc => "Delete the file"
def example(file)
end
end
~ > myapp help test:example
Usage:
thor test:example FILE
Options:
-d, [--delete=DELETE] # Delete the file after parsing it
an example task
41. class Test < Thor
desc " FILE", "an example task"
method_option :delete,
:aliases => "-d",
:desc => "Delete the file"
def example(file)
end
end
~ > myapp help test:example
Usage:
thor test:example FILE
Options:
-d, [--delete=DELETE] # Delete the file after parsing it
an example task
42. class Test < Thor
desc " FILE", "an example task"
method_option :delete,
:aliases => "-d",
:desc => "Delete the file"
def example(file)
end
end
~ > myapp help test:example
Usage:
thor test:example FILE
Options:
-d, [--delete=DELETE] # Delete the file after parsing it
an example task
63. Thanks!
I am currently a Grad Student at
San Jose State
Formerly at ThoughtWorks Studios
slidesha.re/rubycli
Nikhil Mungel
www.hyfather.com @hyfather
Notes de l'éditeur
\n
\n
\n
\n
\n
\n
\n
\n
\n
\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
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&#xA0; mocha works out well.&#xA0;\n&#xA0; For filesystem, MockFS lets you mock the entire file system.&#xA0;\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&#xA0;- one use FakeFS\n\n
Mocking\n&#xA0; mocha works out well.&#xA0;\n&#xA0; For filesystem, MockFS lets you mock the entire file system.&#xA0;\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&#xA0;- 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&#xA0;\n&#xA0; - powerful machines.\n&#xA0; - strong virtualizations (inbuilt)\nvagrant, lxc, openvz\n
Isolate environment of its own.\nIf its is cheap and scriptable to spin up the environment. Then we can have behaviour testing&#xA0;\n&#xA0; - powerful machines.\n&#xA0; - strong virtualizations (inbuilt)\nvagrant, lxc, openvz\n
Isolate environment of its own.\nIf its is cheap and scriptable to spin up the environment. Then we can have behaviour testing&#xA0;\n&#xA0; - powerful machines.\n&#xA0; - strong virtualizations (inbuilt)\nvagrant, lxc, openvz\n
Isolate environment of its own.\nIf its is cheap and scriptable to spin up the environment. Then we can have behaviour testing&#xA0;\n&#xA0; - powerful machines.\n&#xA0; - strong virtualizations (inbuilt)\nvagrant, lxc, openvz\n
Isolate environment of its own.\nIf its is cheap and scriptable to spin up the environment. Then we can have behaviour testing&#xA0;\n&#xA0; - powerful machines.\n&#xA0; - strong virtualizations (inbuilt)\nvagrant, lxc, openvz\n
2 distinct sections -- your and subprocesses\n\nbackticks and system ruby calls not versatile. Doesn&#x2019;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
backticks and system ruby calls not versatile. Doesn&#x2019;t give you full control over the Input/Output/Error stream\nMixLib::Shellout and POpen3 are better alternatives.\n\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
backticks and system ruby calls not versatile. Doesn&#x2019;t give you full control over the Input/Output/Error stream\nMixLib::Shellout and POpen3 are better alternatives.\n\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
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
\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