simple_scripting 0.9.0 → 0.9.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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59993d0d200b77aa1deb2aadda05560b427cee00
|
4
|
+
data.tar.gz: 1baeb57678e05f4fac32cf8d34afb42b175d5b08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a06e70451f2b78d8833edaa7649c7ac9130920aca97b2cd9ec171ca3db30a370a65fd15a120a05256f29289bc3e66cad8202c8a8e0f4ea3c04c47ce7ba09d0a3
|
7
|
+
data.tar.gz: 52cf1615fa2ca5b6f8c1147176ba3859cccc856b613fba1a178d21185bd826f9005cd1d6d88d6fd602e3d8646c267dab4d2a7321357a3c7fe8563c0869821363
|
data/README.md
CHANGED
@@ -15,6 +15,8 @@
|
|
15
15
|
|
16
16
|
This is a definition example:
|
17
17
|
|
18
|
+
require 'simple_scripting/argv'
|
19
|
+
|
18
20
|
result = SimpleOptParse::Argv.decode(
|
19
21
|
['-s', '--only-scheduled-days', 'Only print scheduled days' ],
|
20
22
|
['-d', '--print-defaults TEMPLATE', 'Print the default activities from the named template'],
|
@@ -61,6 +63,7 @@ Say one writes a script (`foo_my_bar.rb`), with a corresponding (`$HOME/.foo_my_
|
|
61
63
|
|
62
64
|
some_relative_file_path=foo
|
63
65
|
some_absolute_file_path=/path/to/bar
|
66
|
+
multiple_paths=foo:/path/to/bar
|
64
67
|
my_password=uTxllKRD2S+IH92oi30luwu0JIqp7kKA
|
65
68
|
|
66
69
|
[a_group]
|
@@ -68,6 +71,8 @@ Say one writes a script (`foo_my_bar.rb`), with a corresponding (`$HOME/.foo_my_
|
|
68
71
|
|
69
72
|
This is the workflow and functionality offered by `SS::C`:
|
70
73
|
|
74
|
+
require 'simple_scripting/configuration'
|
75
|
+
|
71
76
|
# Picks up automatically the configuration file name, based on the calling program
|
72
77
|
#
|
73
78
|
configuration = SimpleScripting::Configuration.load(passwords_key: 'encryption_key')
|
@@ -75,6 +80,7 @@ This is the workflow and functionality offered by `SS::C`:
|
|
75
80
|
configuration.some_relative_file_path.full_path # '$HOME/foo'
|
76
81
|
configuration.some_absolute_file_path # '/path/to/bar'
|
77
82
|
configuration.some_absolute_file_path.full_path # '/path/to/bar' (recognized as absolute)
|
83
|
+
configuration.multiple_paths.full_paths # ['$HOME/foo', '/path/to/bar']
|
78
84
|
|
79
85
|
configuration.my_password.decrypted # 'encrypted_value'
|
80
86
|
|
@@ -85,3 +91,7 @@ This is the workflow and functionality offered by `SS::C`:
|
|
85
91
|
The purpose of encryption in this library is just to avoid displaying passwords in plaintext; it's not considered safe against attacks.
|
86
92
|
|
87
93
|
[BS img]: https://travis-ci.org/saveriomiroddi/simple_scripting.svg?branch=master
|
94
|
+
|
95
|
+
## Help
|
96
|
+
|
97
|
+
See the [wiki](https://github.com/saveriomiroddi/simple_scripting/wiki) for additional help.
|
@@ -6,7 +6,7 @@ module SimpleScripting
|
|
6
6
|
|
7
7
|
extend self
|
8
8
|
|
9
|
-
def decode(*params_definition, arguments: ARGV, long_help: nil, output: $stdout)
|
9
|
+
def decode(*params_definition, arguments: ARGV, long_help: nil, output: $stdout, command: nil)
|
10
10
|
# If the param is a Hash, we have multiple commands. We check and if the command is correct,
|
11
11
|
# recursively call the function with the specific parameters.
|
12
12
|
#
|
@@ -25,7 +25,7 @@ module SimpleScripting
|
|
25
25
|
print_optparse_commands_help(commands_definition, output, true)
|
26
26
|
output == $stdout ? exit : return
|
27
27
|
else
|
28
|
-
return [command, decode(*command_params_definition, arguments: arguments, output: output)]
|
28
|
+
return [command, decode(*command_params_definition, arguments: arguments, output: output, command: command)]
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -33,8 +33,8 @@ module SimpleScripting
|
|
33
33
|
parser_opts_ref = nil # not available outside the block
|
34
34
|
args = {} # { 'name' => mandatory? }
|
35
35
|
|
36
|
-
OptionParser.new do |
|
37
|
-
params_definition.each do |
|
36
|
+
OptionParser.new do |parser_opts|
|
37
|
+
params_definition.each do |param_definition|
|
38
38
|
case param_definition
|
39
39
|
when Array
|
40
40
|
if param_definition[1] && param_definition[1].start_with?('--')
|
@@ -61,8 +61,8 @@ module SimpleScripting
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
parser_opts.on(
|
65
|
-
print_optparse_help(
|
64
|
+
parser_opts.on('-h', '--help', 'Help') do
|
65
|
+
print_optparse_help(parser_opts, args, long_help, output, command: command)
|
66
66
|
output == $stdout ? exit : return
|
67
67
|
end
|
68
68
|
|
@@ -76,29 +76,29 @@ module SimpleScripting
|
|
76
76
|
# Mandatory?
|
77
77
|
if args.fetch(first_arg_name.to_sym)
|
78
78
|
if arguments.empty?
|
79
|
-
print_optparse_help(
|
79
|
+
print_optparse_help(parser_opts_ref, args, long_help, output, command: command)
|
80
80
|
output == $stdout ? exit : return
|
81
81
|
else
|
82
|
-
name = args.keys.first[
|
82
|
+
name = args.keys.first[1..-1].to_sym
|
83
83
|
|
84
|
-
result[
|
84
|
+
result[name] = arguments
|
85
85
|
end
|
86
86
|
# Optional
|
87
87
|
else
|
88
|
-
name = args.keys.first[
|
88
|
+
name = args.keys.first[1..-1].to_sym
|
89
89
|
|
90
|
-
result[
|
90
|
+
result[name] = arguments
|
91
91
|
end
|
92
92
|
else
|
93
|
-
min_args_size = args.count { |
|
93
|
+
min_args_size = args.count { |name, mandatory| mandatory }
|
94
94
|
|
95
95
|
case arguments.size
|
96
96
|
when (min_args_size .. args.size)
|
97
|
-
arguments.zip(args) do |
|
97
|
+
arguments.zip(args) do |value, (name, mandatory)|
|
98
98
|
result[name] = value
|
99
99
|
end
|
100
100
|
else
|
101
|
-
print_optparse_help(parser_opts_ref, args, long_help, output)
|
101
|
+
print_optparse_help(parser_opts_ref, args, long_help, output, command: command)
|
102
102
|
output == $stdout ? exit : return
|
103
103
|
end
|
104
104
|
end
|
@@ -113,9 +113,17 @@ module SimpleScripting
|
|
113
113
|
output.puts "Valid commands:", "", " " + commands_definition.keys.join(', ')
|
114
114
|
end
|
115
115
|
|
116
|
-
def print_optparse_help(parser_opts, args, long_help, output)
|
117
|
-
|
118
|
-
|
116
|
+
def print_optparse_help(parser_opts, args, long_help, output, command: nil)
|
117
|
+
parser_opts_help = parser_opts.to_s
|
118
|
+
|
119
|
+
if command
|
120
|
+
parser_opts_help = parser_opts_help.sub!(/(\[options\])/, "#{command} \\1")
|
121
|
+
end
|
122
|
+
|
123
|
+
if args.size > 0
|
124
|
+
args_display = args.map { |name, mandatory| mandatory ? "<#{ name }>" : "[<#{ name }>]" }.join(' ')
|
125
|
+
parser_opts_help = parser_opts_help.sub!(/^(Usage: .*)/, "\\1 #{args_display}")
|
126
|
+
end
|
119
127
|
|
120
128
|
output.puts parser_opts_help
|
121
129
|
output.puts "", long_help if long_help
|
@@ -24,6 +24,10 @@ module SimpleScripting
|
|
24
24
|
start_with?('/') ? self : File.expand_path(self, '~')
|
25
25
|
end
|
26
26
|
|
27
|
+
def full_paths
|
28
|
+
split(':').map { |value| self.class.new(value).full_path }
|
29
|
+
end
|
30
|
+
|
27
31
|
def decrypted
|
28
32
|
raise "Encryption key not provided!" if @encryption_key.nil?
|
29
33
|
|
@@ -183,7 +183,7 @@ Invalid command. Valid commands:
|
|
183
183
|
expect(output_buffer.string).to eql(expected_output)
|
184
184
|
end
|
185
185
|
|
186
|
-
it 'should implement the help' do
|
186
|
+
it 'should implement the commands help' do
|
187
187
|
decoder_params[:arguments] = ['-h']
|
188
188
|
|
189
189
|
described_class.decode(decoder_params)
|
@@ -197,17 +197,28 @@ Valid commands:
|
|
197
197
|
expect(output_buffer.string).to eql(expected_output)
|
198
198
|
end
|
199
199
|
|
200
|
+
it "should display the command given command's help" do
|
201
|
+
decoder_params[:arguments] = ['command1', '-h']
|
202
|
+
$a = true
|
203
|
+
described_class.decode(decoder_params)
|
204
|
+
|
205
|
+
expected_output = %Q{\
|
206
|
+
Usage: rspec command1 [options] <arg1>
|
207
|
+
-h, --help Help
|
208
|
+
}
|
209
|
+
|
210
|
+
expect(output_buffer.string).to eql(expected_output)
|
211
|
+
end
|
212
|
+
|
200
213
|
end
|
201
214
|
|
202
|
-
describe '
|
215
|
+
describe 'No argv case' do
|
203
216
|
|
204
217
|
let(:decoder_params) {{
|
205
218
|
output: output_buffer,
|
206
219
|
}}
|
207
220
|
|
208
|
-
|
209
|
-
#
|
210
|
-
it 'should be avoided' do
|
221
|
+
it 'should avoided options being interpreted as definitions' do
|
211
222
|
decoder_params[:arguments] = ['pizza']
|
212
223
|
|
213
224
|
actual_result = described_class.decode(decoder_params)
|
@@ -23,6 +23,7 @@ describe SimpleScripting::Configuration do
|
|
23
23
|
let(:configuration_text) {"
|
24
24
|
abspath_key=/tmp/bar
|
25
25
|
relpath_key=foo
|
26
|
+
mixed_multiple_paths_key=/tmp/bar:foo
|
26
27
|
encr_key=uTxllKRD2S+IH92oi30luwu0JIqp7kKA
|
27
28
|
|
28
29
|
[group1]
|
@@ -40,6 +41,8 @@ g2_key=bang
|
|
40
41
|
|
41
42
|
expect(configuration.relpath_key.full_path).to eql(File.expand_path('foo', '~'))
|
42
43
|
|
44
|
+
expect(configuration.mixed_multiple_paths_key.full_paths).to eql(['/tmp/bar', File.expand_path('foo', '~')])
|
45
|
+
|
43
46
|
expect(configuration.encr_key.decrypted).to eql('encrypted_value')
|
44
47
|
|
45
48
|
expect(configuration.group1.g_key).to eql('baz')
|