Toolkit for developing sleek command line apps.
TTY is a toolbox for developing beautiful command line clients in Ruby with a fluid interface for gathering input, querying terminal properties and displaying information.
All too often libraries that interact with terminals create their own interface logic that gathers input from users and displays information back. Many times utility files are created that contain methods for reading system or terminal properties. Shouldn't we focus our energy on building the actual client?
Building terminal tools takes time. I believe that modular components put together in a single package with project scaffolding will help people build things faster and produce higher quality results. It is easy to jump start a new project with available scaffolding and mix and match components to create new tooling.
Add this line to your application's Gemfile to install all components:
gem 'tty'or install a particular component:
gem 'tty-*'And then execute:
$ bundleOr install it yourself as:
$ gem install ttyTTY provides you with commands and many components to get you onto the path of building awesome terminal applications in next to no time.
To simply jump start a new command line application use teletype executable:
$ teletype new appMove in to your new app, and then add more commands:
$ cd app
$ teletype add configThroughout the rest of this guide, I will assume a generated application called app, that you are in the working directory of 'app/', and a newly created bare command config.
new commandRunning teletype new [app-name] will bootstrap an entire project file structure based on the bundler gem command setup enhanced by additional files and folders related to command application development.
For example, to create a new command line application called app do:
$ teletype new appThe output will contain all the files that have been created during setup:
Creating gem 'app'
create app/Gemfile
create app/.gitignore
create app/lib/app.rb
create app/lib/app/version.rb
...In turn, the following files and directories will be generated in the app folder familiar to anyone who has created a gem beforehand:
▾ app/
├── ▾ exe/
│ └── app
├── ▾ lib/
│ ├── ▾ app/
│ │ ├── ▸ commands/
│ │ ├── ▸ templates/
│ │ ├── cli.rb
│ │ ├── command.rb
│ │ └── version.rb
│ └── app.rb
├── CODE_OF_CONDUCT.md
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
└── app.gemspecBy convention the file lib/app/cli.rb provides the main entry point to your command line application:
module App
class CLI < Thor
# Error raised by this runner
Error = Class.new(StandardError)
desc 'version', 'app version'
def version
require_relative 'version'
puts "v#{App::VERSION}"
end
map %w(--version -v) => :version
end
endThis is where all your application commands and subcommands will be defined.
Teletype uses Thor as an option parsing library by directly inheriting from it.
And also by convention the start method is used to parse the command line arguments inside the app executable:
App::CLI.startRun the new command with --help or -h flag to see all available options:
$ teletype new --help
$ teletype new -hExecute teletype to see all available commands.
--author, -a flagThe teletype generator can inject name into documentation for you:
$ teletype new app --author 'Piotr Murach'--ext flagTo specify that teletype should create a binary executable (as exe/GEM_NAME) in the generated project use the --ext flag. This binary will also be included in the GEM_NAME.gemspec manifest. This is disabled by default, to enable do:
$ teletype new app --ext--license, -l flagThe teletype generator comes prepackaged with most popular open source licenses:
agplv3, apache, bsd2, bsd3, gplv2, gplv3, lgplv3, mit, mplv2, custom. By default the mit license is used. To change that do:
$ teletype new app --license bsd3--test, -t flagThe teletype comes configured to work with rspec and minitest frameworks which are the only two acceptable values. The GEM_NAME.gemspec will be configured and appropriate testing directory setup. By default the RSpec framework is used.
$ teletype new app --test=minitest
$ teletype new app -t=minitestadd commandOnce application has been initialized, you can create additional command by using teletype add [command-name] task:
$ teletype add config
$ teletype add createThis will add create.rb and config.rb commands to the CLI client:
▾ app/
├── ▾ commands/
│ ├── config.rb
│ └── create.rb
├── ▸ templates/
│ ├── ▸ config/
│ └── ▸ create/
├── command.rb
├── cli.rb
└── version.rbThen you will be able to call the new commands like so:
$ app config
$ app createThe commands require you to specify the actual logic in their execute methods.
Please note that command names should be provided as camelCase or snake_case. For example:
$ teletype add addConfigCommand # => correct
$ teletype add add_config_command # => correct
$ teletype add add-config-command # => incorrect--args flagYou can specify that teletype should add a command with a variable number of arguments using the --args flag. The --args flag accepts space delimited variable names. To specify required argument use a string name, for an optional argument pass name = nil enclosed in quote marks and any variable number of arguments needs to be preceded by asterisk:
$ teletype add config --args name # required argument
$ teletype add config --args "name = nil" # optional argument
$ teletype add config --args *names # variadic argumentFor more in-depth usage see 2.4 Arguments.
--desc flagEvery generated command will have a default description 'Command description...', however whilst generating a command you can and should specify a custom description to provide more context with --desc flag:
$ teletype add config --desc 'Set and get configuration options'For more in-depth usage see 2.5 Description.
--force flagIf you wish to overwrite currently implemented command use --force flag:
$ teletype add config --forceRunning
teletype add configa new command config will be added to commands folder creating the following files structure inside the lib folder:
▾ app/
├── ▾ commands/
│ └── config.rb
├── ▾ templates/
│ └── ▸ config/
├── cli.rb
├── command.rb
└── version.rbThe lib/app/cli.rb file will contain generated command entry which handles the case where the user asks for the config command help or invokes the actual command:
module App
class CLI < Thor
desc 'config', 'Command description...'
def config(*)
if options[:help]
invoke :help, ['config']
else
require_relative 'commands/config'
App::Commands::Config.new(options).execute
end
end
end
endAnd the lib/app/commands/config.rb will allow you to specify all the command logic. In the Config class which by convention matches the command name, the execute method provides a place to implement the command logic:
module App
module Commands
class Config < App::Command
def initialize(options)
@options = options
end
def execute
# Command logic goes here ...
end
end
end
endNotice that Config inherits from App::Cmd class which you have full access to. This class is meant to provide all the convenience methods to lay foundation for any command development. It will lazy load many tty components inside helper methods which you have access to by opening up the lib/app/command.rb file.
For example in the lib/app/command.rb file, you have access to prompt helper for gathering user input:
# The interactive prompt
#
# @see http://www.rubydoc.info/gems/tty-prompt
#
# @api public
def prompt(**options)
require 'tty-prompt'
TTY::Prompt.new(options)
endor a command helper for running external commands:
# The external commands runner
#
# @see http://www.rubydoc.info/gems/tty-command
#
# @api public
def command(**options)
require 'tty-command'
TTY::Command.new(options)
endYou have full control of the file, so you can use only the tty components that you require. Please bear in mind that all the components are added by default in your app.gemspec which you can change to suite your needs and pick only tty components that fit your case.
A command may accept a variable number of arguments.
For example, if we wish to have a config command that accepts a location of configuration file, then we can run teletype add command passing --args flag:
$ teletype add config --args filewhich will include the required file as an argument to the config method:
module App
class CLI < Thor
desc 'config FILE', 'Set and get configuration options'
def config(file)
...
end
end
endSimilarly, if we want to generate command with two required arguments, we run teletype add command with --args flag that can accept variable names delimited by space character:
$ teletype add set --args name valuewill generate the following:
module App
class CLI < Thor
desc 'set NAME VALUE', 'Set configuration option'
def set(name, value)
...
end
end
endIf we want to have a command that has an optional argument, for example, the file argument is an optional argument in the config command, then you need to enclose --args argument in parentheses:
$ teletype add config --args 'file = nil'In well behaved command line a
No open issues yet, or sync has not completed.