dencli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c630a25a72718f5826c05cef883db24a1c86fb15e2a6b5cc7ff8dfaf2ddd4097
4
+ data.tar.gz: 7de6920626c61aa2982680fd0df38d7f1ff229913d816adcccf3446b7243a7e3
5
+ SHA512:
6
+ metadata.gz: df74bf563daf0e6c6ddb1863ce364225f05d5a8814857da4370dd002851d67f9d9cab422061d9e4a7bb7df6c4b2c3956a74f76c9be20ff3ad661ff3d234b5dea
7
+ data.tar.gz: 99e2f4dc73b837f71a4c167e837b3ca88c6176d85e603bdf0cdd5cd1b21551b8e6c37f8ecf2bd7edfd1345cc65581789caded4a8a3e43d1670ef44f79c12e630
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dencli.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dencli (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (12.3.3)
11
+ rspec (3.10.0)
12
+ rspec-core (~> 3.10.0)
13
+ rspec-expectations (~> 3.10.0)
14
+ rspec-mocks (~> 3.10.0)
15
+ rspec-core (3.10.0)
16
+ rspec-support (~> 3.10.0)
17
+ rspec-expectations (3.10.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.10.0)
20
+ rspec-mocks (3.10.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.10.0)
23
+ rspec-support (3.10.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ dencli!
30
+ rake (~> 12.0)
31
+ rspec (~> 3.2)
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,63 @@
1
+ # DenCli
2
+
3
+ Provides a Command-Line-Interface-API for providing commands.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ [source,ruby]
10
+ ----
11
+ gem 'dencli'
12
+ ----
13
+
14
+ And then execute:
15
+
16
+ [source,sh]
17
+ ----
18
+ $ bundle install
19
+ ----
20
+
21
+ Or install it yourself as:
22
+
23
+ [source,sh]
24
+ ----
25
+ $ gem install dencli
26
+ ----
27
+
28
+ ## Usage
29
+
30
+ [source,ruby]
31
+ ----
32
+ #!/usr/bin/env ruby
33
+
34
+ require 'dencli'
35
+
36
+ cli = CLI.new "This is an example for generate a CLI-API"
37
+ cli.cmd( :example, "I have an example command") { STDERR.puts "This is an example" }
38
+ cli.cmd( :help) {|*args| STDERR.puts cli.help(*args) }
39
+
40
+ cli.sub( :more, "Sub-Commands are also possible with a new cli") do |sub|
41
+ sub.cmd( :help) {|*args| STDERR.puts cli.help( 'more', *args) }
42
+ sub.cmd( :example, "Here is an example, too") { STDERR.puts "This is an other example" }
43
+ sub.cmd( :foo, "BAR") { STDERR.puts "FOO bar"}
44
+
45
+ sub.sub( :deeper, "You want to have Sub-Sub-Commands?") do |sub2|
46
+ sub2.cmd( :help) {|*args| STDERR.puts cli.help( 'more', 'deeper', *args) }
47
+ sub2.cmd( :last, "The last example") { STDERR.puts "The last example" }
48
+
49
+ sub2.sub( :'sub-commands', "Endless Sub-Sub- ...") do |sub3|
50
+ sub2.cmd( :help) {|*args| STDERR.puts cli.help( 'more', 'deeper', 'sub-commands', *args) }
51
+ sub3.cmd( :hehe, "The real last example", min: 2) { STDERR.puts "Trust me!" }
52
+ end
53
+ end
54
+ end
55
+
56
+ cli.call *ARGV
57
+ ----
58
+
59
+ ## Development
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on git at https://git.denkn.at/deac/dencli.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'dencli'
4
+
5
+ cli = DenCli.new 'example', "This is an example for generate a DenCli-API"
6
+ cli.cmd( :example, "I have an example command") { STDERR.puts "This is an example" }
7
+ cli.cmd( :help, "", aliases: [nil, '-h', '--help']) {|*args| STDERR.puts cli.help(*args) }
8
+
9
+ cli.sub( :more, "Sub-Commands are also possible with a new cli") do |sub|
10
+ sub.cmd( :help, "") {|*args| STDERR.puts cli.help( 'more', *args) }
11
+ sub.cmd( :example, "Here is an example, too") { STDERR.puts "This is an other example" }
12
+ sub.cmd( :foo, "BAR") { STDERR.puts "FOO bar"}
13
+
14
+ sub.sub( :deeper, "You want to have Sub-Sub-Commands?") do |sub2|
15
+ sub2.cmd( :help, "") {|*args| STDERR.puts cli.help( 'more', 'deeper', *args) }
16
+ sub2.cmd( :last, "The last example") { STDERR.puts "The last example" }
17
+
18
+ sub2.sub( :'sub-commands', "Endless Sub-Sub- ...") do |sub3|
19
+ sub2.cmd( :help, "") {|*args| STDERR.puts cli.help( 'more', 'deeper', 'sub-commands', *args) }
20
+ sub3.cmd( :hehe, "The real last example", min: 2) { STDERR.puts "Trust me!" }
21
+ end
22
+ end
23
+ end
24
+
25
+ cli.call *ARGV
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/dencli/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "dencli"
5
+ spec.version = DenCli::VERSION
6
+ spec.authors = ["Denis Knauf"]
7
+ spec.email = ["git+dencli@denkn.at"]
8
+ spec.licenses = ["LGPL-3.0"]
9
+
10
+ spec.summary = %q{Commands and Subcommands}
11
+ spec.description = %q{Embedded commands for Command-Line-Interfaces}
12
+ spec.homepage = "https://git.denkn.at/deac/dencli"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = spec.homepage
20
+
21
+ spec.add_development_dependency "rspec", "~> 3.2"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "bin"
29
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+ end
@@ -0,0 +1,183 @@
1
+ class DenCli
2
+ class UsageError < ::RuntimeError
3
+ end
4
+ class UnknownCommand < UsageError
5
+ end
6
+
7
+ class <<self
8
+ # Helper Function for generate Regular Expressions of string,
9
+ # which matches all strings which has parts fron beginning of the given string.
10
+ # `n("abc")` would produce: `/(?:a|ab|abc)/`
11
+ # You can define a minimum length to match:
12
+ # `n("abcdef",4)` => `/abcd(?:|e|ef)/`
13
+ def n s, min = nil
14
+ min ||= 1
15
+ /#{s.length <= min ?
16
+ Regexp.quote(s) :
17
+ "#{Regexp.quote s[0...min]}#{
18
+ s[min...-1].
19
+ reverse.
20
+ each_char.
21
+ reduce( "#{Regexp.quote s[-1]}?") {|f,n|
22
+ "(?:#{Regexp.quote n}#{f})?"
23
+ }
24
+ }"
25
+ }/
26
+ end
27
+
28
+ # Wraps `n(s,min=)` in a full matching RegExp with ending `\0`:
29
+ # `r("abc")` would produce: `/\A(?:a|ab|abc)\0\z/`
30
+ # You can define a minimum length to match:
31
+ # `r("abcdef",4)` => `/\aabcd(?:|e|ef)\0\z/`
32
+ def r s, min = nil
33
+ /\A#{n s, min}\0\z/
34
+ end
35
+
36
+ # Generates a list of aliases for given `cmd`:
37
+ # `g(:abc)` => `["a", "ab", "abc"]`
38
+ # `g(:abcdef, 4)` => `["abcd", "abcde", "abcdef"]`
39
+ def gen_aliases cmd, min = nil
40
+ r = ((min||1)-1).upto cmd.length-1
41
+ if block_given?
42
+ r.each {|i| yield cmd[0..i] }
43
+ else
44
+ r.map {|i| cmd[0..i] }
45
+ end
46
+ end
47
+ alias g gen_aliases
48
+ end
49
+
50
+ class CMD
51
+ attr_reader :parent, :name, :desc, :exe
52
+ def initialize parent, name, desc, exe
53
+ raise "Proc expected, instead of: #{exe.inspect}" unless Proc === exe
54
+ @parent, @name, @desc, @exe = parent, name, desc, exe
55
+ end
56
+
57
+ def _full_cmd post
58
+ parent._full_cmd [@name]+post
59
+ end
60
+
61
+ def full_cmd
62
+ _full_cmd []
63
+ end
64
+
65
+ def call *a
66
+ @exe.call *a
67
+ end
68
+
69
+ def help
70
+ "#{parent.full_cmd.join ' '} #{name}\n#{ desc}"
71
+ end
72
+
73
+ def inspect
74
+ "#<%s:0x%x %s @name=%p @desc=%p @parent=<%s:0x%x %s> @exe=<arity=%d>>" % [
75
+ self.class.name, self.object_id, self.full_cmd,
76
+ @name, @desc, @parent.class.name, @parent.class.object_id, @parent.full_cmd,
77
+ @exe.arity
78
+ ]
79
+ end
80
+ end
81
+
82
+ class Sub
83
+ attr_reader :parent, :name, :desc, :subs
84
+ def initialize parent, name, desc
85
+ @parent, @name, @desc, @subs, @aliases = parent, name, desc, {}, {}
86
+ end
87
+
88
+ def _full_cmd post
89
+ parent._full_cmd [@name]+post
90
+ end
91
+
92
+ def full_cmd
93
+ _full_cmd []
94
+ end
95
+
96
+ def [] k
97
+ @aliases[k]
98
+ end
99
+
100
+ def help n = nil, *a
101
+ if n.nil?
102
+ r = "#{full_cmd.join ' '}: #{desc}\n\n"
103
+ m = @subs.map {|k,_| k.length }.max
104
+ @subs.each do |k, c|
105
+ r += " % -#{m}s %s\n" % [k, c.desc] unless k.nil?
106
+ end
107
+ r
108
+ elsif @aliases.has_key? n
109
+ @aliases[n].help *a
110
+ else
111
+ raise UnknownCommand, "unknown command: #{_full_cmd( [n])[1..-1].join ' '}, available for #{full_cmd[1..-1].join' '}: #{@subs.keys.inspect}"
112
+ end
113
+ end
114
+
115
+ def call *a
116
+ n, *a = *a
117
+ if @aliases.has_key? n
118
+ @aliases[n].call *a
119
+ else
120
+ raise UnknownCommand, "unknown command: #{_full_cmd( [n])[1..-1].join ' '}, available for #{full_cmd[1..-1].join' '}: #{@subs.keys.inspect}"
121
+ end
122
+ end
123
+
124
+ def _add name, min, obj, aliases
125
+ name = name.to_s unless name.nil?
126
+ @subs[name] = obj
127
+ DenCli.gen_aliases( name, min) {|a| @aliases[a] = obj }
128
+ if aliases
129
+ [*aliases].each {|a| @aliases[a] = obj }
130
+ end
131
+ obj
132
+ end
133
+ private :_add
134
+
135
+ def sub name, desc, min: nil, aliases: nil, &exe
136
+ r = _add name, min, Sub.new( self, name, desc), aliases
137
+ block_given? ? yield( r) : r
138
+ end
139
+
140
+ def cmd name, desc, min: nil, aliases: nil, &exe
141
+ _add name, min, CMD.new( self, name, desc, exe), aliases
142
+ end
143
+
144
+ def inspect
145
+ "#<%s:0x%x %s @name=%p @desc=%p @subs={%s} @aliases={%s} @parent=<%s:0x%x %s>>" % [
146
+ self.class.name, self.object_id, self.full_cmd,
147
+ @name, @desc, @subs.keys.join(', '), @aliases.keys.join(', '), @parent.class.name, @parent.class.object_id, @parent.full_cmd
148
+ ]
149
+ end
150
+ end
151
+
152
+ def initialize progname, desc
153
+ @subs = Sub.new self, progname, desc
154
+ end
155
+
156
+ def full_cmd
157
+ []
158
+ end
159
+
160
+ def _full_cmd post
161
+ post
162
+ end
163
+
164
+ def sub *a, &exe
165
+ @subs.sub *a, &exe
166
+ end
167
+
168
+ def cmd *a, &exe
169
+ @subs.cmd *a, &exe
170
+ end
171
+
172
+ def call *a
173
+ @subs.call *a
174
+ end
175
+
176
+ def help *args
177
+ @subs.help *args
178
+ end
179
+
180
+ def [] k
181
+ @subs[k]
182
+ end
183
+ end
@@ -0,0 +1,3 @@
1
+ class DenCli
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dencli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Denis Knauf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ description: Embedded commands for Command-Line-Interfaces
28
+ email:
29
+ - git+dencli@denkn.at
30
+ executables:
31
+ - example.rb
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.adoc
40
+ - Rakefile
41
+ - bin/example.rb
42
+ - dencli.gemspec
43
+ - lib/dencli.rb
44
+ - lib/dencli/version.rb
45
+ homepage: https://git.denkn.at/deac/dencli
46
+ licenses:
47
+ - LGPL-3.0
48
+ metadata:
49
+ homepage_uri: https://git.denkn.at/deac/dencli
50
+ source_code_uri: https://git.denkn.at/deac/dencli
51
+ changelog_uri: https://git.denkn.at/deac/dencli
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.3.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.7.6.2
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Commands and Subcommands
72
+ test_files: []