cling

A modular, non-macro-based command line interface library cli command-line cling
2.0.1 released
devnote-dev/cling
21 2 2
Devonte W

CLI.cr

Yet another command line interface library for Crystal. Based on spf13/cobra, CLI.cr is built to be almost entirely modular, giving you absolute control over almost everything without the need for embedded macros - there isn't even a default help command or flag!

Installation

  1. Add the dependency to your shard.yml:
dependencies:
  cli:
    github: devnote-dev/cli.cr
    branch: stable
  1. Run shards install

Usage

require "cli"

class MainCmd < CLI::Command
  def setup : Nil
    @name = "greet"
    @description = "Greets a person"
    add_argument "name", desc: "the name of person to greet", required: true
    add_option 'c', "caps", desc: "greet with capitals"
    add_option 'h', "help", desc: "sends help information"
  end

  def pre_run(args, options)
    if options.has? "help"
      puts help_template # generated using CLI::Formatter

      false
    else
      true
    end
  end

  def run(args, options) : Nil
    msg = "Hello, #{args.get("name")}!"

    if options.has? "caps"
      puts msg.upcase
    else
      puts msg
    end
  end
end

main = MainCmd.new
main.execute ARGV
$ crystal greet.cr -h
Usage:
        greet <arguments> [options]

Arguments:
        name    the name of person to greet (required)

Options:
        -c, --caps  greet with capitals
        -h, --help  sends help information

$ crystal greet.cr Dev
Hello, Dev!

$ crystal greet.cr -c Dev
HELLO, DEV!

Contributing

  1. Fork it (https://github.com/devnote-dev/cli.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

This repository is managed under the Mozilla Public License v2.

© 2022 devnote-dev

cling:
  github: devnote-dev/cling
  version: ~> 2.0.1
License MPL
Crystal 1.5.0

Authors

Dependencies 0

Development Dependencies 0

Dependents 0

Last synced .
search fire star recently