Home

Awesome

Clicr

Build Status ISC

Command Line Interface for Crystal

A simple Command line interface builder which aims to be easy to use.

Installation

Add the dependency to your shard.yml:

dependencies:
  clicr:
    github: j8r/clicr

Features

This library uses generics, thanks to Crystal's powerful type-inference, and few macros, to provide this following advantages:

Usage

Simple example

require "clicr"

Clicr.new(
  label: "This is my app.",
  commands: {
    talk: {
      label:      "Talk",
      action:    {"CLI.say": "t"},
      arguments: %w(directory),
      options:   {
        name: {
          label:   "Your name",
          default: "foo",
        },
        no_confirm: {
          short: 'y',
          label:  "Print the name",
        },
      },
    },
  },
).run

module CLI
  def self.say(arguments, name, no_confirm)
    puts arguments, name, no_confirm
  end
end

Example of commands:

$ myapp --help
Usage: myapp COMMANDS [OPTIONS]

Myapp can do everything

COMMANDS
  t, talk   Talk

OPTIONS
  --name=foo         Your name
  -y, --no-confirm   Print the name

'myapp --help' to show the help.
$ myapp talk /tmp name=bar
no, bar in /tmp
$ myapp talk home name=bar -y
yes, bar in home
$ myapp talk test
no, foo in test

Advanced example

See the one in the spec test

CLI Composition

It's also possible to merge several commands or options together.

other = {
  pp: {
    label: "It pp",
    action: "pp"
  }
}

Clicr.new(
  label: "Test app",
  commands: {
    puts: {
      alias: 'p',
      label: "It puts",
      action: "puts",
    }.merge(other) 
  }
)

Help output:

Usage: myapp COMMAND

Test app

COMMAND
  p, puts   It puts
  pp        It pp

'myapp --help' to show the help.

Reference

Commands

Example: s, start

commands: {
  short: {
    action: { "say": "s" },
    label: "Starts the server",
    description: <<-E.to_s,
    This is a full multi-line description
    explaining the command
    E,
  }
}

Arguments

Example: command FooBar, command mysource mytarget

arguments: %w(directory names)
arguments: {"source", "target"}

Options

Boolean options

Example: -y, --no-confirm

options: {
  no_confirm: {
    short: 'y',
    label: "No confirmations",
  }
}

Special case: the help_option, which is set to "help" with the options -h, --help by default, shows the help of the current (sub)command

String options

Example: --name=foo, or --name foo

options: {
  name: {
    label: "This is your name",
    default: "Foobar",
  }
}

License

Copyright (c) 2020 Julien Reichardt - ISC License