clue 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +16 -1
- data/lib/clue.rb +18 -0
- data/lib/clue/arguments.rb +2 -2
- data/lib/clue/options.rb +8 -4
- data/lib/clue/streams.rb +3 -2
- data/lib/clue/version.rb +1 -1
- data/spec/arguments_spec.rb +5 -3
- data/spec/options_spec.rb +18 -7
- data/spec/streams_spec.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52b1b3fba7f900b089e9f1a7555a4dc946218df5
|
4
|
+
data.tar.gz: dac8a07689cb0a35bf9d22ee4b3fbf3e9fdd9618
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f760a74830c914fdea3c57a63674f1664bbd1550d6cf10a806b933dc00dbcfb7d57aecf3d31c39ced1582a07d39f5ce18a07da6e736377bf070bab32835d405e
|
7
|
+
data.tar.gz: 413240c4ef79e0ae264461e83daef3208ecb5acd1c37833400e3531786d872d0d4f2766354a2a3b366cf896da1fc7894a3fb12f9c37f13c290d5862fb60120fe
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -6,6 +6,8 @@ The use-case in mind when creating this gem was rake tasks/capistrano recipes in
|
|
6
6
|
|
7
7
|
## Prerequisites
|
8
8
|
|
9
|
+
[![Build Status](https://travis-ci.org/novelys/clue.png?branch=master)](https://travis-ci.org/novelys/clue)
|
10
|
+
|
9
11
|
This gem depends only on activesupport. It is tested agains many rubies: 1.8.7, 1.9.3, 2.0.0, jruby-18, jruby-19, rubinius-18, rubinius-19, thanks to travis-ci.
|
10
12
|
|
11
13
|
While it is tested against Ruby 1.8, we do not garanty support in future releases, as it is approaching end of life.
|
@@ -35,11 +37,23 @@ clue.call
|
|
35
37
|
The command name can be supplied when initializing the object, as done above, or at a later point, by setting `command` to some value.
|
36
38
|
Calling `call` will raise an exception if `command` is not present.
|
37
39
|
|
40
|
+
### Config
|
41
|
+
|
42
|
+
Not all command line tools expects the same format of options, value, inputs; you can configure those when initialiazing the object, or at a later point using the `config` hash. It recognizes :
|
43
|
+
|
44
|
+
* `between_arguments`: defaults to `' '`. Used between arguments list
|
45
|
+
* `between_options`: defaults to `' '`. Used between options list.
|
46
|
+
* `between_streams`: defaults to `' '`. Used between streams list.
|
47
|
+
* `option_value_separator`: defaults to `' '`. Used between an option and its value. Will usually be `' '` or `=`.
|
48
|
+
* `option_prefix`: defaults to `'--'`. Prepended to an option. Will usually be `-`, or `--`.
|
49
|
+
* `option_delimiter`: defaults to `"'"`. Surrounds an option value, in order to prevent escaping issues.
|
50
|
+
* `stream_delimiter`: defaults to `"'"`. Surrounds a stream, in order to prevent escaping issues.
|
51
|
+
|
38
52
|
### Options
|
39
53
|
|
40
54
|
`Clue` accepts options when initializing, as in `clue = Clue.new(options: [:first, :second, {third: :value}])`.
|
41
55
|
|
42
|
-
You must supply "singular" (options without value) first, and "valued" options as a trailing hash, as shown above.
|
56
|
+
You must supply "singular" (options without value) first, and "valued" options as a trailing hash, as shown above.
|
43
57
|
You can add options at a later point use `add_singular_option`, `add_singular_options`, `add_option`, and `add_options`.
|
44
58
|
|
45
59
|
### Arguments
|
@@ -76,4 +90,5 @@ There is no mecanisme built-in for validating the presence/format of arguments,
|
|
76
90
|
|
77
91
|
## Changelog
|
78
92
|
|
93
|
+
* `1.1.0`: Allow configuration of cli command string defaults.
|
79
94
|
* `1.0.0`: First version.
|
data/lib/clue.rb
CHANGED
@@ -12,6 +12,8 @@ class Clue
|
|
12
12
|
include Options
|
13
13
|
include Streams
|
14
14
|
|
15
|
+
attr_accessor :config
|
16
|
+
|
15
17
|
# Store command name, parse & store args
|
16
18
|
def initialize(*args)
|
17
19
|
hsh = args.last.kind_of?(Hash) && args.pop || {}
|
@@ -23,10 +25,26 @@ class Clue
|
|
23
25
|
self.verbose = hsh.delete(:verbose)
|
24
26
|
self.dry_run = hsh.delete(:dry_run)
|
25
27
|
|
28
|
+
init_config hsh.delete(:config)
|
26
29
|
init_options hsh[:options]
|
27
30
|
init_arguments hsh[:arguments]
|
28
31
|
init_streams hsh
|
29
32
|
|
30
33
|
after_initialize(command, hsh) if self.respond_to?(:after_initialize)
|
31
34
|
end
|
35
|
+
|
36
|
+
# Handles config
|
37
|
+
def init_config(config = {})
|
38
|
+
@config = {
|
39
|
+
:between_arguments => ' ',
|
40
|
+
:between_options => ' ',
|
41
|
+
:between_streams => ' ',
|
42
|
+
:option_value_separator => ' ',
|
43
|
+
:option_prefix => '--',
|
44
|
+
:option_delimiter => "'",
|
45
|
+
:stream_delimiter => "'"
|
46
|
+
}
|
47
|
+
|
48
|
+
@config.merge!(config) if config
|
49
|
+
end
|
32
50
|
end
|
data/lib/clue/arguments.rb
CHANGED
@@ -16,7 +16,7 @@ module Clue::Arguments
|
|
16
16
|
value = [value]
|
17
17
|
end
|
18
18
|
|
19
|
-
@arguments = value
|
19
|
+
@arguments = value
|
20
20
|
end
|
21
21
|
|
22
22
|
# Add one argument
|
@@ -36,6 +36,6 @@ module Clue::Arguments
|
|
36
36
|
|
37
37
|
# Returns a string or arguments suitable for cli use
|
38
38
|
def cli_arguments
|
39
|
-
@arguments.join
|
39
|
+
@arguments.join self.config[:between_arguments]
|
40
40
|
end
|
41
41
|
end
|
data/lib/clue/options.rb
CHANGED
@@ -17,7 +17,7 @@ module Clue::Options
|
|
17
17
|
@options = options
|
18
18
|
elsif options.kind_of?(Array)
|
19
19
|
@singular_options = options
|
20
|
-
@options = @singular_options.pop if @singular_options.last.kind_of?(Hash)
|
20
|
+
@options = @singular_options.pop if @singular_options.last.kind_of?(Hash)
|
21
21
|
elsif options.kind_of?(String) || options.kind_of?(Symbol)
|
22
22
|
@singular_options = [options.to_sym]
|
23
23
|
end
|
@@ -50,19 +50,23 @@ module Clue::Options
|
|
50
50
|
|
51
51
|
# Returns the options string
|
52
52
|
def cli_options
|
53
|
-
options_as_array.join
|
53
|
+
options_as_array.join self.config[:between_options]
|
54
54
|
end
|
55
55
|
|
56
56
|
# Returns options as an array
|
57
57
|
def options_as_array
|
58
58
|
# Valued options
|
59
59
|
ary = @options.each_with_object([]) do |(key, value), memo|
|
60
|
-
|
60
|
+
prefix = self.config[:option_prefix]
|
61
|
+
separator = self.config[:option_value_separator]
|
62
|
+
delimiter = self.config[:option_delimiter]
|
63
|
+
memo << "#{prefix}#{key.to_s}#{separator}#{delimiter}#{value.to_s}#{delimiter}"
|
61
64
|
end
|
62
65
|
|
63
66
|
# Singular options
|
64
67
|
ary = @singular_options.each_with_object(ary) do |opt, memo|
|
65
|
-
|
68
|
+
prefix = self.config[:option_prefix]
|
69
|
+
memo << "#{prefix}#{opt.to_s}"
|
66
70
|
end
|
67
71
|
|
68
72
|
ary
|
data/lib/clue/streams.rb
CHANGED
@@ -32,9 +32,10 @@ module Clue::Streams
|
|
32
32
|
def cli_streams
|
33
33
|
ary = STREAMS_SYMBOLS.each_with_object([]) do |(key, value), memo|
|
34
34
|
ivar = instance_variable_get "@#{key}"
|
35
|
-
|
35
|
+
delimiter = self.config[:stream_delimiter]
|
36
|
+
memo << "#{value} #{delimiter}#{ivar}#{delimiter}" if ivar
|
36
37
|
end
|
37
38
|
|
38
|
-
ary.join
|
39
|
+
ary.join self.config[:between_streams]
|
39
40
|
end
|
40
41
|
end
|
data/lib/clue/version.rb
CHANGED
data/spec/arguments_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'clue'
|
2
2
|
|
3
3
|
describe "Arguments" do
|
4
|
-
subject { Clue.new 'commandname'}
|
4
|
+
subject { Clue.new 'commandname', :config => {:between_arguments => "FOO"}}
|
5
5
|
let(:arguments) { subject.instance_variable_get :@arguments }
|
6
6
|
|
7
7
|
it 'should add one argument' do
|
@@ -32,7 +32,7 @@ describe "Arguments" do
|
|
32
32
|
|
33
33
|
it 'can merge arguments into a string' do
|
34
34
|
subject.add_arguments %w(john jim jones)
|
35
|
-
|
35
|
+
|
36
36
|
expect(subject.cli_arguments).to include "john"
|
37
37
|
expect(subject.cli_arguments).to include "jim"
|
38
38
|
expect(subject.cli_arguments).to include "jones"
|
@@ -53,8 +53,10 @@ describe "Arguments" do
|
|
53
53
|
|
54
54
|
it 'can merge arguments into a string' do
|
55
55
|
subject.add_arguments %w(john jim)
|
56
|
-
|
56
|
+
sep = subject.config[:between_arguments]
|
57
|
+
|
57
58
|
expect(subject.cli_arguments).to include "john"
|
58
59
|
expect(subject.cli_arguments).to include "jim"
|
60
|
+
expect(subject.cli_arguments).to eq "john#{sep}jim"
|
59
61
|
end
|
60
62
|
end
|
data/spec/options_spec.rb
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
require 'clue'
|
2
2
|
|
3
3
|
describe "Options" do
|
4
|
-
subject
|
4
|
+
subject do
|
5
|
+
Clue.new 'commandname', :config => {
|
6
|
+
:option_prefix => '_',
|
7
|
+
:option_value_separator => '==',
|
8
|
+
:option_delimiter => '"""'
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
5
12
|
let(:options) { subject.instance_variable_get :@options }
|
6
13
|
let(:singular) { subject.instance_variable_get :@singular_options }
|
7
14
|
|
@@ -52,12 +59,16 @@ describe "Options" do
|
|
52
59
|
it 'can merge options into a string' do
|
53
60
|
subject.add_options :key1 => 'value1', :key2 => 'value2'
|
54
61
|
subject.add_singular_options %w(john jim jones)
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
expect(subject.cli_options).to include "
|
62
|
+
|
63
|
+
prefix = subject.config[:option_prefix]
|
64
|
+
sep = subject.config[:option_value_separator]
|
65
|
+
delim = subject.config[:option_delimiter]
|
66
|
+
|
67
|
+
expect(subject.cli_options).to include "#{prefix}key1#{sep}#{delim}value1#{delim}"
|
68
|
+
expect(subject.cli_options).to include "#{prefix}key2#{sep}#{delim}value2#{delim}"
|
69
|
+
expect(subject.cli_options).to include "#{prefix}john"
|
70
|
+
expect(subject.cli_options).to include "#{prefix}jim"
|
71
|
+
expect(subject.cli_options).to include "#{prefix}jones"
|
61
72
|
end
|
62
73
|
|
63
74
|
it 'should recognize options given during initialization' do
|
data/spec/streams_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'clue'
|
2
2
|
|
3
3
|
describe "Streams" do
|
4
|
-
subject { Clue.new 'commandname'}
|
4
|
+
subject { Clue.new 'commandname', :config => {:stream_delimiter => '<>'}}
|
5
5
|
|
6
6
|
let(:stdin) { subject.stdin }
|
7
7
|
let(:stdout) { subject.stdout }
|
@@ -38,9 +38,9 @@ describe "Streams" do
|
|
38
38
|
|
39
39
|
it 'can merge streams into a string' do
|
40
40
|
subject.set_streams :stdin => 'value', :stdout => 'value2', :stderr => 'val3'
|
41
|
-
|
42
|
-
expect(subject.cli_streams).to include ">
|
43
|
-
expect(subject.cli_streams).to include "<
|
44
|
-
expect(subject.cli_streams).to include "2>
|
41
|
+
delim = subject.config[:stream_delimiter]
|
42
|
+
expect(subject.cli_streams).to include "> #{delim}value2#{delim}"
|
43
|
+
expect(subject.cli_streams).to include "< #{delim}value#{delim}"
|
44
|
+
expect(subject.cli_streams).to include "2> #{delim}val3#{delim}"
|
45
45
|
end
|
46
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Soltysiak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-04-
|
11
|
+
date: 2013-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|