Clip (CLI Parser) allows you to deserialize CLI parameters into an object, and generates the help for you.
The goal of Clip is to let you in control. It does not execute your code. It does not print anything. It can read from ARGV but also from any array of strings. You choose what you want to do.
Documentation: https://erdnaxeli.github.io/clip/
-
Add the dependency to your
shard.yml
:dependencies: clip: github: erdnaxeli/clip
-
Run
shards install
In a file command.cr:
require "clip"
@[Clip::Doc("An example commmand.")]
struct Command
include Clip::Mapper
@[Clip::Doc("Enable some effect.")]
getter effect = false
@[Clip::Doc("The file to work on.")]
getter file : String
end
begin
command = Command.parse
rescue ex : Clip::ParsingError
puts ex
exit
end
case command
when Clip::Mapper::Help
puts command.help
else
if command.effect
puts "Doing something with an effect on #{command.file}."
else
puts "Doing something on #{command.file}."
end
end
Then:
$ crystal build command.cr
$ ./command
Error:
argument is required: FILE
$ ./command --help
Usage: ./command [OPTIONS] FILE
An example commmand.
Arguments:
FILE The file to work on. [required]
Options:
--effect / --no-effect Enable some effect. [default: false]
--help Show this message and exit.
$ ./command myfile
Doing something on myfile.
$ ./command --effect myfile
Doing something with an effect on myfile.
- Fork it (https://github.com/erdnaxeli/clip/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
- Alexandre Morignot - creator and maintainer