Class: Skylab::Face::Command
- Inherits:
-
Object
- Object
- Skylab::Face::Command
show all
- Includes:
- PathPrettifier, Colors, Nodeish
- Defined in:
- lib/myterm/vendor/skylab/face/cli.rb,
lib/myterm/cli.rb
Defined Under Namespace
Modules: Nodeish, TreeDefiner, Treeish
Classes: Namespace
Constant Summary
collapse
- NoUsageRe =
/\A#{Regexp.escape(Skylab::Face::Colors.hi('usage:'))} /
Constants included
from Colors
Skylab::Face::Colors::Esc, Skylab::Face::Colors::Styles
Instance Method Summary
collapse
Methods included from Nodeish
#build_empty_option_parser, #invite, #invocation_string, #parent=, #usage
Methods included from Colors
#bold, #hi, #highlight_header, #ohno, #style, #yelo
Constructor Details
#initialize(name, *rest, &block) ⇒ Command
Returns a new instance of Command.
35
36
37
38
39
40
|
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 35
def initialize name, *rest, &block
rest.any? and
raise ArgumentError.new("too many args for command: #{rest.inspect}")
@parser_definition = block @intern = name.to_sym
end
|
Instance Method Details
#build_option_parser(req) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 46
def build_option_parser req
parser = build_empty_option_parser
ugly, ugly_id = [parser.banner.dup, parser.banner.object_id]
if @parser_definition
args = [parser, req]
@parser_definition.arity > 0 and args = args[0, @parser_definition.arity]
instance_exec(*args, &@parser_definition)
end
if ugly == parser.banner && ugly_id = parser.banner.object_id
parser.banner = usage_string
end
parser
end
|
#for_run(parent, name_as_used) ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 62
def for_run parent, name_as_used
self.parent = parent
@out = @parent.out
@err = @parent.err
@name_as_used = name_as_used
self end
|
#method ⇒ Object
70
71
72
|
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 70
def method
@parent.method method_symbol
end
|
#method_symbol ⇒ Object
74
75
76
|
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 74
def method_symbol
@intern.to_s.downcase.gsub(/[^a-z0-9]+/, '_').intern
end
|
#name ⇒ Object
78
79
80
|
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 78
def name
@intern.to_s
end
|
#parse(argv) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 82
def parse argv
req = { }
class << req
attr_accessor :command
attr_accessor :method_parameters
end
req.method_parameters = argv
req.command = self
begin
build_option_parser(req).parse! argv
req
rescue OptionParser::ParseError => e
@out.puts (e.to_s)
invite
nil
end
end
|
#summary ⇒ Object
102
103
104
105
|
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 102
def summary
build_option_parser({}).to_s =~ /\A[\n]*([^\n]*)(\n+[^\n])?/
[ "#{$1}#{ ' [..]' if $2 }".sub(NoUsageRe, '') ]
end
|
#syntax(*args) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 107
def syntax *args
case args.length
when 0; @syntax ||= "#{invocation_string} [opts] [args]"
when 1; @syntax = args.first
else raise ArgumentError.new("expecting 0 or 1 argument")
end
end
|
#usage_string ⇒ Object
115
116
117
|
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 115
def usage_string
"#{hi('usage:')} #{syntax}"
end
|