svenfuchs/cl

View on GitHub
examples/args/required

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Required < Cl::Cmd
  register :required

  arg :one, required: true
  arg :two

  def run
    p cmd: registry_key, one: one, two: two
  end
end

Cl.new($0).run(%w(required one two))

# Output:
#
#   {:cmd=>:required, :one=>"one", :two=>"two"}

Cl.new($0).run(%w(required one))

# Output:
#
#   {:cmd=>:required, :one=>"one", :two=>nil}

Cl.new($0).run(%w(required))

# Output:
#
#   Missing arguments (given: 0, required: 1)
#
#   Usage: bin/examples required one [two] [options]
#
#   Arguments:
#
#     one         type: string, required
#     two         type: string
#
#   Options:
#
#     --help      Get help on this command

Cl.new($0).run(%w(required one two three))

# Output:
#
#   Too many arguments: one two three (given: 3, allowed: 2)
#
#   Usage: bin/examples required one [two] [options]
#
#   Arguments:
#
#     one         type: string, required
#     two         type: string
#
#   Options:
#
#     --help      Get help on this command