CLI.K stands for Command Line Interface in the Kernel. It is a tiny
command-line option parser exposed as a single Kernel method, cli,
which maps option strings to lambdas. There is no DSL, no help-text
machinery, and no auto-generated usage. The whole library is well under
100 lines of code.
require 'clik'
cli '-f --file' => lambda { |f| @file = f },
'-d --debug' => lambda { $DEBUG = true },
'-h --help' => lambda { show_help }Each key is an option (or a whitespace-separated list of aliases) and each value is a lambda. The lambda's arity determines how many arguments the option consumes from the command line.
By default cli reads from ARGV, but you can hand it any array as the
first argument:
cli ['--file', 'hello.txt'],
'-f --file' => lambda { |f| @file = f }It returns whatever non-flag arguments were left over.
CLI.K expands run-on short flags the way standard Unix tools do:
# -abc is the same as -a -b -c
cli ['-abc'],
'-a' => lambda { ... },
'-b' => lambda { ... },
'-c' => lambda { ... }CLI.K also ships a tiny ask helper for prompting:
require 'clik/ask'
ans = ask "Are you nice? [Y/n] ", "Y"CLI.K is a direct descendant of Clap by Michel Martens, and the core option-dispatch loop is essentially copied from it. Michel deserves the credit for the central insight that this level of simplicity is all most command-line tools really need.
What CLI.K adds on top of Clap:
| Feature | Clap | CLI.K |
|---|---|---|
| Core option-to-lambda dispatch | ✓ | ✓ |
Class-based API (Clap.run(argv, opts)) |
✓ | |
Kernel method (cli ...) — no instantiation |
✓ | |
Aliases packed into one key ('-f --file' => ...) |
✓ | |
Bundled short flags (-abc → -a -b -c) |
✓ | |
ask helper for interactive prompts |
✓ |
If you want a class-based, minimal-no-extras parser, use Clap. If you want option aliases packed into the key and Unix-style short-flag bundling exposed via a Kernel method, use CLI.K. They are conceptually the same library with different ergonomic choices.
$ gem install clik
Or in a Gemfile:
gem 'clik'CLI.K is copyrighted open-source software.
Copyright (c) 2013 Rubyworks
Copyright (c) 2010 Michel Martens
Distributed under the BSD-2-Clause license. See LICENSE.txt and NOTICE.txt for details.