The quick way to create a user-friendly Command Line Interface in Crystal. ⚡powered by Toːka
- Easily create a CLI with nested sub-commands and complex flags
- Automatic help screens, argument type coercion, input validation and meaningful error messages
-
Add the dependency to your
shard.yml:dependencies: cliq: github: busyloop/cliq
-
Run
shards install
require "cliq"
class GreetPerson < Cliq::Command
command "greet person"
summary "Greet someone"
description "I greet, therefore I am"
args ["<name> Name to greet"]
flags({
yell: Bool?,
count: {
type: Int32,
default: 1,
value_name: "TIMES",
description: "Print the greeting this many times"
}
})
def call(args)
raise Cliq::Error.new("Must provide a <name>") if args.size < 1
greeting = "Hello #{args[0]}!"
greeting = greeting.upcase if @yell
@count.times { puts greeting }
end
end
# Let's go!
Cliq.invoke(ARGV)- You can have any number of
Cliq::Commandsubclasses in your program.
Cliq merges them together to form the final CLI. - Each must have a method
#call(args : Array(String)). - Use
commandto declare the command name- Spaces are allowed.
If you want a sub-commandfoo bar batzthen just put exactly that in there.
- Spaces are allowed.
- Use
summaryto declare a short text to be displayed in the command list (top level help screen) - Use
descriptionto declare a longer text to be displayed in the command help screen (./foo bar --help) - Both
summaryanddescriptioncan be multi-line strings and will be auto-indented as needed - Use
argsto describe positional arguments- Takes an Array of String's in format
"[foo] Description"
- Takes an Array of String's in format
- Use
flagsto declare the flags that your command accepts - See examples/demo.cr for a demo with multiple sub-commands
flags({
verbose: Bool?,
count: Int32
})This allows --verbose or -v
and requires --count N or -c N (where N must be an integer).
flags({
verbose: {
type: Bool,
nilable: true,
description: "Enable verbosity"
}
count: {
type: Int32,
description: "Print the greeting this many times",
verifier: ->(n : Int32){ n >= 0 || "Must be greater than zero" }
}
})typeThe type. Examples:String,Int32?,Array(String)nilableIf the type is optional ("nil-able"). You can also make thetypenilable for the same effect.defaultThe default value.descriptionThe human-readable description. Can be multi-line.longAllows to manually configure long-options. Auto-generated from the name otherwise.shortAllows to manually configure short-options. Auto-generated otherwise. Set tofalseto disable.value_nameHuman-readable value name, shown next to the option name like:--foo=HEREcategoryHuman-readable category name, for grouping in the help page. Optional.value_converterConverter to use for the value.key_converterConverter for the key to use for aHashtype.verifierVerifier to validate the value.
Cliq is a thin wrapper around Toːka. Please refer to the Toːka documentation for advanced usage.
If you do not need sub-commands in your program then you should consider using Toːka directly.
- Fork it (https://github.com/busyloop/cliq/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
