Official Site – Docs – Examples
Terminal User Experience
Tux is a modular, dependency-free Elixir library designed for the speedy creation of elegant command line interfaces which subscribe to the philosophy "One module per command".
Its modular structure ensures its core functionalities can be overwritten and composed to fulfill custom needs, and having no dependencies means it can achieve a higher level of security by minimizing trusted parties.
Add tux
to your list of dependencies in mix.exs
and optionally update
your .formatter.exs
:
# mix.exs
{:tux, "~> 0.4.0"}
# .formatter.exs
import_deps: [:tux]
Here's a very short example to illustrate the mechanics of the library,
although keep in mind that tux has more features, among which: command preloads
, nested commands
, command alerts
, help messages
, command testing
and more.
- A command module implements a command:
defmodule Demo.HelloCmd do
use Tux.Command
@impl true
def about(), do: "Greet current user"
@impl true
def main(env, args), do: {:ok, "Hello #{env.pre.user}!"}
end
- A dispatcher module groups commands together:
defmodule Demo do
use Tux.Dispatcher
# Command registration
cmd "hello", Demo.HelloCmd, preloads: [:user]
cmd "adios", Demo.AdiosCmd
cmd "group", Demo.AnotherDispatcher
# Preloads can be executed before each command that registers them
def user(_), do: System.fetch_env!("USER")
end
- Test your escript with the supplied testing macros:
defmodule DemoTest do
use Tux.Case
scenario "check greet command",
using: Demo,
invoke: "hello",
expect: [approx: "Hello"]
end
- Build your elixir app as an escript, just make sure to add the
escript: [main_module: Demo]
to the:project
section of yourmix.exs
. See mix escript.build for more details.
$ mix escript.build
Generated escript demo
$ ./demo hello
Hello tuxuser!
- Visit the official site
- Read the docs on HexDocs
- Look into the examples folder for complete escripts
Tux is open source software licensed under the Apache 2.0 License.