mcli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8525a628284d643fb3483ac0ecd460520968aa74
4
+ data.tar.gz: 6fb3670630b6ba42e2a8c39d2b16c89677fea286
5
+ SHA512:
6
+ metadata.gz: 33d8b44f87b79c4a05f5e1e2315ecb006980d4e70391bd8149b6bd57aecf207b057283cd4c6524718f94a289b150b901d4498e869aed2b1ab6c46cade7b8d87e
7
+ data.tar.gz: c1dd2a0e0062c087e8303c38ee39887a59a185572437e94d33b4e6660d9322c281511956542c65e7afe0cef59c9bfdceaa8f924e2ff1e7860851d484a40dcc06
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mcli.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
7
+ gem 'aruba'
8
+ gem 'simplecov',
9
+ git: 'git@github.com:thomas07vt/simplecov.git',
10
+ branch: 'issues/624/refuse_coverage_drop_on_failed_test'
11
+ gem 'codecov', :require => false, :group => :test
12
+
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 John Thomas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,207 @@
1
+ # MCLI
2
+ [![codecov](https://codecov.io/gh/thomas07vt/mcli/branch/master/graph/badge.svg)](https://codecov.io/gh/thomas07vt/mcli)
3
+
4
+ Creating command line tools is a :cake: . This gem is basically a wrapper around the ruby ```OptionParser``` class. It turns your ruby objects into parsers.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'mcli'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install mcli
21
+
22
+ ## Usage
23
+
24
+ 1. Require MCLI
25
+
26
+ ```ruby
27
+ require 'mcli'
28
+ ```
29
+
30
+ 2. Create a ```MCLI::Command``` subclass
31
+
32
+ ```ruby
33
+ class MyCommand < MCLI::Command
34
+ end
35
+ ```
36
+
37
+ 3. Call the Command Runner
38
+
39
+ ```ruby
40
+ MCLI.run
41
+ ```
42
+
43
+ ### Examples
44
+
45
+ #### Arguments
46
+
47
+ ```ruby
48
+ #args.rb
49
+ ```
50
+ ```ruby
51
+ #!/usr/bin/env ruby
52
+ require 'mcli'
53
+
54
+ class Args < MCLI::Command
55
+ register_as :args
56
+
57
+ def run
58
+ puts "The passed args are: #{args.inspect}"
59
+ end
60
+ end
61
+
62
+ MCLI.run
63
+ ```
64
+
65
+ ```bash
66
+ $ ./args.rb args first second
67
+ The passed args are: ["first", "second"]
68
+ ```
69
+
70
+ ##### Options with aliases
71
+
72
+ ```ruby
73
+ #options.rb
74
+ ```
75
+ ```ruby
76
+ #!/usr/bin/env ruby
77
+ require 'mcli'
78
+
79
+ class Options < MCLI::Command
80
+ register_as :options
81
+ option :say, alias: :s
82
+
83
+ def run
84
+ puts "Saying: #{options[:say]}"
85
+ end
86
+ end
87
+
88
+ MCLI.run
89
+ ```
90
+
91
+ ```bash
92
+ $ ./options.rb options --say 'hello'
93
+ Saying: hello
94
+ $ ./options.rb options -s 'hello'
95
+ Saying: hello
96
+ ```
97
+
98
+ ##### Options with defaults
99
+
100
+ ```ruby
101
+ #options.rb
102
+ ```
103
+ ```ruby
104
+ #!/usr/bin/env ruby
105
+ require 'mcli'
106
+
107
+ class Options < MCLI::Command
108
+ register_as :options
109
+ option :say, alias: :s, default: 'hello'
110
+
111
+ def run
112
+ puts "Saying: #{options[:say]}"
113
+ end
114
+ end
115
+
116
+ MCLI.run
117
+ ```
118
+
119
+ ```bash
120
+ $ ./options.rb options --say hi
121
+ Saying: hi
122
+ $ ./options.rb options -s hi
123
+ Saying: hi
124
+ $ ./options.rb options
125
+ Saying: hello
126
+ ```
127
+
128
+ ##### Required Options
129
+
130
+ ```ruby
131
+ #options.rb
132
+ ```
133
+ ```ruby
134
+ #!/usr/bin/env ruby
135
+ require 'mcli'
136
+
137
+ class Options < MCLI::Command
138
+ register_as :options
139
+ option :say, alias: :s, required: true
140
+
141
+ def run
142
+ puts "Saying: #{options[:say]}"
143
+ end
144
+ end
145
+
146
+ MCLI.run
147
+ ```
148
+
149
+ ```bash
150
+ $ ./options.rb options --say hi
151
+ Saying: hi
152
+ $ ./options.rb options -s hi
153
+ Saying: hi
154
+ $ ./options.rb options
155
+ mcli/lib/mcli/command.rb:25:in `block in parse': missing argument: (OptionParser::MissingArgument)
156
+ mcli/lib/mcli/command.rb:23:in `map'
157
+ mcli/lib/mcli/command.rb:23:in `parse'
158
+ mcli/lib/mcli/command.rb:62:in `block in call'
159
+ mcli/lib/mcli/command.rb:60:in `tap'
160
+ mcli/lib/mcli/command.rb:60:in `call'
161
+ mcli/lib/mcli.rb:17:in `run'
162
+ from options.rb `<main>'
163
+
164
+ ```
165
+
166
+ ##### Boolean Options
167
+
168
+ ```ruby
169
+ #options.rb
170
+ ```
171
+ ```ruby
172
+ #!/usr/bin/env ruby
173
+ require 'mcli'
174
+
175
+ class Options < MCLI::Command
176
+ register_as :options
177
+ option :heads, alias: :h, boolean: true
178
+
179
+ def run
180
+ puts "Heads: #{options[:heads]}"
181
+ end
182
+ end
183
+
184
+ MCLI.run
185
+ ```
186
+
187
+ ```bash
188
+ $ ./options.rb options --heads
189
+ Heads: true
190
+ $ ./options.rb options --no-heads
191
+ Heads: false
192
+ ```
193
+
194
+ ## Development
195
+
196
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
197
+
198
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
199
+
200
+ ## Contributing
201
+
202
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thomas07vt/mcli.
203
+
204
+
205
+ ## License
206
+
207
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mcli"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mcli'
4
+
5
+ MCLI.run
6
+
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,20 @@
1
+ require 'optparse'
2
+ require 'mcli/version'
3
+ require 'mcli/help_error'
4
+ require 'mcli/command_group'
5
+ require 'mcli/command'
6
+ require 'mcli/command/option'
7
+ require 'mcli/null_command'
8
+
9
+ module MCLI
10
+ class << self
11
+ def run
12
+ command = ARGV.shift
13
+
14
+ CommandGroup.commands.fetch(command.to_s.to_sym) do |command|
15
+ ARGV.unshift(command)
16
+ NullCommand
17
+ end.call
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,71 @@
1
+ class MCLI::Command
2
+ attr_reader :args
3
+
4
+ def initialize(args=[])
5
+ @args = args
6
+ end
7
+
8
+ def parse
9
+ @options = {}
10
+
11
+ self.class.options.each do |option|
12
+ @options[option.name] = option.default if option.default
13
+
14
+ parser.on(*option.to_args) do |o|
15
+ @options[option.name] = o
16
+ end
17
+ end
18
+
19
+ parser.parse!
20
+
21
+ self.class.options
22
+ .select { |option| option.required == true }
23
+ .map do |required_option|
24
+ if @options[required_option.name].nil?
25
+ raise OptionParser::MissingArgument
26
+ end
27
+ end
28
+ end
29
+
30
+ def options
31
+ @options ||= {}
32
+ end
33
+
34
+ def parser
35
+ @parser ||= create_parser
36
+ end
37
+
38
+ def create_parser
39
+ OptionParser.new.tap do |parser|
40
+ parser.on("-h", "--help", "Help") do
41
+ raise MCLI::HelpError.new
42
+ end
43
+ end
44
+ end
45
+
46
+ class << self
47
+ def option(option_name, opts={})
48
+ options << Option.new(option_name, opts)
49
+ end
50
+
51
+ def options
52
+ @options ||= []
53
+ end
54
+
55
+ def register_as(command_name)
56
+ MCLI::CommandGroup.register(command_name, self)
57
+ end
58
+
59
+ def call
60
+ new(ARGV).tap do |command|
61
+ begin
62
+ command.parse
63
+ command.run
64
+ rescue MCLI::HelpError
65
+ command.help
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+
@@ -0,0 +1,34 @@
1
+ # @private
2
+ class MCLI::Command::Option
3
+ attr_reader :name, :alias, :default, :required, :type, :boolean
4
+
5
+ def initialize(name, options={})
6
+ opts = default_options.merge(options)
7
+
8
+ @name = name.to_sym
9
+ @alias = opts[:alias].to_s if opts[:alias]
10
+ @default = opts[:default]
11
+ @required = opts[:required]
12
+ @type = opts[:type]
13
+ @boolean = opts[:boolean]
14
+ end
15
+
16
+ def to_args
17
+ [].tap do |args|
18
+ args << "-#{@alias}" if @alias
19
+ args << "--#{@name} #{@name.upcase}" if @required && !@boolean
20
+ args << "--#{@name} [#{@name.upcase}]" if !@required && !@boolean
21
+ args << "--[no-]#{@name}" if @boolean
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def default_options
28
+ {
29
+ required: false,
30
+ boolean: false
31
+ }
32
+ end
33
+ end
34
+
@@ -0,0 +1,16 @@
1
+ class MCLI::CommandGroup
2
+ class << self
3
+ def register(command_name, command_klass)
4
+ commands[command_name] = command_klass
5
+ end
6
+
7
+ def commands
8
+ @commands ||= {}
9
+ end
10
+
11
+ def clear
12
+ @commands = {}
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1 @@
1
+ class MCLI::HelpError < StandardError; end
@@ -0,0 +1,19 @@
1
+ class MCLI::NullCommand < MCLI::Command
2
+ option :version, alias: 'v', boolean: true
3
+
4
+ def run
5
+ if options[:version]
6
+ puts MCLI::VERSION
7
+ else
8
+ help
9
+ end
10
+ end
11
+
12
+ def help
13
+ puts help_message
14
+ end
15
+
16
+ def help_message
17
+ "No command passed\n#{parser.to_s}"
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module MCLI
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mcli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mcli"
8
+ spec.version = MCLI::VERSION
9
+ spec.authors = ["John Thomas"]
10
+ spec.email = ["thomas07@vt.edu"]
11
+
12
+ spec.summary = %q{Create CLI tools using ruby objects with a nice API}
13
+ spec.homepage = "https://github.com/thomas07vt/mcli"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "bin"
20
+ spec.executables = spec.files.grep(%r{^bin/mcli}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mcli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Thomas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-11 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - thomas07@vt.edu
58
+ executables:
59
+ - mcli
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/mcli
72
+ - bin/setup
73
+ - lib/mcli.rb
74
+ - lib/mcli/command.rb
75
+ - lib/mcli/command/option.rb
76
+ - lib/mcli/command_group.rb
77
+ - lib/mcli/help_error.rb
78
+ - lib/mcli/null_command.rb
79
+ - lib/mcli/version.rb
80
+ - mcli.gemspec
81
+ homepage: https://github.com/thomas07vt/mcli
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.6.11
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Create CLI tools using ruby objects with a nice API
105
+ test_files: []