Skip to content

Arguments types

So far we used two types for an argument: String and String?. But like options you can use numeric types too.

The supported types are:

  • all subtypes of Int: Int32, UInt32, and all others
  • all subtypes of Float: BigFloat, Float32 and Float64
  • String
  • and all those types combined with Nil: Int32?, Float32?, String?, and so on.

Bool is not supported. It can only be used with a flag.

During the parsing, Clip tries to convert the string to the attribute's type. If any error happens, a Clip::ParsingError is raised (see the errors section for details).

require "clip"

module Myapplication
  VERSION = "0.1.0"

  struct Command
    include Clip::Mapper

    getter name : Int32
  end

  def self.run
    begin
      command = Command.parse
    rescue ex : Clip::Error
      puts ex
      return
    end

    if command.is_a?(Clip::Mapper::Help)
      puts command.help
    else
      hello(command.name)
    end
  end

  def self.hello(name)
    puts "Hello #{name}"
  end
end

Myapplication.run
$ shards build
Dependencies are satisfied
Building: myapplication
$ ./bin/myapplication --help
Usage: ./bin/myapplication [OPTIONS] NAME

Arguments:
  NAME  [required]

Options:
  --help  Show this message and exit.
$ ./bin/myapplication Alice
Error:
  argument's value is invalid: NAME
$ ./bin/myapplication 42
Hello 42