cli_config 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +68 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/cli_config.gemspec +29 -0
- data/lib/cli_config.rb +35 -0
- data/lib/cli_config/option.rb +51 -0
- data/lib/cli_config/version.rb +3 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 81073fed9f35e6bf2534855ae8553cd1ee279b63
|
4
|
+
data.tar.gz: bb92bb66ccea64ad1c811f87ea43c91201481f65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 10a4399ac9253c393ee1a982ccd4d59d49fa20ce9dc509968b0406135fa24d19184f9115b1e6ac4e016f7c3db1f1fe7c365b2a6cbc6b2d03ab60316d05439902
|
7
|
+
data.tar.gz: 9bc5406afa807a95dbee5ecdfdb3bfcd70105e1703ab44dd64fc05c7526a3043d6e04678ee3789be54acd8afd6aa03370f716e88412af6b772287ad4e9d2bb65
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# CLIConfig
|
2
|
+
|
3
|
+
## A tiny gem for handling configs for command line apps
|
4
|
+
|
5
|
+
CLIConfig provides a dsl heavily based on solnic/virtus for specifying options
|
6
|
+
that will be filled from yam or command line options via OptionParser.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'cli_config'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install cli_config
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class RPGConfig
|
28
|
+
include CLIConfig
|
29
|
+
|
30
|
+
option :name, String, desc: 'your name'
|
31
|
+
option :race, Symbol, desc: 'your race (%{default})', default: :human
|
32
|
+
option :strength, Integer, desc: 'your strength (%{default})', default: 33
|
33
|
+
option :stamina, Integer, desc: 'your stamina (%{default})', default: 33, key: :x
|
34
|
+
option :intelligence, Integer, desc: 'your intelligence (%{default})', default: 34
|
35
|
+
end
|
36
|
+
|
37
|
+
config = RPGConfig.new(name: 'test')
|
38
|
+
|
39
|
+
# Or read from a yaml file
|
40
|
+
# config = RPGConfig.load('/some/config.yml')
|
41
|
+
|
42
|
+
parser = OptionParser.new
|
43
|
+
parser.banner = "Usage: #{$0} [options]"
|
44
|
+
config.override_from_option_parser(parser)
|
45
|
+
|
46
|
+
puts parser
|
47
|
+
```
|
48
|
+
|
49
|
+
Output:
|
50
|
+
|
51
|
+
```
|
52
|
+
Usage: rpg [options]
|
53
|
+
-n, --name VALUE your name
|
54
|
+
-r, --race VALUE your race (human)
|
55
|
+
-s, --strength VALUE your strength (33)
|
56
|
+
-x, --stamina VALUE your stamina (33)
|
57
|
+
-i, --intelligence VALUE your intelligence (34)
|
58
|
+
```
|
59
|
+
|
60
|
+
## Development
|
61
|
+
|
62
|
+
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.
|
63
|
+
|
64
|
+
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).
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jreinert/cli_config.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cli_config"
|
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
data/cli_config.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cli_config/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'cli_config'
|
8
|
+
spec.version = CLIConfig::VERSION
|
9
|
+
spec.authors = ['Joakim Reinert']
|
10
|
+
spec.email = ['mail+gems@jreinert.com']
|
11
|
+
spec.license = 'MIT'
|
12
|
+
|
13
|
+
spec.summary = 'A tiny gem for handling configs for command line apps'
|
14
|
+
spec.description = 'Provides dsl heavily based on solnic/virtus for' \
|
15
|
+
'specifying options that will be filled from yaml' \
|
16
|
+
'or command line options via OptionParser'
|
17
|
+
spec.homepage = 'https://github.com/jreinert/cli_config'
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec'
|
27
|
+
spec.add_runtime_dependency 'virtus', '~> 1.0'
|
28
|
+
spec.add_runtime_dependency 'activesupport', '~> 4.2'
|
29
|
+
end
|
data/lib/cli_config.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'virtus'
|
4
|
+
require 'cli_config/version'
|
5
|
+
require 'cli_config/option'
|
6
|
+
|
7
|
+
module CLIConfig
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
include Virtus.model
|
12
|
+
extend ClassMethods
|
13
|
+
include InstanceMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def load(file)
|
18
|
+
new(YAML.load_file(file))
|
19
|
+
end
|
20
|
+
|
21
|
+
def option(name, type, options = {})
|
22
|
+
attribute(name, Option[type], { strict: true }.merge(options))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module InstanceMethods
|
27
|
+
def override_from_option_parser(option_parser)
|
28
|
+
attribute_set.each do |attribute|
|
29
|
+
attribute.from_option_parser(option_parser) do |value|
|
30
|
+
send(:"#{attribute.name}=", value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'active_support/core_ext/object/try'
|
2
|
+
|
3
|
+
module CLIConfig
|
4
|
+
class Option < Virtus::Attribute
|
5
|
+
def self.[](type)
|
6
|
+
Class.new(self).tap do |klass|
|
7
|
+
klass.define_singleton_method(:type) { type }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def coerce(value)
|
12
|
+
Virtus::Attribute.build(
|
13
|
+
self.class.type,
|
14
|
+
strict: options[:strict]
|
15
|
+
).coerce(value)
|
16
|
+
end
|
17
|
+
|
18
|
+
def from_option_parser(parser, &block)
|
19
|
+
if boolean?
|
20
|
+
parser.on("-#{key}", "--[no-]#{long_key}", description, &block)
|
21
|
+
else
|
22
|
+
parser.on("-#{key}", "--#{long_key} #{value_name}", description, &block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def description
|
29
|
+
default = default_value.value
|
30
|
+
description = options[:description] || options[:desc]
|
31
|
+
return unless description
|
32
|
+
description % { default: default }
|
33
|
+
end
|
34
|
+
|
35
|
+
def key
|
36
|
+
options.fetch(:key, name.to_s[0])
|
37
|
+
end
|
38
|
+
|
39
|
+
def long_key
|
40
|
+
name.to_s.tr('_', '-')
|
41
|
+
end
|
42
|
+
|
43
|
+
def value_name
|
44
|
+
options[:value_name].try(:upcase) || 'VALUE'
|
45
|
+
end
|
46
|
+
|
47
|
+
def boolean?
|
48
|
+
self.class.type == Boolean
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cli_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joakim Reinert
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-07 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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: virtus
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.2'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.2'
|
83
|
+
description: Provides dsl heavily based on solnic/virtus forspecifying options that
|
84
|
+
will be filled from yamlor command line options via OptionParser
|
85
|
+
email:
|
86
|
+
- mail+gems@jreinert.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- cli_config.gemspec
|
100
|
+
- lib/cli_config.rb
|
101
|
+
- lib/cli_config/option.rb
|
102
|
+
- lib/cli_config/version.rb
|
103
|
+
homepage: https://github.com/jreinert/cli_config
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.5.1
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: A tiny gem for handling configs for command line apps
|
127
|
+
test_files: []
|
128
|
+
has_rdoc:
|