Skip to content
mosop edited this page Dec 1, 2016 · 11 revisions

The model is a main concept in optarg. Models are used for modeling command-line options and arguments. A model is just a Crystal class and inherits the Optarg::Model class.

class Model < Optarg::Model
end

To define options and arguments, call the API methods inside model classes:

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

To parse a command line, call Optarg::Model.parse:

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

result = Model.parse(%w(-s hello))

To access parsed values, use value accessors or value containers:

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

result = Model.parse(%w(-s hello))
result.s # => hello
result.options[String]["-s"] # => hello 
Clone this wiki locally