Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mosop/cli
Yet another Crystal library for building command-line interface applications.
https://github.com/mosop/cli
cli crystal-library
Last synced: about 1 month ago
JSON representation
Yet another Crystal library for building command-line interface applications.
- Host: GitHub
- URL: https://github.com/mosop/cli
- Owner: mosop
- License: mit
- Created: 2016-06-07T15:30:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-06-27T03:55:42.000Z (over 4 years ago)
- Last Synced: 2024-08-01T17:32:31.065Z (4 months ago)
- Topics: cli, crystal-library
- Language: Crystal
- Homepage:
- Size: 223 KB
- Stars: 101
- Watchers: 2
- Forks: 8
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - cli - Library for building command-line interface applications (CLI Builders)
- awesome-crystal - cli - Library for building command-line interface applications (Cli Builders)
README
# Crystal CLI
Yet another Crystal library for building command-line interface applications.
[![CircleCI](https://circleci.com/gh/mosop/cli.svg?style=shield)](https://circleci.com/gh/mosop/cli)
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
cli:
github: mosop/cli
```## Code Samples
### Option Parser
```crystal
class Hello < Cli::Command
class Options
bool "--bye"
arg "to"
enddef run
if args.bye?
print "Goodbye"
else
print "Hello"
end
puts " #{args.to}!"
end
endHello.run %w(world) # prints "Hello, world!"
Hello.run %w(--bye world) # prints "Goodbye, world!"
```### Subcommand
```crystal
class Polygon < Cli::Supercommand
command "triangle", default: trueclass Triangle < Cli::Command
def run
puts 3
end
endclass Square < Cli::Command
def run
puts 4
end
endclass Hexagon < Cli::Command
def run
puts 6
end
end
endPolygon.run %w(triangle) # prints "3"
Polygon.run %w(square) # prints "4"
Polygon.run %w(hexagon) # prints "6"
Polygon.run %w() # prints "3"
```### Replacing
```crystal
class New < Cli::Command
def run
puts "new!"
end
endclass Obsolete < Cli::Command
replacer_command New
endObsolete.run # prints "new!"
```### Inheritance
```crystal
abstract class Role < Cli::Command
class Options
string "--name"
end
endclass Chase < Cli::Supercommand
class Mouse < Role
def run
puts "#{options.name} runs away."
end
endclass Cat < Role
def run
puts "#{options.name} runs into a wall."
end
end
endChase.run %w(mouse --name Jerry) # prints "Jerry runs away."
Chase.run %w(cat --name Tom) # prints "Tom runs into a wall."
```### Help
```crystal
class Call < Cli::Command
class Help
header "Receives an ancient message."
footer "(C) 20XX mosop"
endclass Options
arg "message", desc: "your message to call them", required: true
bool "-w", not: "-W", desc: "wait for response", default: true
help
end
endCall.run %w(--help)
```Output:
```
call [OPTIONS] MESSAGEReceives an ancient message.
Arguments:
MESSAGE (required) your message to call themOptions:
-w wait for response
(default: true)
-W disable -w
-h, --help show this help(C) 20XX mosop
```### Versioning
```crystal
class Command < Cli::Supercommand
version "1.0.0"class Options
version
end
endCommand.run %w(-v) # prints 1.0.0
```### Shell Completion
```crystal
class TicketToRide < Cli::Command
class Options
string "--by", any_of: %w(train plane taxi)
arg "for", any_of: %w(kyoto kanazawa kamakura)
end
endputs TicketToRide.generate_bash_completion
# or
puts TicketToRide.generate_zsh_completion
```## Usage
```crystal
require "cli"
```and see:
* [Code Samples](#code_samples)
* [Wiki](https://github.com/mosop/cli/wiki)
* [API Document](http://mosop.me/cli/Cli.html)## Want to Do
- Application-Level Logger
- I18n## Release Notes
See [Releases](https://github.com/mosop/cli/releases).