A Crystal CLI library that absorbs most of the tedious processing required by the CLI and allows users to focus only on the essential logic.
- crystal : 0.27.2 0.31.1 0.32.1 0.33.0 0.34.0
- Simple and readable syntax for command and task
- Multiple tasks for sub commands
- Auto-navigate when commands and tasks are not found
- Easy accessors for args
- Around filter to commands
Cmds.command defines a new command and its
run method is used for the main logic.
For example, hello command can be defined as follows.
Cmds.command "hello" do def run puts "Hello world!" end end Cmds.run(ARGV)
$ prog hello Hello world!
When you want sub commands, it can be defined as task by
For example, you can define a
pretty task in the
json command as follows.
Cmds.command "json" do task "pretty" do path = args.shift? || abort "specify file" puts Pretty.json(File.read(path)) end end Cmds.run(ARGV)
$ echo "[1,2]" > foo.json $ prog json pretty specify file $ prog json pretty foo.json [ 1, 2 ]
In this case, only one task is defined, but of course you can define multiple tasks with
If a command or task is not sufficiently specified, it will automatically navigate.
For example, if you call the above
prog binary without args, it navigates possible commands.
$ prog usage: prog <command> ... prog ^^^^^^^^^ commands: json missing <command>.
Then, it navigates tasks too.
$ prog json usage: prog json <task> ... prog json ^^^^^^ tasks: pretty missing <task>.
args : Array(String)presents CLI args
arg2is used for handy accessors
For example, in the case of a
sum task that returns the addition of two arguments,
arg2 methods are available as follows.
Cmds.command "math" do task "sum", "a b" do a = arg1(&.to_i) b = arg2(&.to_i) puts a + b end end Cmds.run
$ prog math sum 1 2 3
Here, "a b" the second argument of the task definition is directly used as usage hint when navigating.
$ prog math sum 1 usage: prog math sum a b prog math sum 1 missing <arg2>.
after methods will be automatically fired.
Add this to your application's
dependencies: cmds: github: maiha/cmds.cr version: 0.3.7
- Fork it (https://github.com/maiha/cmds.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
- maiha maiha - creator, maintainer