Class: DenCli

Inherits:
Object
  • Object
show all
Defined in:
lib/dencli.rb,
lib/dencli/version.rb

Defined Under Namespace

Classes: CMD, Interactive, Sub, UnknownCommand, UsageError

Constant Summary collapse

VERSION =
'0.5.6'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(progname, description) ⇒ DenCli

Returns a new instance of DenCli.



71
72
73
# File 'lib/dencli.rb', line 71

def initialize progname, description
	@subs = Sub.new self, progname, description
end

Instance Attribute Details

#subsObject (readonly)

Returns the value of attribute subs.



69
70
71
# File 'lib/dencli.rb', line 69

def subs
  @subs
end

Class Method Details

.assert_type(klass, method, argument_name, object, *types) ⇒ Object



5
6
7
8
9
# File 'lib/dencli.rb', line 5

def self.assert_type klass, method, argument_name, object, *types
	unless types.any? {|type| object.is_a? type }
		raise ArgumentError, "#{klass.name}.#{method} expects #{types.map( &:to_s).join '|' } for #{argument_name}, not: #{object.inspect}"
	end
end

.gen_aliases(cmd, min = nil) ⇒ Object Also known as: g

Generates a list of aliases for given ‘cmd`: `g(:abc)` => `[“a”, “ab”, “abc”]` `g(:abcdef, 4)` => `[“abcd”, “abcde”, “abcdef”]`



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dencli.rb', line 53

def gen_aliases cmd, min = nil
	case min
	when false then min = cmd.length
	when nil then min = 1
	end
	r = ([min, 1].max - 1).upto cmd.length-2
	if block_given?
		r.each {|i| yield cmd[0..i] }
		yield cmd
	else
		r.map  {|i| cmd[0..i] } + [cmd]
	end
end

.n(s, min = nil) ⇒ Object

Helper Function for generate Regular Expressions of string, which matches all strings which has parts fron beginning of the given string. ‘n(“abc”)` would produce: `/(?:a|ab|abc)/` You can define a minimum length to match: `n(“abcdef”,4)` => `/abcd(?:|e|ef)/`



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dencli.rb', line 27

def n s, min = nil
	min ||= 1
	/#{s.length <= min ?
		Regexp.quote(s) :
		"#{Regexp.quote s[0...min]}#{
			s[min...-1].
				reverse.
				each_char.
				reduce( "#{Regexp.quote s[-1]}?") {|f,n|
					"(?:#{Regexp.quote n}#{f})?"
			}
		}"
	}/
end

.r(s, min = nil) ⇒ Object

Wraps ‘n(s,min=)` in a full matching RegExp with ending `0`: `r(“abc”)` would produce: `/A(?:a|ab|abc)0z/` You can define a minimum length to match: `r(“abcdef”,4)` => `/aabcd(?:|e|ef)0z/`



46
47
48
# File 'lib/dencli.rb', line 46

def r s, min = nil
	/\A#{n s, min}\0\z/
end

Instance Method Details

#[](k) ⇒ Object



83
# File 'lib/dencli.rb', line 83

def []( k) @subs[k] end

#_full_cmd(post) ⇒ Object



79
80
81
# File 'lib/dencli.rb', line 79

def _full_cmd post
	post
end

#_help_full(output, subs) ⇒ Object



112
113
114
# File 'lib/dencli.rb', line 112

def _help_full output, subs
	Sub._help_commands output, subs.to_enum( :commands)
end

#call(*a) ⇒ Object



94
95
96
# File 'lib/dencli.rb', line 94

def call *a
	@subs.call *a
end

#cmd(*a, **o, &exe) ⇒ Object



90
91
92
# File 'lib/dencli.rb', line 90

def cmd *a, **o, &exe
	@subs.cmd *a, **o, &exe
end

#full_cmdObject



75
76
77
# File 'lib/dencli.rb', line 75

def full_cmd
	[]
end

#has?(k) ⇒ Boolean

Returns:

  • (Boolean)


84
# File 'lib/dencli.rb', line 84

def has?( k) @subs.has? k end

#help(*args, **opts) ⇒ Object



102
103
104
# File 'lib/dencli.rb', line 102

def help *args, **opts
	@subs.help *args, **opts
end

#help_full(*args, output: nil) ⇒ Object



106
107
108
109
110
# File 'lib/dencli.rb', line 106

def help_full *args, output: nil
	output ||= $stdout
	x = @subs.goto *args
	_help_full output, x
end

#interactive(*args, **opts) ⇒ Object



116
117
118
# File 'lib/dencli.rb', line 116

def interactive *args, **opts
	Interactive.new self, *args, **opts
end

#sub(*a, **o, &exe) ⇒ Object



86
87
88
# File 'lib/dencli.rb', line 86

def sub *a, **o, &exe
	@subs.sub *a, **o, &exe
end

#usage(*args, **opts) ⇒ Object



98
99
100
# File 'lib/dencli.rb', line 98

def usage *args, **opts
	@subs.usage *args, **opts
end