optarg~amberframework

Yet another Crystal library for parsing command-line options and arguments.
0.1.12 released

optarg

Yet another library for parsing command-line options and arguments, written in the Crystal language.

Build Status

Installation

Add this to your application's shard.yml:

dependencies:
  optarg:
    github: mosop/optarg

Features

Accessor

class Model < Optarg::Model
  string "--foo"
end

result = Model.parse(%w(--foo bar))
result.foo # => "bar"

Nilable Accessor

class Model < Optarg::Model
  string "--foo"
end

result = Model.parse(%w())
result.foo? # => nil
result.foo # raises KeyError

Synonyms

class Model < Optarg::Model
  string %w(-f --file)
end

result = Model.parse(%w(-f foo.cr))
result.f # => "foo.cr"
result.file # => "foo.cr"

Boolean

class Model < Optarg::Model
  bool "-b"
end

result = Model.parse(%w(-b))
result.b? # => true

Array

class Model < Optarg::Model
  array "-e"
end

result = Model.parse(%w(-e foo -e bar -e baz))
result.e # => ["foo", "bar", "baz"]

Concatenation

class Model < Optarg::Model
  bool "-a"
  bool "-b"
end

result = Model.parse(%w(-ab))
result.a? # => true
result.b? # => true

Default Value

class Model < Optarg::Model
  string "-s", default: "string"
  bool "-b", default: false
  array "-a", default: %w(1 2 3)
end

result = Model.parse(%w())
result.s  # => "string"
result.b? # => false
result.a  # => ["1", "2", "3"]

Negation

class Model < Optarg::Model
  bool "-b", default: true, not: "-B"
end

result = Model.parse(%w(-B))
result.b? # => false

Arguments

class Model < Optarg::Model
  string "-s"
  bool "-b"
end

result = Model.parse(%w(-s foo -b bar -- baz))
result.args # => ["bar"]
result.unparsed_args # => ["baz"]

Inheritance (Reusable Model)

class Animal < Optarg::Model
  bool "--sleep"
end

class Cat < Animal
  bool "--mew"
end

class Dog < Animal
  bool "--woof"
end

Cat.parse(%w()).responds_to?(:sleep?) # => true
Dog.parse(%w()).responds_to?(:sleep?) # => true

Handler

class Model < Optarg::Model
  on("--goodbye") { goodbye! }

  def goodbye!
    raise "Goodbye, world!"
  end
end

Model.parse %w(--goodbye) # raises "Goodbye, world!"

Custom Initializer

class The
  def message
    "Someday again!"
  end
end

class Model < Optarg::Model
  def initialize(argv, @the : The)
    super argv
  end

  on("--goodbye") { raise @the.message }
end

Model.new(%w(--goodbye), The.new).parse # raises "Someday again!"

Usage

require "optarg"

and see Features.

Release Notes

  • v0.1.9
    • Concatenation
  • v0.1.3
    • Custom Initializer
  • v0.1.2
    • Synonyms
  • v0.1.1
    • Handler

Development

[WIP]

Contributing

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

  • mosop - creator, maintainer
optarg:
  github: amberframework/optarg
  version: ~> 0.1.12
License MIT
Crystal none

Authors

  • mosop

Dependencies 0

Development Dependencies 0

Dependents 2

Last synced .
search fire star recently