class OptionParser

Overview

OptionParser is a class for command-line options processing. It supports:

Run crystal for an example of a CLI built with OptionParser.

Short example:

require "option_parser"

upcase = false
destination = "World"

OptionParser.parse! do |parser|
  parser.banner = "Usage: salute [arguments]"
  parser.on("-u", "--upcase", "Upcases the sallute") { upcase = true }
  parser.on("-t NAME", "--to=NAME", "Specifies the name to salute") { |name| destination = name }
  parser.on("-h", "--help", "Show this help") { puts parser }
end

destination = destination.upcase if upcase
puts "Hello #{destination}!"

Defined in:

option_parser.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from class Reference

! !, ==(other)
==(other : self)
==
, hash hash, inspect(io : IO) : Nil inspect, nil? nil?, object_id object_id, same?(other : Nil)
same?(other : Reference)
same?
, to_s(io : IO) : Nil to_s

Instance methods inherited from class Object

! !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO)
inspect
, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try

Class methods inherited from class Object

==(other : Class) ==, ===(other) ===, cast(other) : self cast, from_json(string_or_io) from_json, from_yaml(string : String) from_yaml, hash hash, inspect(io) inspect, name : String name, to_s(io) to_s

Class Method Detail

def self.new(&block) #

Creates a new parser, with its configuration specified in the block.


[View source]
def self.parse(args, &block) #

Creates a new parser, with its configuration specified in the block, and uses it to parse the passed args.


[View source]
def self.parse!(&block) #

Creates a new parser, with its configuration specified in the block, and uses it to parse the arguments passed to the program.


[View source]
def self.new #

Creates a new parser.


[View source]

Instance Method Detail

def banner=(banner) #

Establishes the initial message for the help printout. Typically, you want to write here the name of your program, and a one-line template of its invocation.

Example:

parser.banner = "Usage: crystal [command] [switches] [program file] [--] [arguments]"

[View source]
def on(flag, description, &block : String -> ) #

Establishes a handler for a flag.

Flags can (but don't have to) start with a dash. They can also have an optional argument, which will get passed to the block. Each flag has a description, which will be used for the help message.

Examples of valid flags:

  • -a, -B
  • --something-longer
  • -f FILE, --file FILE, --file=FILE (these will yield the passed value to the block as a string)

[View source]
def on(short_flag, long_flag, description, &block : String -> ) #

Establishes a handler for a pair of short and long flags.

See the other definition of #on for examples.


[View source]
def parse(args) #

Parses the passed args, running the handlers associated to each option.


[View source]
def parse! #

Parses the passed the arguments passed to the program, running the handlers associated to each option.


[View source]
def separator(message = "") #

Adds a separator, with an optional header message, that will be used to print the help.

This way, you can group the different options in an easier to read way.


[View source]
def to_s(io : IO) #

Returns all the setup options, formatted in a help message.


[View source]
def unknown_args(&unknown_args : Array(String), Array(String) -> ) #

Sets a handler for arguments that didn't match any of the setup options.

You typically use this to get the main arguments (not modifiers) that your program expects (for example, filenames)


[View source]