class Clipper

Overview

Clipper lets you convert the ARGV array into a hash of all the expected and optional flags and arguments.

Defined in:

clipper.cr

Instance Method Summary

Instance Method Detail

def arg(name) #

Specifies an expected positional argument.

Use this method to capture any positional argument that is not associated with any flag. This is useful to capture sub-commands:

clipper = Clipper.new
clipper.arg "command"
options = clipper.parse ["download", "the-internet"]
options["command"]  # => "download"

or, to capture positional arguments for a command:

clipper.arg "source"
clipper.arg "target"
options = clipper.parse ["source.txt"]
options["source"]  # => "source.txt"
options["target"]  # => false

[View source]
def flag(long, short = nil, default = false) #

Specifies an expected flag.

After running #parse, the value provided by the user will be available using the long key.

clipper = Clipper.new
clipper.flag "--port PORT", "-p PORT", default: "3000"
options = clipper.parse ARGV
options["--port"]  # => "3000"

Both the short key and default value are optional.


[View source]
def parse(args = [] of String) #

Parses a array and returns a hash of expected and actual options.

This method typically expects ARGV as its input array.

clipper = Clipper.new

clipper.flag "--cache", "-c"
clipper.flag "--long-only"
clipper.flag "--port PORT", "-p PORT", default: "3000"
clipper.arg "command"

options = clipper.parse
# => {"--cache" => false, "--long-only" => false,
# => "--port" => "3000", "command" => false}

options = clipper.parse ["--cache", "-p", "4567"]
# => {"--cache" => true, "--long-only" => false,
# => "--port" => "4567", "command" => false}

options = clipper.parse ["download"]
# => {"--cache" => true, "--long-only" => false,
# => "--port" => "4567", "command" => "download"}

[View source]