ordu 0.0.2

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: 45c704d10811be244289b8d3fb027cd6888725bb
4
+ data.tar.gz: 78c7a7d69b655afbf2cd34839d6a22e255c2f6ed
5
+ SHA512:
6
+ metadata.gz: 0b38d6eb9f53b1eb20cbcb70ba9d420db96350f25c2d86f4c712b212d5195969db3c57b9d9c9b52f3973b11466256ed1197b1cc74f7022914a9a20e827ed52b1
7
+ data.tar.gz: 5e1d20bd648b9c895c10fcf2f0a4709c12ecad6424c7b15e4a090357815e5d42ef6bec0ebc133c67ae2e82a3959107244952f3a52b88bfdb177f55e882c43f53
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at jjbuckley@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ordu.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 JJ Buckley
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.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # Ordú
2
+
3
+ Another command-line interface library for Ruby. This one is based entirely on
4
+ the venerable and built-in `OptionParser` class.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'ordu'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install ordu
21
+
22
+ ## Usage
23
+
24
+ Write a subclass of `Ordu`. Then use the `option`, `command` and `action` to
25
+ define behaviour. In the background, a set of command-line parsers will be
26
+ built up. Now, calling the `parse!` method on the class itself, and passing in
27
+ an array of arguments will give you your CLI.
28
+
29
+ Here's a simple example:
30
+
31
+ ```ruby
32
+ #!/usr/bin/env ruby
33
+ require 'ordu'
34
+ class Sneezer < Ordu
35
+ # Define options much as you would with Ruby's built-in OptionParser. The
36
+ # difference is that blocks are evaluated within the Ordu instance's
37
+ # context.
38
+ option('-h', '--help', 'print this message') { puts(self); exit }
39
+ option('-V', '--version', 'print the version') { puts('0.0.1'); exit }
40
+ option('-v', '--[no-]verbose', 'be noisy') { |v| $verbose = v }
41
+
42
+ # Define an action to be run after commands are processed. This is optional.
43
+ action do |*args|
44
+ puts "Leftover args: #{args}" if $verbose
45
+ end
46
+
47
+ # Define one or more subcommands (with a brief description). They will be
48
+ # turned into Ordu parsers, and given the remaining arguments to parse. The
49
+ # DSL within a command is exactly like that of the top-level class.
50
+ command 'sneeze', 'sneeze on the console' do
51
+ option('-h', '--help', 'you are reading it') { puts(self); exit }
52
+ option('-l', '--loud', 'sneeze louder') { @loud = true }
53
+ option('-t', '--times N', Integer, 'sneeze repeatedly') { |n| @times = n }
54
+
55
+ action do |*args|
56
+ (@times || 1).times { puts 'Achoo!'.tap { |s| s.upcase! if @loud } }
57
+ puts "After sneezing: #{args}" if $verbose
58
+ end
59
+ end
60
+ end
61
+
62
+ Sneezer.parse!(ARGV) # Processes the commands
63
+ ```
64
+
65
+ This gives you a useless little tool which sneezed multiple times on demand.
66
+ You can see it in action if you're in this project's directory, and you run
67
+
68
+ examples/sneezer.rb -h
69
+ examples/sneezer.rb sneeze -h
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
74
+ run `rake test` to run the tests. You can also run `bin/console` for an
75
+ interactive prompt that will allow you to experiment.
76
+
77
+ To install this gem onto your local machine, run `bundle exec rake install`.
78
+ To release a new version, update the version number in `ordu.gemspec`, and
79
+ then run `bundle exec rake release`, which will create a git tag for the
80
+ version, push git commits and tags, and push the `.gem` file to
81
+ [rubygems.org](https://rubygems.org).
82
+
83
+ ## Contributing
84
+
85
+ Bug reports and pull requests are welcome on GitHub at
86
+ https://github.com/bjjb/ordu. This project is intended to be a safe, welcoming
87
+ space for collaboration, and contributors are expected to adhere to the
88
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT
93
+ License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ordu"
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
data/bin/setup ADDED
@@ -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
data/lib/ordu.rb ADDED
@@ -0,0 +1,124 @@
1
+ require 'optparse'
2
+ # A little class for building command-line programs. Based on OptionParser.
3
+ #
4
+ # You define a subclass of Ordu with the `option`, `command` and `action`
5
+ # directives. `option` looks a lot like `OptionParser#on`, but can also accept
6
+ # a Symbol or a String (allowing you to specify an instance method as the
7
+ # option handler). `command` takes a block which is used as the body of the
8
+ # command (or, again, a String/Symbol, or an Ordu class). `action` is another
9
+ # block (or Symbol/String) which is executed if no subcommands are run.
10
+ #
11
+ # When the class is defined, these options, commands and action are simply
12
+ # remembered. Then when the class is initialized, the instance is created and
13
+ # the options applied.
14
+ #
15
+ # When `parse!` is called, the command-line switches are parsed. The next
16
+ # leftover argument is checked to see if it is a command. If so, then that
17
+ # command is built into an Ordu, and `parse!`ed with the next arguments.
18
+ # Everything left is passed into the `action` method (which instance_execs the
19
+ # block specified in the class definition).
20
+ class Ordu < OptionParser
21
+ # Make a new Ordu instance. It will be set up with the options, commands and
22
+ # action specified in its class definition.
23
+ def initialize(options: nil, commands: nil, action: nil)
24
+ super()
25
+ options ||= self.class.options
26
+ commands ||= self.class.commands
27
+ action ||= self.class.action
28
+ options.each { |o| option!(*o) }
29
+ commands.each { |c| command!(*c) }
30
+ @action = action
31
+ end
32
+
33
+ # Parse options in order, and call commands for non-option arguments (if the
34
+ # command exists). This is how programs are exected.
35
+ def parse!(args)
36
+ args = order!(args)
37
+ unless args.empty?
38
+ if commands.key?(args.first)
39
+ return commands[args.shift].parse!(args)
40
+ end
41
+ end
42
+ run(*args)
43
+ end
44
+
45
+ # Parses a new instance. Note that this must be the top-level, since you
46
+ # can't supply a name. This is convenient for starting the parser from
47
+ # scripts.
48
+ def self.parse!(args)
49
+ new.parse!(args)
50
+ end
51
+
52
+ private
53
+
54
+ def program_name
55
+ name || super
56
+ end
57
+
58
+ def name
59
+ @name ||= self.class.name
60
+ end
61
+
62
+ def self.name(name = nil)
63
+ name ? @name = name : @name
64
+ end
65
+
66
+ def self.desc(desc = nil)
67
+ desc ? @desc = desc : @desc
68
+ end
69
+
70
+ # Runs the action for this Ordu instance.
71
+ def run(*args)
72
+ return instance_exec(*args, &@action) if @action
73
+ end
74
+
75
+ # Gets the hash of subcommands
76
+ def commands
77
+ @commands ||= {}
78
+ end
79
+
80
+ # Applies a new option (using OptionParser#on). The last argument must be a
81
+ # block, and will be instance_exec'ed in this instance.
82
+ def option!(*args)
83
+ b = args.pop
84
+ on(*args) { |*x| instance_exec(*x, &b) }
85
+ end
86
+
87
+ # Applies a new command (i.e., a new Ordu which may be run if the command is
88
+ # given). name and desc are set on the new class.
89
+ def command!(name, desc, block)
90
+ command = Class.new(self.class, &block)
91
+ command.name [program_name, name].join(' ')
92
+ command.desc desc
93
+ commands[name] = command
94
+ end
95
+
96
+ # Defines a new option for instances of this Ordu class.
97
+ def self.option(*args, &block)
98
+ args.push(block) if block_given?
99
+ options.push(args)
100
+ end
101
+
102
+ # An array of the options to be applied
103
+ def self.options
104
+ @options ||= []
105
+ end
106
+
107
+ # Defines a new command for instances of this Ordu class. The block will be
108
+ # pushed in as the last element of the command array.
109
+ def self.command(*args, &block)
110
+ args.push(block) if block_given?
111
+ commands.push(args)
112
+ end
113
+
114
+ # An array of the commands available to instances of this Ordu class.
115
+ def self.commands
116
+ @commands ||= []
117
+ end
118
+
119
+ # Defines the action to be run once parsing is finished.
120
+ def self.action(&block)
121
+ @action = block if block_given?
122
+ @action ||= lambda { }
123
+ end
124
+ end
data/ordu.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ordu'
7
+ spec.version = '0.0.2'
8
+ spec.authors = ['JJ Buckley']
9
+ spec.email = ['jj@bjjb.org']
10
+
11
+ spec.summary = 'A little CLI library based on OptionParser'
12
+ spec.description = <<-EOF
13
+ A small library to simplify writing command-line interfaces. It has no
14
+ dependencies, and allows you to specify subcommands.
15
+ EOF
16
+ spec.homepage = 'https://bjjb.github.io/ordu'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features|examples)/})
21
+ end
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.12'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'minitest', '~> 5.0'
27
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ordu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - JJ Buckley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-14 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: |
56
+ A small library to simplify writing command-line interfaces. It has no
57
+ dependencies, and allows you to specify subcommands.
58
+ email:
59
+ - jj@bjjb.org
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/ordu.rb
74
+ - ordu.gemspec
75
+ homepage: https://bjjb.github.io/ordu
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.5.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: A little CLI library based on OptionParser
99
+ test_files: []