CLI.cr
Yet another command line interface library for Crystal. Based on spf13/cobra, CLI.cr is built to be almost entirely modular, giving you absolute control over almost everything without the need for embedded macros - there isn't even a default help command or flag!
Installation
- Add the dependency to your
shard.yml
:
dependencies:
cli:
github: devnote-dev/cli.cr
branch: stable
- Run
shards install
Usage
require "cli"
class MainCmd < CLI::Command
def setup : Nil
@name = "greet"
@description = "Greets a person"
add_argument "name", desc: "the name of person to greet", required: true
add_option 'c', "caps", desc: "greet with capitals"
add_option 'h', "help", desc: "sends help information"
end
def pre_run(args, options)
if options.has? "help"
puts help_template # generated using CLI::Formatter
false
else
true
end
end
def run(args, options) : Nil
msg = "Hello, #{args.get("name")}!"
if options.has? "caps"
puts msg.upcase
else
puts msg
end
end
end
main = MainCmd.new
main.execute ARGV
$ crystal greet.cr -h
Usage:
greet <arguments> [options]
Arguments:
name the name of person to greet (required)
Options:
-c, --caps greet with capitals
-h, --help sends help information
$ crystal greet.cr Dev
Hello, Dev!
$ crystal greet.cr -c Dev
HELLO, DEV!
Contributing
- Fork it (https://github.com/devnote-dev/cli.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Devonte W - creator and maintainer
This repository is managed under the Mozilla Public License v2.
© 2022 devnote-dev