Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Repository files navigation

DEPRECATED

This library is no longer being maintained. It is being kept online to allow for past versions of the Amber CLI to still compile on pre Crystal 1.14 releases. Message Date: July 22nd 2025

Crystal CLI

Yet another Crystal library for building command-line interface applications.

CircleCI

Installation

Add this to your application's shard.yml:

dependencies:
cli:
github: mosop/cli

Code Samples

Option Parser

classHello < Cli::CommandclassOptions
bool "--bye"
arg "to"enddefrunif args.bye?
print"Goodbye"elseprint"Hello"endputs"#{args.to}!"endendHello.run %w(world)# prints "Hello, world!"Hello.run %w(--bye world)# prints "Goodbye, world!"

Subcommand

classPolygon < Cli::Supercommand
command "triangle", default:trueclassTriangle < Cli::Commanddefrunputs3endendclassSquare < Cli::Commanddefrunputs4endendclassHexagon < Cli::Commanddefrunputs6endendendPolygon.run %w(triangle)# prints "3"Polygon.run %w(square)# prints "4"Polygon.run %w(hexagon)# prints "6"Polygon.run %w()# prints "3"

Replacing

classNew < Cli::Commanddefrunputs"new!"endendclassObsolete < Cli::Command
replacer_command NewendObsolete.run # prints "new!"

Inheritance

abstractclassRole < Cli::CommandclassOptions
string "--name"endendclassChase < Cli::SupercommandclassMouse < Roledefrunputs"#{options.name} runs away."endendclassCat < Roledefrunputs"#{options.name} runs into a wall."endendendChase.run %w(mouse --name Jerry)# prints "Jerry runs away."Chase.run %w(cat --name Tom)# prints "Tom runs into a wall."

Help

classCall < Cli::CommandclassHelp
header "Receives an ancient message."
footer "(C) 20XX mosop"endclassOptions
arg "message", desc:"your message to call them", required:true
bool "-w", not:"-W", desc:"wait for response", default:true
help
endendCall.run %w(--help)

Output:

call [OPTIONS] MESSAGE
Receives an ancient message.
Arguments:
MESSAGE (required) your message to call them
Options:
-w wait for response
(default: true)
-W disable -w
-h, --help show this help
(C) 20XX mosop

Versioning

classCommand < Cli::Supercommand
version "1.0.0"classOptions
version
endendCommand.run %w(-v)# prints 1.0.0

Shell Completion

classTicketToRide < Cli::CommandclassOptions
string "--by", any_of:%w(train plane taxi)
arg "for", any_of:%w(kyoto kanazawa kamakura)endendputsTicketToRide.generate_bash_completion
# orputsTicketToRide.generate_zsh_completion

Usage

require"cli"

and see:

Want to Do

  • Application-Level Logger
  • I18n

Release Notes

See Releases.

About

Yet another Crystal library for building command-line interface applications.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages