Skip to content

Commit

Permalink
add examples, generate readme
Browse files Browse the repository at this point in the history
  • Loading branch information
svenfuchs committed May 18, 2019
1 parent f67b08d commit cad2057
Show file tree
Hide file tree
Showing 38 changed files with 1,254 additions and 39 deletions.
541 changes: 532 additions & 9 deletions README.md

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions bin/readme
@@ -0,0 +1,12 @@
#!/usr/bin/env ruby
$: << File.expand_path('../../lib', __FILE__)

require 'erb'

def example(path)
str = File.read("examples/#{path}")
str.lines[5..-1].join
end

erb = ERB.new(File.read('var/README.erb.md'), nil, '-')
puts erb.result
22 changes: 22 additions & 0 deletions examples/readme/alias
@@ -0,0 +1,22 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP', alias: :group

def run
p opts, to, to?, group, group?
end
end

Cl.new('owners').run(%w(add --to one))

# Output:
#
# {"to" => "one"}
# "one"
# true
# "one"
# true
16 changes: 16 additions & 0 deletions examples/readme/args
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
args :owners

def run
p owners
end
end

Cl.new('owners').run(%w(add one,two))

# => ["one,two"]
18 changes: 18 additions & 0 deletions examples/readme/array
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP', type: :array

def run
p to
end
end

Cl.new('owners').run(%w(add --to one --to two))

# Output:
#
# ["one", "two"]
18 changes: 18 additions & 0 deletions examples/readme/default
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP', default: 'default'

def run
p to
end
end

Cl.new('owners').run(%w(add))

# Output:
#
# "default"
21 changes: 21 additions & 0 deletions examples/readme/deprecated
@@ -0,0 +1,21 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP'
opt '--target GROUP', deprecated: 'Deprecated: --target'

def run
p to, deprecated_opts
end
end

Cl.new('owners').run(%w(add --target one))

# Output:
#
# "one"
# {:target=>'Deprecated: --target'}

20 changes: 20 additions & 0 deletions examples/readme/deprecated_alias
@@ -0,0 +1,20 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP', alias: :target, deprecated: :target

def run
p to, deprecated_opts
end
end

Cl.new('owners').run(%w(add --target one))

# Output:
#
# "one"
# {:target=>:to}

18 changes: 18 additions & 0 deletions examples/readme/downcase
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP', downcase: true

def run
p to
end
end

Cl.new('owners').run(%w(add --to ONE))

# Output:
#
# "one"
26 changes: 26 additions & 0 deletions examples/readme/enum
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP', enum: %w(one two)

def run
p to
end
end

Cl.new('owners').run(%w(add --to one))

# Output:
#
# "one"

Cl.new('owners').run(%w(add --to unknown))

# Unknown value: to=unknown (known values: one, two)
#
# Usage: enum add [options]
#
# Options: ...
17 changes: 17 additions & 0 deletions examples/readme/example
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP', example: 'group-one'
end

Cl.new('owners').run(%w(add --help))

# Usage: example add [options]
#
# Options:
#
# --to GROUP type: string, e.g.: group-one
# --help Get help on this command
29 changes: 29 additions & 0 deletions examples/readme/format
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP', format: /^\w+$/

def run
p to
end
end

Cl.new('owners').run(%w(add --to one))

# Output:
#
# "one"

Cl.new('owners').run(['add', '--to', 'does not match!'])

Invalid format: to (format: /^\w+$/)

Usage: format add [options]

Options:

--to GROUP type: string, format: /^\w+$/
--help Get help on this command
18 changes: 18 additions & 0 deletions examples/readme/internal
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP'
opt '--hidden', internal: true
end

Cl.new('owners').run(%w(add --help))

# Usage: example add [options]
#
# Options:
#
# --to GROUP type: string, e.g.: group-one
# --help Get help on this command
29 changes: 29 additions & 0 deletions examples/readme/max
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--retries COUNT', max: 5, type: :integer

def run
p retries
end
end

Cl.new('owners').run(%w(add --retries 1))

# Output:
#
# 1

Cl.new('owners').run(%w(add --retries 10))

# Exceeds max value: retries (max: 5)
#
# Usage: max add [options]
#
# Options:
#
# --retries COUNT type: integer, max: 5
# --help Get help on this command
20 changes: 20 additions & 0 deletions examples/readme/opts
@@ -0,0 +1,20 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP'

def run
p opts, to, to?
end
end

Cl.new('owners').run(%w(add --to one))

# Output:
#
# {"to" => "one"}
# "one"
# true
29 changes: 29 additions & 0 deletions examples/readme/required
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP', required: true

def run
p to
end
end

Cl.new('owners').run(%w(add --to one))

# Output:
#
# "one"

Cl.new('owners').run(%w(add))

# Missing required option: to
#
# Usage: required add [options]
#
# Options:
#
# --to GROUP type: string, required: true
# --help Get help on this command
36 changes: 36 additions & 0 deletions examples/readme/requireds
@@ -0,0 +1,36 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
# read DNF, i.e. "apikey OR username AND password
required :api_key, [:username, :password]

opt '--api_key KEY'
opt '--username NAME'
opt '--password PASS'

def run
p to, retries
end
end

Cl.new('owners').run(%w(add --to one --retries 1))

# Output:
#
# "one"
# 1

Cl.new('owners').run(%w(add --retries 1))

# Missing option: to (required by retries)
#
# Usage: requires add [options]
#
# Options:
#
# --to GROUP type: string
# --retries INT type: string, requires: to
# --help Get help on this command
32 changes: 32 additions & 0 deletions examples/readme/requires
@@ -0,0 +1,32 @@
#!/usr/bin/env ruby
$: << File.expand_path('lib')

require 'cl'

class Add < Cl::Cmd
opt '--to GROUP'
opt '--retries INT', requires: :to

def run
p to, retries
end
end

Cl.new('owners').run(%w(add --to one --retries 1))

# Output:
#
# "one"
# 1

Cl.new('owners').run(%w(add --retries 1))

# Missing option: to (required by retries)
#
# Usage: requires add [options]
#
# Options:
#
# --to GROUP type: string
# --retries INT type: string, requires: to
# --help Get help on this command

0 comments on commit cad2057

Please sign in to comment.