cmdlib 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0aecf89b44a2d47b9e8132bf782e82be0deda784
4
+ data.tar.gz: 0c8751d793463751009fbcc73cce8e96ffdee92c
5
+ SHA512:
6
+ metadata.gz: 78eefd3639d1f23b07abf9e5de8c02fff310b57a9e4fc641fc21f47af180cb7ac2357c970d08cdf31c6bab6a885ba03aeb158e6221f7269230a96d9248712ca2
7
+ data.tar.gz: d771b0f907d233857eae8890dd69ef54ddeeac9b93068cd9cb057d5d5612e7deb901f5da86f637490e930c58dd8d635e3bb12159c45c3995f0464d7807631561
@@ -0,0 +1,21 @@
1
+ module Cmdlib
2
+ # Class for create command object.
3
+ class Command
4
+
5
+ # Contain object with describe text.
6
+ attr_accessor :describe
7
+
8
+ # Numbers of required arguments.
9
+ attr_accessor :argnum
10
+
11
+ def initialize
12
+ @describe = Describe.new
13
+ @argnum = nil
14
+ end
15
+
16
+ def handler
17
+ puts "fatal error: do not set handler for '#{@describe.oname}' command."
18
+ end
19
+ end # class Command
20
+
21
+ end # module Cmdlib
@@ -0,0 +1,63 @@
1
+ module Cmdlib
2
+ # Create class with describe information
3
+ # of any objects.
4
+ class Describe
5
+
6
+ # This string have should text with object name.
7
+ attr_accessor :oname
8
+
9
+ # This string have should text with brief information.
10
+ attr_accessor :brief
11
+
12
+ # This array string have should text with details information.
13
+ attr_accessor :details
14
+
15
+ # This array string have should text with examples information.
16
+ attr_accessor :example
17
+
18
+ # This array string have should text with tags information.
19
+ attr_accessor :options
20
+
21
+ def initialize
22
+ @oname = ''
23
+ @brief = ''
24
+ @details = []
25
+ @example = []
26
+ @options = []
27
+ end
28
+
29
+ # Display title in follow format: *** [title] ***.
30
+ def self.outtitle ( str )
31
+ if( str.length < (80-4) ) then
32
+ borderlen = 80 - str.length - 4
33
+ print '*' * (borderlen/2)
34
+ print '[ '
35
+ print str
36
+ print ' ]'
37
+ puts '*' * (borderlen/2)
38
+ else
39
+ puts str
40
+ end
41
+ end
42
+
43
+ # Display all information about object.
44
+ def display
45
+ puts
46
+ Describe.outtitle( @oname )
47
+ puts
48
+ puts ' ' + @brief
49
+ puts
50
+ puts '** DESCRIBE:'
51
+ @details.each do |line|
52
+ puts ' ' + line
53
+ end
54
+ puts
55
+ puts '** EXAMPLE:'
56
+ @example.each do |line|
57
+ puts ' ' + line
58
+ end
59
+ puts
60
+ end
61
+ end # class Describe
62
+
63
+ end # module Cmdlib
@@ -0,0 +1,205 @@
1
+ module Cmdlib
2
+ # Class with methods for handled commands
3
+ # from CLI application.
4
+ class Handler
5
+ OPTION_PREFIX_SHORT = '-'
6
+ OPTION_PREFIX_LONG = '--'
7
+
8
+ # This array contain information to use application.
9
+ attr_accessor :usage
10
+
11
+ def initialize
12
+ @usage = []
13
+ @cmdlist = []
14
+ @optlist = []
15
+ end
16
+
17
+ # Add CLICommand object to CLIHandler.
18
+ def addcmd ( cmd )
19
+ raise TypeError, 'Incorrectly types for command object.' unless
20
+ cmd.respond_to? :describe and
21
+ cmd.respond_to? :handler and
22
+ cmd.describe.instance_of? Describe
23
+
24
+ @cmdlist << cmd
25
+ end
26
+
27
+ # Add CLICommand object to CLIHandler.
28
+ def addopt ( opt )
29
+ raise TypeError, 'Incorrectly types for option object.' unless
30
+ opt.respond_to? :brief and
31
+ opt.respond_to? :shortname and
32
+ opt.respond_to? :longname and
33
+ opt.respond_to? :value and
34
+ opt.respond_to? :param and
35
+ opt.brief.instance_of? String
36
+
37
+ @optlist << opt
38
+ end
39
+
40
+ # Main handler method, execute command handler.
41
+ def run
42
+ # parsing options.
43
+ optparser
44
+
45
+ # handled position arguments.
46
+ if ARGV.size > 0 then
47
+ if ARGV[0] == 'help' or ARGV[0] == '--help' or ARGV[0] == '-h' then
48
+ # display help specificly command.
49
+ if ARGV.size == 2 then
50
+ @cmdlist.each do |e|
51
+ if ARGV[1] == e.describe.oname then
52
+ e.describe.display
53
+ exit
54
+ end
55
+ end
56
+ puts "fatal error: unknown command with name - #{ARGV[1]}"
57
+ # display usage help.
58
+ else
59
+ puts
60
+ # dislpay usage information.
61
+ @usage.each do |e|
62
+ puts e
63
+ end
64
+ if @cmdlist.size > 0 then
65
+ puts
66
+ puts '** COMMANDS:'
67
+ maxlen = 0
68
+ # dislpay command list.
69
+ @cmdlist.each do |e|
70
+ maxlen = e.describe.oname.length if e.describe.oname.length > maxlen
71
+ end
72
+ @cmdlist.each do |e|
73
+ print ' ' + e.describe.oname
74
+ print "#{' ' * (maxlen - e.describe.oname.length)} -- "
75
+ puts e.describe.brief
76
+ end
77
+ puts
78
+ puts ' For details type: help <command>'
79
+ end
80
+ puts
81
+ # display options list.
82
+ if @optlist.size > 0 then
83
+ puts '** OPTIONS:'
84
+ maxlen = 0
85
+ # make shortname to string with option names.
86
+ listout = []
87
+ @optlist.each do |e|
88
+ optnames = ''
89
+ if e.shortname.length == 0
90
+ optnames += ' '
91
+ else
92
+ optnames += OPTION_PREFIX_SHORT + e.shortname
93
+ end
94
+ optnames += ','
95
+ if e.longname.length != 0
96
+ optnames += OPTION_PREFIX_LONG + e.longname
97
+ end
98
+ listout << optnames
99
+ maxlen = optnames.length if optnames.length > maxlen
100
+ end
101
+ # make longname to string with option names.
102
+ @optlist.each_with_index do |e,i|
103
+ print ' ' + listout[i]
104
+ print "#{' ' * (maxlen - listout[i].length)} -- "
105
+ puts e.brief
106
+ end
107
+ puts
108
+ end
109
+ end
110
+ else
111
+ # handling input command.
112
+ @cmdlist.each do |e|
113
+ if ARGV[0] == e.describe.oname then
114
+ # check arguments numbers.
115
+ if e.argnum != nil then
116
+ if (ARGV.size - 1) != e.argnum then
117
+ puts 'fatal error: wrong arguments for program.'
118
+ exit
119
+ end
120
+ end
121
+ # run handler of command.
122
+ e.handler
123
+ exit
124
+ end
125
+ end
126
+ puts "fatal error: unknown command with name - #{ARGV[0]}"
127
+ end
128
+ # exit, after handled all arguments.
129
+ exit
130
+ end
131
+ end
132
+
133
+ # Check input arguments on equal option syntax.
134
+ # Return option name if success, else return ''.
135
+ def getopt ( opt )
136
+ if opt.length > OPTION_PREFIX_LONG.length then
137
+ return opt[OPTION_PREFIX_LONG.length, opt.length] if opt[0, OPTION_PREFIX_LONG.length] == OPTION_PREFIX_LONG
138
+ end
139
+ if opt.length > OPTION_PREFIX_SHORT.length then
140
+ return opt[OPTION_PREFIX_SHORT.length, opt.length] if opt[0, OPTION_PREFIX_SHORT.length] == OPTION_PREFIX_SHORT
141
+ end
142
+ return ''
143
+ end
144
+
145
+ # parse command line on the option exist.
146
+ def optparser
147
+ options = []
148
+ # search option in option list.
149
+ ARGV.each_with_index do |opt,i|
150
+ r = getopt( opt )
151
+ options << i if r != '' and r != 'h' and r != 'help'
152
+ end
153
+ # handling each option in list.
154
+ options.each do |oi|
155
+ # find option in CLIHandler list.
156
+ @optlist.each do |opt|
157
+ argval = getopt( ARGV[oi] )
158
+ # if option has a parameter.
159
+ if opt.param then
160
+ # if option-value set in next argument.
161
+ if argval == opt.longname or
162
+ argval == opt.shortname then
163
+ # argument is not set.
164
+ if oi.next >= ARGV.size then
165
+ puts "fatal error: unable to find argument for option '#{ARGV[oi]}'."
166
+ exit
167
+ end
168
+ # if next argument option.
169
+ if getopt( ARGV[oi.next] ) != '' then
170
+ puts "fatal error: miss argument for option '#{ARGV[oi]}'."
171
+ exit
172
+ end
173
+ opt.value = ARGV[oi.next]
174
+ options << oi.next
175
+ next
176
+ end
177
+ # if option-value built-in option.
178
+ oname = opt.shortname
179
+ oname = opt.longname if ARGV[oi][0, OPTION_PREFIX_LONG.length] == OPTION_PREFIX_LONG
180
+ if argval[0, oname.length] == oname then
181
+ opt.value = argval[oname.length, argval.length - oname.length]
182
+ # delete assign symbols.
183
+ opt.value = opt.value[1, opt.value.length - 1] if opt.value[0] == '='
184
+ end
185
+ # single (toggle) option.
186
+ else
187
+ if argval == opt.longname or
188
+ argval == opt.shortname then
189
+ opt.value = true
190
+ end
191
+ end
192
+ end
193
+ end
194
+ # delete option from ARGV.
195
+ onamelist = []
196
+ options.each do |oi|
197
+ onamelist << ARGV[oi]
198
+ end
199
+ onamelist.each do |oname|
200
+ ARGV.delete( oname )
201
+ end
202
+ end
203
+ end # class Handler
204
+
205
+ end # module Cmdlib
@@ -0,0 +1,36 @@
1
+ module Cmdlib
2
+ # Class for create option object.
3
+ class Option
4
+
5
+ # Contain text with option short name (String).
6
+ attr_accessor :shortname
7
+
8
+ # Contain text with option long name (String).
9
+ attr_accessor :longname
10
+
11
+ # Contain text with describe option (String).
12
+ attr_accessor :brief
13
+
14
+ # Contain option value.
15
+ attr_accessor :value
16
+
17
+ # Contain parameter tag, the option can be have a parameter.
18
+ attr_accessor :param
19
+
20
+ # Set option shortname (sname) and longname (lname).
21
+ def initialize ( sname, lname, brief = '', param = false )
22
+ raise TypeError, 'Incorrectly types for option constructor.' unless
23
+ sname.instance_of? String and
24
+ lname.instance_of? String and
25
+ brief.instance_of? String
26
+
27
+ @shortname = sname
28
+ @longname = lname
29
+ @brief = brief
30
+ @value = nil
31
+ @param = param
32
+ end
33
+
34
+ end # class Option
35
+
36
+ end # module Cmdlib
@@ -0,0 +1,3 @@
1
+ module Cmdlib
2
+ VERSION = "0.1.1"
3
+ end
data/lib/cmdlib.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "cmdlib/version"
2
+ require "cmdlib/command.rb"
3
+ require "cmdlib/describe.rb"
4
+ require "cmdlib/option.rb"
5
+ require "cmdlib/handler.rb"
6
+
7
+ module Cmdlib
8
+ # Your code goes here...
9
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmdlib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Anton S. Gerasimov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: ''
42
+ email:
43
+ - gera_box@mail.ru
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/cmdlib.rb
49
+ - lib/cmdlib/command.rb
50
+ - lib/cmdlib/describe.rb
51
+ - lib/cmdlib/handler.rb
52
+ - lib/cmdlib/option.rb
53
+ - lib/cmdlib/version.rb
54
+ homepage: https://github.com/gera-gas/cmdlib
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.2.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Simple constructor of CLI (Command Line Interface) handler.
78
+ test_files: []
79
+ has_rdoc: