argh 0.0.1
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.
- data/lib/argh.rb +11 -0
- data/lib/argh/attributable.rb +26 -0
- data/lib/argh/collector.rb +21 -0
- data/lib/argh/formatters.rb +10 -0
- data/lib/argh/formatters/base_formatter.rb +26 -0
- data/lib/argh/formatters/jcommander_formatter.rb +31 -0
- data/lib/argh/version.rb +5 -0
- data/spec/examples/basic_spec.rb +68 -0
- data/spec/examples/jcommander_spec.rb +87 -0
- data/spec/spec_helper.rb +1 -0
- metadata +73 -0
data/lib/argh.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module Argh
|
2
|
+
|
3
|
+
base = File.dirname(File.expand_path(__FILE__))
|
4
|
+
|
5
|
+
autoload :Attributable, base + '/argh/attributable'
|
6
|
+
autoload :Collector, base + '/argh/collector'
|
7
|
+
autoload :Formatters, base + '/argh/formatters'
|
8
|
+
|
9
|
+
autoload :VERSION, base + '/argh/version'
|
10
|
+
|
11
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Argh
|
2
|
+
module Attributable
|
3
|
+
|
4
|
+
DEFAULT_FORMATTER = Formatters::JCommanderFormatter
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def argh(name, formatter_klass = DEFAULT_FORMATTER, &block)
|
13
|
+
collector = Collector.new(&block)
|
14
|
+
mod = Module.new
|
15
|
+
mod.send(:define_method, name) do
|
16
|
+
formatter = formatter_klass.new collector, self
|
17
|
+
formatter.process
|
18
|
+
end
|
19
|
+
# Extend this module onto ourselves
|
20
|
+
self.send(:include, mod)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Argh
|
2
|
+
|
3
|
+
class Collector
|
4
|
+
|
5
|
+
attr_reader :attributes
|
6
|
+
|
7
|
+
def initialize(&block)
|
8
|
+
@attributes = []
|
9
|
+
instance_eval(&block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def attribute(name, &block)
|
13
|
+
@attributes << {
|
14
|
+
:name => name,
|
15
|
+
:lambda => block || lambda { |i| i.send(name) }
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Argh
|
2
|
+
module Formatters
|
3
|
+
|
4
|
+
class BaseFormatter
|
5
|
+
|
6
|
+
def initialize(collector, context)
|
7
|
+
@collector = collector
|
8
|
+
@context = context
|
9
|
+
end
|
10
|
+
|
11
|
+
def attributes(&block)
|
12
|
+
return enum_for(:attributes) unless block_given?
|
13
|
+
@collector.attributes.each do |attr|
|
14
|
+
value = @context.instance_eval(&attr[:lambda])
|
15
|
+
yield attr[:name], value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def process
|
20
|
+
raise 'BaseFormatter should be subclassed'
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Argh
|
2
|
+
module Formatters
|
3
|
+
|
4
|
+
class JCommanderFormatter < BaseFormatter
|
5
|
+
|
6
|
+
def process
|
7
|
+
information = []
|
8
|
+
attributes.each do |name, value|
|
9
|
+
piece = escape(name, value)
|
10
|
+
information << piece unless piece.nil?
|
11
|
+
end
|
12
|
+
information.join ' '
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
# NOTE: Strings need to be escaped when they contain a single-quote,
|
18
|
+
# by breaking the quote, inserting a ' (escaped) and then re-opening
|
19
|
+
def escape(name, value)
|
20
|
+
case value
|
21
|
+
when NilClass, FalseClass then nil
|
22
|
+
when TrueClass then "-#{name}"
|
23
|
+
when Fixnum then "-#{name} #{value}"
|
24
|
+
else "-#{name} '#{value.to_s.gsub(%q{'}) { %q{'\''} }}'"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/lib/argh/version.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
# To facilitate example below
|
4
|
+
class BasicExample
|
5
|
+
|
6
|
+
include Argh::Attributable
|
7
|
+
|
8
|
+
argh 'single' do
|
9
|
+
attribute :name
|
10
|
+
end
|
11
|
+
|
12
|
+
argh 'multiple' do
|
13
|
+
attribute :name
|
14
|
+
attribute :age
|
15
|
+
end
|
16
|
+
|
17
|
+
argh 'multiple_reverse' do
|
18
|
+
attribute :age
|
19
|
+
attribute :name
|
20
|
+
end
|
21
|
+
|
22
|
+
argh 'lambs' do
|
23
|
+
attribute(:name) { name.reverse }
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def name
|
29
|
+
'John Crepezzi'
|
30
|
+
end
|
31
|
+
|
32
|
+
def age
|
33
|
+
26
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
##########
|
39
|
+
|
40
|
+
describe Argh::Attributable do
|
41
|
+
|
42
|
+
let(:example) { BasicExample.new }
|
43
|
+
|
44
|
+
it 'should have a version' do
|
45
|
+
Argh::VERSION.should be_a(String)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'better not define #attribute on example instance' do
|
49
|
+
example.should_not respond_to(:attribute)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should get the value of existing properties' do
|
53
|
+
example.single.should == '-name \'John Crepezzi\''
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should be able to serialize multiple attributes' do
|
57
|
+
example.multiple.should == '-name \'John Crepezzi\' -age 26'
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should be always bring back attributes in order' do
|
61
|
+
example.multiple_reverse.should == '-age 26 -name \'John Crepezzi\''
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should be able to specify a lambda instead of the original' do
|
65
|
+
example.lambs.should == '-name \'izzeperC nhoJ\''
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
# To facilitate examples
|
4
|
+
class JCommanderExample
|
5
|
+
attr_accessor :attr
|
6
|
+
include Argh::Attributable
|
7
|
+
argh 'console_attributes', Argh::Formatters::JCommanderFormatter do
|
8
|
+
attribute :attr
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
##########
|
13
|
+
|
14
|
+
describe Argh::Formatters::JCommanderFormatter do
|
15
|
+
|
16
|
+
let(:attr) { nil }
|
17
|
+
let(:result) do
|
18
|
+
example = JCommanderExample.new
|
19
|
+
example.attr = attr
|
20
|
+
example.console_attributes
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'with a fixnum attribute' do
|
24
|
+
|
25
|
+
let(:attr) { 123 }
|
26
|
+
|
27
|
+
it 'should return the arguments properly' do
|
28
|
+
result.should == '-attr 123'
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'with a true attribute' do
|
34
|
+
|
35
|
+
let(:attr) { true }
|
36
|
+
|
37
|
+
it 'should return only the name of the attribute' do
|
38
|
+
result.should == '-attr'
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'with a false attribute' do
|
44
|
+
|
45
|
+
let(:attr) { false }
|
46
|
+
|
47
|
+
it 'should return an empty string' do
|
48
|
+
result.should == ''
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'with a nil attribute' do
|
54
|
+
|
55
|
+
let(:attr) { nil }
|
56
|
+
|
57
|
+
it 'should return an empty string' do
|
58
|
+
result.should == ''
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'with a string attribute' do
|
64
|
+
|
65
|
+
describe 'a clear string' do
|
66
|
+
|
67
|
+
let(:attr) { 'John Crepezzi' }
|
68
|
+
|
69
|
+
it 'should return the arguments properly' do
|
70
|
+
result.should == %q{-attr 'John Crepezzi'}
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
describe 'a string including single quotes' do
|
76
|
+
|
77
|
+
let(:attr) { 'John \' Crepezzi' }
|
78
|
+
|
79
|
+
it 'should return the arguments properly escaped' do
|
80
|
+
result.should == %q{-attr 'John '\'' Crepezzi'}
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative '../lib/argh'
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: argh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- John Crepezzi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Easy command-line argument building in Ruby
|
31
|
+
email: johnc@broadstreetads.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- lib/argh/attributable.rb
|
37
|
+
- lib/argh/collector.rb
|
38
|
+
- lib/argh/formatters/base_formatter.rb
|
39
|
+
- lib/argh/formatters/jcommander_formatter.rb
|
40
|
+
- lib/argh/formatters.rb
|
41
|
+
- lib/argh/version.rb
|
42
|
+
- lib/argh.rb
|
43
|
+
- spec/examples/basic_spec.rb
|
44
|
+
- spec/examples/jcommander_spec.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
homepage: https://github.com/broadstreetads/argh
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.24
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Easy command-line arguments
|
70
|
+
test_files:
|
71
|
+
- spec/examples/basic_spec.rb
|
72
|
+
- spec/examples/jcommander_spec.rb
|
73
|
+
- spec/spec_helper.rb
|