Skip to content

Latest commit

 

History

History
106 lines (73 loc) · 2.96 KB

File metadata and controls

106 lines (73 loc) · 2.96 KB

CLI.K

Source Code | Report Issue

Gem Version

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.

Usage

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.

Bundled Short Flags

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 { ... }

Asking the User Things

CLI.K also ships a tiny ask helper for prompting:

require 'clik/ask'

ans = ask "Are you nice? [Y/n] ", "Y"

Lineage and What CLI.K Adds to Clap

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.

Installation

$ gem install clik

Or in a Gemfile:

gem 'clik'

Copyrights & License

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.