opts.cr Build Status

a wrapper for OptionParser to provide default values and handy args

crystal versions

Installation

Add this to your application's shard.yml:

dependencies:
  opts:
    github: maiha/opts.cr
    version: 0.6.0

Opts module

class methods

class consts

override PROGRAM VERSION ARGS USAGE as you like.

instance methods

Flow

  1. App(or Opts).run(ARGV) # appliation entrypoint
  2. app = App.new # instance creation
  3. app.run(ARGV) # instance etnrypoint
    • app.setup(ARGV) # parse ARGV by parse and args is now available
    • app.run # (required) write main routine here
    • app.on_error(err) # only when an error occurred in run(ARGV)

Usage

require "opts"

class App
  include Opts

  VERSION = "0.1.0"
  PROGRAM = "foo"

  option host    : String, "-h host"  , "Server hostname", "127.0.0.1"
  option port    : Int32 , "-p port"  , "Server port number", 80
  option help    : Bool  , "--help"   , "Show this help", false
  option version : Bool  , "--version", "Print the version and exit", false

  def run
    p [host, port, args]
  end
end

App.run
% ./foo
["127.0.0.1", 80, []]

% ./foo a
["127.0.0.1", 80, ["a"]]

% ./foo a b
["127.0.0.1", 80, ["a", "b"]]

% ./foo -h localhost a b
["localhost", 80, ["a", "b"]]

% ./foo --version
foo 0.1.0

% ./foo -x
foo version 0.1.0

Usage: foo

Options:
  -h host                          Server hostname (default: 127.0.0.1).
  -p port                          Server port number (default: 80).
  --help                           Show this help.
  --version                        Print the version and exit.

Invalid option: -x

with block

class App1
  include Opts

  option list : Array(String)       , "-s VALUE", "store into list" do list << v; end
  option hash : Hash(String, String), "-d VALUE", "store into hash" do
    key, val = v.split("=", 2)
    hash[key] = val
  end
end

App1.run(["-s", "a"]).list   # => ["a"]
App1.run(["-d", "x=1"]).hash # => {"x" => "1"}

Nilable

class App2
  include Opts

  option token : String?, "-a TOKEN", "Your access token", nil
end

App2.run.token                # => nil
App2.run(["-a", "xxx"]).token # => "xxx"

Further usages

Real products

Contributing

  1. Fork it ( https://github.com/maiha/opts.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