Skip to content

Parsing Options

mosop edited this page Nov 14, 2016 · 16 revisions

Integration with optarg

Crystal CLI integrates its APIs with optarg. optarg is a library for parsing command-line options and arguments.

Once you define a command class, the class automatically have an optarg's model class that is named Options. You can define options and arguments inside the Options class.

To access values, that are parsed by optarg, in a running context, use the following methods:

  • options
  • args
  • named_args
  • nameless_args
  • parsed_args
  • unparsed_args

These methods are proxy methods and internally invoke the corresponding optarg's API methods. The method names are the same as the optarg API.

class Smile < Cli::Command
  class Options
    bool "--say-hello"
  end

  def run
    puts "hello" if options.say_hello?
    puts ":)"
  end
end

Smile.run %w(--say-hello)

This prints:

hello
:)

Clone this wiki locally