• Stars
    star
    29
  • Rank 860,307 (Top 17 %)
  • Language
    Crystal
  • License
    ISC License
  • Created over 6 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple declarative command line interface builder

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:

  • Compile time validation - methods must accept all possible options and arguments
  • No possible double commands/options at compile-time
  • Declarative NamedTuple configuration
  • Customizable configuration - supports all languages (See Clicr.new parameters)
  • Fast execution, limited runtime

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,
  }
}
  • action is a NamedTuple with as key the method to call, and as a value a command alia, which can be empty for none.
  • in action, parentheses can be added to determine the arguments placement, like File.new().file
  • label is supposed to be short, one-line description
  • description can be a multi-line description of the command. If not set, label will be used.

Arguments

Example: command FooBar, command mysource mytarget

arguments: %w(directory names)
arguments: {"source", "target"}
  • if a Tuple is given, the arguments number must be exactly the Tuple size.
  • if an Array is given, the arguments number must be at least, or more, the Array size

Options

Boolean options

Example: -y, --no-confirm

options: {
  no_confirm: {
    short: 'y',
    label: "No confirmations",
  }
}
  • short creates a short alias of one character - must be a Char
  • concatenating single characters arguments like -Ry1 is possible
  • dashes -, being invalid named arguments, will be replaced by _ when calling the action method.

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",
  }
}
  • an optional default value can be set.
  • if a default is not set, a type can be defined to cast from a given type, instead of a raw String. For example, type: Int32 will call Int32.new.
  • can only be String (because arguments passed as ARGV are Array(String)) - if others type are needed, the cast must be done after the action method call

License

Copyright (c) 2020 Julien Reichardt - ISC License

More Repositories

1

dockerfiles

Repository for my dockerfiles
Dockerfile
123
star
2

cride

A light CLI text editor/IDE written in Crystal
Crystal
50
star
3

crystalizer

(De)serialize any Crystal object - out of the box. Supports JSON, YAML and Byte format.
Crystal
39
star
4

con

A simple, fast and readable JSON-compatible serialization format
Crystal
23
star
5

pool.cr

A simple thread-safe generic pool.
Crystal
14
star
6

crystal-autobind

Automatic C bindings generator for Crystal
Crystal
14
star
7

crystal-object-send

Interpret a String to an Object method call
Crystal
13
star
8

tail.cr

Tailing library for Crystal - get and/or follow the end of a file/IO
Crystal
10
star
9

error.cr

Efficient errors without raising exceptions - no expensive stack unwinding
Crystal
8
star
10

libcrown

Library for Unix users, groups and passwords manipulation in Crystal
Crystal
7
star
11

minetest-entity_modifier

Modify the model and size of entities, including players.
Lua
5
star
12

semantic_compare

Compare semver versions using semantic expressions similar to ones from npm's semver implementation
Crystal
5
star
13

crystal-byte-protocol

A byte protocol for Crystal
Crystal
4
star
14

crystalxd

Crystal client for the LXD REST API
Crystal
4
star
15

sherd

Crystal package manager
Crystal
3
star
16

dotfiles

My Git-versioned dotfiles
CSS
2
star
17

crystal-open-simplex-noise

2D, 3D, and 4D open simplex noise in crystal.
Crystal
2
star
18

dstats

Simple docker stats to get performance stats of your containers
Shell
2
star
19

urandgen.sh

Generate random numbers in base32 or base64 with /dev/urandom
Shell
2
star
20

SimpleShellMenu

A minimal POSIX shell menu in 50 lines that only use stty, dd and od
Shell
2
star
21

unicode_blocr

Identify the Unicode block to which a character belong.
Crystal
2
star
22

DustShip

A little POSIX compliant shell game where the goal is to collect asteroids with a ship
Shell
2
star
23

Orball

A Cordova/mobile compatible experiment game using Phaser with CoffeeScript - https://j8r.github.io/Orball/
JavaScript
2
star
24

js-byte-format

Encode/decode JavaScript datatypes (String, Number, Object) to/from bytes
JavaScript
1
star
25

dynany

Dynamic JSON/YAML mapping manipulation - extends Any
Crystal
1
star