https://github.com/rubyworks/clik
Kernel#cli
https://github.com/rubyworks/clik
Last synced: 6 months ago
JSON representation
Kernel#cli
- Host: GitHub
- URL: https://github.com/rubyworks/clik
- Owner: rubyworks
- License: other
- Created: 2013-03-07T14:51:35.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2014-12-27T17:02:55.000Z (about 11 years ago)
- Last Synced: 2024-11-15T18:09:13.716Z (over 1 year ago)
- Language: Ruby
- Size: 206 KB
- Stars: 97
- Watchers: 7
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# CLI.K
[Website](http://github.com/rubyworks/clik) /
[Documentation](http://rubydoc.info/gems/clik/frames) /
[Report Issue](http://github.com/rubyworks/clik/issues) /
[Source Code](http://github.com/rubyworks/clik)
[](http://flattr.com/thing/324911/Rubyworks-Ruby-Development-Fund)
## About
CLI.K stands for Command Line Interface in the Kernel. It provides a very
simple `cli` method for parsing command line options.
## Status
[](http://badge.fury.io/rb/clik)
[](https://travis-ci.org/rubyworks/clik)
## Usage
### They call it "K.I.S.S."
Usage is very straightforward, although it might look a little odd at first
glance:
```ruby
require 'clik'
cli '-f --file' => lambda{ |f| @file = f },
'-d --debug' => lambda{ $DEBUG = true },
'-h --help' => lambda{ show_help }
```
There's very little to it really. The `cli` command simply maps command
line options to procedures which are used to process them. That's it.
In our example, notice that `-f` and `--file` are easily defined as
synonymous options. Simple. Then notice that the `-f/--file` option's
procedure takes an argument, so the command line option takes an argument
as well. Again simple.
The cli method has a few additional niceties. It can handle run-on flags,
i.e. `-abc` is the same as `-a -b -c`. And you can pass it an alternate
set of arguments to parse, as the first argument, to use something other
than the default `ARGV`.
```ruby
argv = ['--testing']
cli argv,
...
```
### You need help, no you really don't
At this point, you might be wondering about help output. Clearly there are
no descriptions given in our example. Well, guess what! It's really easy
to print something out yourself. In fact, if you really want to *do it right*,
create a manpage with [ronn](git://github.com/rtomayko/ronn.git) or
[md2man](https://github.com/sunaku/md2man), and impress your friends.
It's a much better approach then jamming all that verbiage into the command
line options parser code.
### Ask and you shell receive
In addition to #cli, CLI.K provides the #ask method. This is a very simple
command line query method.
ans = ask "Are you nice? [Y/n]"
Other Ruby libraries have their own take on the #ask method, and this very
simple implementation can just as soon be overridden. No biggy. But it's nice
to have for simple use cases.
## Acknowledgments
We have to give credit where credit is due. This interface is the great
achievement of Michel Martens, who created the original [Clap](https://github.com/soveran/clap)
library from which CLI.K evolved. Mr. Martens deserves high praise for this
design. It's not easy to realize that this level of simplicity is all one
really needs! Thank you, Michel!
## Copyrights & License
CLI.K is copyrighted open-source software.
Copyright (c) 2013 Rubyworks
CLI.K is base on Michel Marten's Clap library.
Copyright (c) 2010 Michel Martens
See LICENSE.txt and NOTICE.txt files for details.