百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

cli-kit

> 编程语言
开源

命令行接口框架/引擎

161 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

命令行接口框架/引擎

cli-kit

cli-kit is a ruby Command-Line application framework. Its primary design goals are:

  1. Modularity: The framework tries not to own your application, but rather to live on its edges.
  2. Startup Time: cli-kit encourages heavy use of autoloading (and uses it extensively internally) to reduce the amount of code loaded and evaluated whilst booting your application. We are able to achieve a 130ms runtime in a project with 21kLoC and ~50 commands.

cli-kit is developed and heavily used by the Developer Infrastructure team at Shopify. We use it to build a number of internal developer tools, along with cli-ui.

Getting Started

To begin creating your first cli-kit application, run:

bash
gem install cli-kit
cli-kit new myproject

Where myproject is the name of the application you wish to create. Then, you will be prompted to select how the project consumes cli-kit and cli-ui. The available options are:

  • Vendor (faster execution, more difficult to update dependencies)
  • Bundler (slower execution, easier dependency management)

You're now ready to write your very first cli-kit application!

Upgrading to version 5

Version 5 includes breaking changes and significant new features. Notably:

  • autocall is completely removed;
  • A help system and command-line option parsing system is added.

Migrating away from autocall is as simple as replacing autocall(:X) { y } with X = y.

Using the new help system is not yet well-documented, but some hints can be found in the gen/ilb/gen/commands directory of this repo. Existing commands in existing apps should continue to work.

To use the new help system, commands should all define invoke instead of call. invoke takes name as before, but the first argument is now an instance of Opts, defined in the command class, which must be a subclass of CLI::Kit::Opts. For example:

ruby
class MyCommand , '', ''`

### Creating the Command Action

The action for a specific command is stored in its own Ruby file, in the `/lib/myproject/commands/`
directory.  Here is an example of the `add` command in our previous to-do app example:
```ruby
require 'myproject'

module Myproject
  module Commands
    class Add < Myproject::Command
      def call(args, _name)
        # command action goes here
      end

      def self.help
        # help or instructions go here
      end
    end
  end
end

The call(args, _name) method is what actually runs when the myproject add command is executed.

  • Note: The args parameter represents all the arguments the user has specified.

Let's say that you are trying to compute the sum of 2 numbers that the user has specified as arguments. For example:

ruby
def call(args, _name)
  sum = args.map(&:to_i).inject(&:+)
  puts sum
end

Getting Help

Above, you'll notice that we also have a self.help method. This method is what runs when the user has incorrectly used the command, or has requested help. For example:

ruby
def self.help
  "Print the sum of 2 numbers.\nUsage: {{command:#{Myproject::TOOL_NAME} add}} 5 7"
end

User Interfaces

cli-kit also features cli-ui, another gem from us here at Shopify, which allows for the use of powerful command-line user interface elements. For more details on how to use cli-ui, visit the cli-ui repo.

Examples

  • A Simple To-Do App

Issues· 8 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言