Home

Awesome

Samovar

Teapot

Samovar is a modern framework for building command-line tools and applications. It provides a declarative class-based DSL for building command-line parsers that include automatic documentation generation. It helps you keep your functionality clean and isolated where possible.

Development Status

Motivation

I've been using Optimist and while it's not bad, it's hard to use for sub-commands in a way that generates nice documentation. It also has pretty limited support for complex command lines (e.g. nested commands, splits, matching tokens, etc). Samovar is a high level bridge between the command line and your code: it generates decent documentation, maps nicely between the command line syntax and your functions, and supports sub-commands using classes which are easy to compose.

One of the other issues I had with existing frameworks is testability. Most frameworks expect to have some pretty heavy logic directly in the binary executable, or at least don't structure your code in a way which makes testing easy. Samovar structures your command processing logic into classes which can be easily tested in isolation, which means that you can mock up and spec your command-line executables easily.

Examples

Installation

Add this line to your application's Gemfile:

gem 'samovar'

And then execute:

$ bundle

Or install it yourself as:

$ gem install samovar

Usage

Generally speaking, you should create Command classes that represent specific functions in your program. The top level command might look something like this:

require 'samovar'

class List < Samovar::Command
	self.description = "List the current directory"
	
	def call
		system("ls -lah")
	end
end

class Application < Samovar::Command
	options do
		option '--help', "Do you need help?"
	end
	
	nested :command, {
		'list' => List
	}, default: 'list'
	
	def call
		if @options[:help]
			self.print_usage
		else
			@command.call
		end
	end
end

Application.call # Defaults to ARGV.

Basic Options

require 'samovar'

class Application < Samovar::Command
	options do
		option '-f/--frobulate <text>', "Frobulate the text"
		option '-x | -y', "Specify either x or y axis.", key: :axis
		option '-F/--yeah/--flag', "A boolean flag with several forms."
		option '--things <a,b,c>', "A list of things" do |value|
			value.split(/\s*,\s*/)
		end
	end
end

application = Application.new(['-f', 'Algebraic!'])
application.options[:frobulate] # 'Algebraic!'

application = Application.new(['-x', '-y'])
application.options[:axis] # :y

application = Application.new(['-F'])
application.options[:flag] # true

application = Application.new(['--things', 'x,y,z'])
application.options[:things] # ['x', 'y', 'z']

Nested Commands

require 'samovar'

class Create < Samovar::Command
	def invoke(parent)
		puts "Creating"
	end
end

class Application < Samovar::Command
	nested '<command>',
		'create' => Create
	
	def invoke(program_name: File.basename($0))
		if @command
			@command.invoke
		else
			print_usage(program_name)
		end
	end
end

Application.new(['create']).invoke

ARGV Splits

require 'samovar'

class Application < Samovar::Command
	many :packages
	split :argv
end

application = Application.new(['foo', 'bar', 'baz', '--', 'apples', 'oranges', 'feijoas'])
application.packages # ['foo', 'bar', 'baz']
application.argv # ['apples', 'oranges', 'feijoas']

Parsing Tokens

require 'samovar'

class Application < Samovar::Command
	self.description = "Mix together your favorite things."
	
	one :fruit, "Name one fruit"
	many :cakes, "Any cakes you like"
end

application = Application.new(['apple', 'chocolate cake', 'fruit cake'])
application.fruit # 'apple'
application.cakes # ['chocolate cake', 'fruit cake']

Explicit Commands

Given a custom Samovar::Command subclass, you can instantiate it with options:

application = Application['--root', path]

You can also duplicate an existing command instance with additions/changes:

concurrent_application = application['--threads', 12]

These forms can be useful when invoking one command from another, or in unit tests.

Contributing

We welcome contributions to this project.

  1. Fork it.
  2. Create your feature branch (git checkout -b my-new-feature).
  3. Commit your changes (git commit -am 'Add some feature').
  4. Push to the branch (git push origin my-new-feature).
  5. Create new Pull Request.

Developer Certificate of Origin

This project uses the Developer Certificate of Origin. All contributors to this project must agree to this document to have their contributions accepted.

Contributor Covenant

This project is governed by the Contributor Covenant. All contributors and participants agree to abide by its terms.

Future Work

Multi-value Options

Right now, options can take a single argument, e.g. --count <int>. Ideally, we support a specific sub-parser defined by the option, e.g. --count <int...> or --tag <section> <tags...>. These would map to specific parsers using Samovar::One and Samovar::Many internally.

Global Options

Options can only be parsed at the place they are explicitly mentioned, e.g. a command with sub-commands won't parse an option added to the end of the command:

command list --help

One might reasonably expect this to parse but it isn't so easy to generalize this:

command list -- --help

In this case, do we show help? Some effort is required to disambiguate this. Initially, it makes sense to keep things as simple as possible. But, it might make sense for some options to be declared in a global scope, which are extracted before parsing begins. I'm not sure if this is really a good idea. It might just be better to give good error output in this case (you specified an option but it was in the wrong place).

Shell Auto-completion

Because of the structure of the Samovar command parser, it should be possible to generate a list of all possible tokens at each point. Therefore, semantically correct tab completion should be possible.

As a secondary to this, it would be nice if Samovar::One and Samovar::Many could take a list of potential tokens so that auto-completion could give meaningful suggestions, and possibly improved validation.

Short/Long Help

It might be interesting to explore whether it's possible to have -h and --help do different things. This could include command specific help output, more detailed help output (similar to a man page), and other useful help related tasks.