clasp-ruby 0.15.2 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c21f8a690de12605745b8001539ad68e6782bf45
4
- data.tar.gz: f293ce2cb5788ab18e26717fc46189b21e7ac1a7
3
+ metadata.gz: 0d2393ba0de2e70168bc58f88cdf73b9d4f5e748
4
+ data.tar.gz: 60463d18965a903998197c5f0b1dd9cb4bb10836
5
5
  SHA512:
6
- metadata.gz: c39760c3a529ac09b824dd7e8211f95967f7d2278584b45885b12f4bda825375a8af94825d1b05dba8b292d5ca1404dcc868042d9bc5926fce2186a69fdf7f7f
7
- data.tar.gz: f64a8b1a004295b65411d939ccf5f50065954432668f618e35f76b9376ab7162d4792f110d7bb04a1f5ee812092accabe0447895746ebcd3b51bae6f08edc5f1
6
+ metadata.gz: 7b3aa22592465766c7486b75b538b66436c27b67cc502c6d7eb29e2aa59eb9cf160ef0c043409ed4acbaeb406e6952e050152b2ff1dbbd729f735924fbea33ff
7
+ data.tar.gz: e6e14d4f75a31553b3a40394b0b4cbd1920f2cbf6ee630c43d9f2301dcfe4a96d85257f669ea76f9770f1f8e472357336cc53fe43788a44531f7447a1955e9b6
@@ -0,0 +1,203 @@
1
+ # CLASP.Ruby Example - **show_usage_and_version**
2
+
3
+ ## Summary
4
+
5
+ Example illustrating various kinds of *flag* and *option* aliases, including the combination of short-names.
6
+
7
+ ## Source
8
+
9
+ ```ruby
10
+ #!/usr/bin/env ruby
11
+
12
+ # examples/flag_and_option_aliases.rb
13
+
14
+ # requires
15
+
16
+ require 'clasp'
17
+
18
+ # constants
19
+
20
+ ProgramVersion = [ 0, 0, 1 ]
21
+
22
+ InfoLines = [
23
+
24
+ 'CLASP.Ruby examples',
25
+ :version,
26
+ "Illustrates use of CLASP.Ruby's use of flags, options, and aliases",
27
+ '',
28
+ ]
29
+
30
+ # Specify aliases, parse, and checking standard flags
31
+
32
+ Flag_Debug = CLASP.Flag('--debug', alias: '-d', help: 'runs in Debug mode')
33
+ Option_Verbosity = CLASP.Option('--verbosity', alias: '-v', help: 'specifies the verbosity', values: [ 'terse', 'quiet', 'silent', 'chatty' ])
34
+ Flag_Chatty = CLASP.Flag('--verbosity=chatty', alias: '-c')
35
+
36
+ Aliases = [
37
+
38
+ Flag_Debug,
39
+ Option_Verbosity,
40
+ Flag_Chatty,
41
+
42
+ CLASP::FlagAlias.Help,
43
+ CLASP::FlagAlias.Version,
44
+ ]
45
+
46
+ args = CLASP::Arguments.new ARGV, Aliases
47
+
48
+ if args.flags.include?(CLASP::FlagAlias.Help)
49
+
50
+ CLASP.show_usage(Aliases, exit_code: 0, version: ProgramVersion, stream: $stdout, info_lines: InfoLines)
51
+ end
52
+
53
+ if args.flags.include?('--version')
54
+
55
+ CLASP.show_version(Aliases, exit_code: 0, version: ProgramVersion, stream: $stdout)
56
+ end
57
+
58
+
59
+ # Program-specific processing of flags/options
60
+
61
+ if (opt = args.find_option('--verbosity'))
62
+
63
+ $stdout.puts "verbosity is specified as: #{opt.value}"
64
+ end
65
+
66
+ if args.flags.include?('--debug')
67
+
68
+ $stdout.puts 'Debug mode is specified'
69
+ end
70
+
71
+
72
+
73
+ # Check for any unrecognised flags or options
74
+
75
+ if (unused = args.find_first_unknown())
76
+
77
+ $stderr.puts "#{args.program_name}: unrecognised flag/option: #{unused}"
78
+
79
+ sys.exit(1)
80
+ end
81
+ ```
82
+
83
+ ## Usage
84
+
85
+ ### No arguments
86
+
87
+ If executed with no arguments
88
+
89
+ ```
90
+ ruby examples/flag_and_option_aliases.rb
91
+ ```
92
+
93
+ or (in a Unix shell):
94
+
95
+ ```
96
+ ./examples/flag_and_option_aliases.rb
97
+ ```
98
+
99
+ it gives the output:
100
+
101
+ ```
102
+ ```
103
+
104
+ ### Show usage
105
+
106
+ If executed with the arguments
107
+
108
+ ```
109
+ ruby examples/flag_and_option_aliases.rb --help
110
+ ```
111
+
112
+ it gives the output:
113
+
114
+ ```
115
+ CLASP.Ruby examples
116
+ flag_and_option_aliases.rb 0.0.1
117
+ Illustrates use of CLASP.Ruby's use of flags, options, and aliases
118
+
119
+ USAGE: flag_and_option_aliases.rb [ ... flags and options ... ]
120
+
121
+ flags/options:
122
+
123
+ -d
124
+ --debug
125
+ runs in Debug mode
126
+
127
+ -c --verbosity=chatty
128
+ -v <value>
129
+ --verbosity=<value>
130
+ specifies the verbosity
131
+ where <value> one of:
132
+ terse
133
+ quiet
134
+ silent
135
+ chatty
136
+
137
+ --help
138
+ shows this help and terminates
139
+
140
+ --version
141
+ shows version and terminates
142
+ ```
143
+
144
+ ### Specify flags and options in long-form
145
+
146
+ If executed with the arguments
147
+
148
+ ```
149
+ ruby examples/flag_and_option_aliases.rb --debug --verbosity=silent
150
+ ```
151
+
152
+ it gives the output:
153
+
154
+ ```
155
+ verbosity is specified as: silent
156
+ Debug mode is specified
157
+ ```
158
+
159
+ ### Specify flags and options in short-form
160
+
161
+ If executed with the arguments
162
+
163
+ ```
164
+ ruby examples/flag_and_option_aliases.rb -v silent -d
165
+ ```
166
+
167
+ it gives the (same) output:
168
+
169
+ ```
170
+ verbosity is specified as: silent
171
+ Debug mode is specified
172
+ ```
173
+
174
+ ### Specify flags and options in short-form, including an alias for an option-with-value
175
+
176
+ If executed with the arguments
177
+
178
+ ```
179
+ ruby examples/flag_and_option_aliases.rb -c -d
180
+ ```
181
+
182
+ it gives the output:
183
+
184
+ ```
185
+ verbosity is specified as: chatty
186
+ Debug mode is specified
187
+ ```
188
+
189
+ ### Specify flags and options with combined short-form
190
+
191
+ If executed with the arguments
192
+
193
+ ```
194
+ ruby examples/flag_and_option_aliases.rb -dc
195
+ ```
196
+
197
+ it gives the (same) output:
198
+
199
+ ```
200
+ verbosity is specified as: chatty
201
+ Debug mode is specified
202
+ ```
203
+
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # examples/flag_and_option_aliases.rb
4
+
5
+ # requires
6
+
7
+ require 'clasp'
8
+
9
+ # constants
10
+
11
+ ProgramVersion = [ 0, 0, 1 ]
12
+
13
+ InfoLines = [
14
+
15
+ 'CLASP.Ruby examples',
16
+ :version,
17
+ "Illustrates use of CLASP.Ruby's use of flags, options, and aliases",
18
+ '',
19
+ ]
20
+
21
+ # Specify aliases, parse, and checking standard flags
22
+
23
+ Flag_Debug = CLASP.Flag('--debug', alias: '-d', help: 'runs in Debug mode')
24
+ Option_Verbosity = CLASP.Option('--verbosity', alias: '-v', help: 'specifies the verbosity', values: [ 'terse', 'quiet', 'silent', 'chatty' ])
25
+ Flag_Chatty = CLASP.Flag('--verbosity=chatty', alias: '-c')
26
+
27
+ Aliases = [
28
+
29
+ Flag_Debug,
30
+ Option_Verbosity,
31
+ Flag_Chatty,
32
+
33
+ CLASP::FlagAlias.Help,
34
+ CLASP::FlagAlias.Version,
35
+ ]
36
+
37
+ args = CLASP::Arguments.new ARGV, Aliases
38
+
39
+ if args.flags.include?(CLASP::FlagAlias.Help)
40
+
41
+ CLASP.show_usage(Aliases, exit_code: 0, version: ProgramVersion, stream: $stdout, info_lines: InfoLines)
42
+ end
43
+
44
+ if args.flags.include?('--version')
45
+
46
+ CLASP.show_version(Aliases, exit_code: 0, version: ProgramVersion, stream: $stdout)
47
+ end
48
+
49
+
50
+ # Program-specific processing of flags/options
51
+
52
+ if (opt = args.find_option('--verbosity'))
53
+
54
+ $stdout.puts "verbosity is specified as: #{opt.value}"
55
+ end
56
+
57
+ if args.flags.include?('--debug')
58
+
59
+ $stdout.puts 'Debug mode is specified'
60
+ end
61
+
62
+
63
+
64
+ # Check for any unrecognised flags or options
65
+
66
+ if (unused = args.find_first_unknown())
67
+
68
+ $stderr.puts "#{args.program_name}: unrecognised flag/option: #{unused}"
69
+
70
+ exit 1
71
+ end
72
+
73
+
74
+
@@ -0,0 +1,141 @@
1
+ # CLASP.Ruby Example - **show_usage_and_version**
2
+
3
+ ## Summary
4
+
5
+ Simple example supporting ```--help``` and ```--version```.
6
+
7
+ ## Source
8
+
9
+ ```ruby
10
+ #!/usr/bin/env ruby
11
+
12
+ # examples/show_usage_and_version.rb
13
+
14
+ # requires
15
+
16
+ require 'clasp'
17
+
18
+ # constants
19
+
20
+ ProgramVersion = [ 0, 0, 1 ]
21
+
22
+ InfoLines = [
23
+
24
+ 'CLASP.Ruby examples',
25
+ :version,
26
+ "Illustrates use of CLASP.Ruby's CLASP.show_usage() and CLASP.show_version() methods",
27
+ '',
28
+ ]
29
+
30
+ # Specify aliases, parse, and checking standard flags
31
+
32
+ Aliases = [
33
+
34
+ CLASP::FlagAlias.Help,
35
+ CLASP::FlagAlias.Version,
36
+ ]
37
+
38
+ args = CLASP::Arguments.new ARGV, Aliases
39
+
40
+ if args.flags.include?('--help')
41
+
42
+ CLASP.show_usage(Aliases, exit_code: 0, version: ProgramVersion, stream: $stdout, info_lines: InfoLines)
43
+ end
44
+
45
+ if args.flags.include?('--version')
46
+
47
+ CLASP.show_version(Aliases, exit_code: 0, version: ProgramVersion, stream: $stdout)
48
+ end
49
+
50
+
51
+ # Check for any unrecognised flags or options
52
+
53
+ if (unused = args.find_first_unknown())
54
+
55
+ $stderr.puts "#{args.program_name}: unrecognised flag/option: #{unused}"
56
+
57
+ sys.exit(1)
58
+ end
59
+
60
+
61
+ $stdout.puts 'no flags specified'
62
+ ```
63
+
64
+ ## Usage
65
+
66
+ ### No arguments
67
+
68
+ If executed with no arguments
69
+
70
+ ```
71
+ ruby examples/show_usage_and_version.rb
72
+ ```
73
+
74
+ or (in a Unix shell):
75
+
76
+ ```
77
+ ./examples/show_usage_and_version.rb
78
+ ```
79
+
80
+ it gives the output:
81
+
82
+ ```
83
+ no flags specified
84
+ ```
85
+
86
+ ### Show usage
87
+
88
+ If executed with the arguments
89
+
90
+ ```
91
+ ruby examples/show_usage_and_version.rb --help
92
+ ```
93
+
94
+ it gives the output:
95
+
96
+ ```
97
+ CLASP.Ruby examples
98
+ show_usage_and_version.rb 0.0.1
99
+ Illustrates use of CLASP.Ruby's show_usage() and show_version() methods
100
+
101
+ USAGE: show_usage_and_version.rb [ ... flags and options ... ]
102
+
103
+ flags/options:
104
+
105
+ --help
106
+ Shows usage and terminates
107
+
108
+ --version
109
+ Shows version and terminates
110
+ ```
111
+
112
+ ### Show version
113
+
114
+ If executed with the arguments
115
+
116
+ ```
117
+ ruby examples/show_usage_and_version.rb --version
118
+ ```
119
+
120
+ it gives the output:
121
+
122
+ ```
123
+ show_usage_and_version.rb 0.0.1
124
+ ```
125
+
126
+ ### Unknown option
127
+
128
+ If executed with the arguments
129
+
130
+ ```
131
+ ruby examples/show_usage_and_version.rb --unknown=value
132
+ ```
133
+
134
+ it gives the output (on the standard error stream):
135
+
136
+ ```
137
+ show_usage_and_version.rb: unrecognised flag/option: --unknown=value
138
+ ```
139
+
140
+ with an exit code of 1
141
+
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # examples/show_usage_and_version.rb
4
+
5
+ # requires
6
+
7
+ require 'clasp'
8
+
9
+ # constants
10
+
11
+ ProgramVersion = [ 0, 0, 1 ]
12
+
13
+ InfoLines = [
14
+
15
+ 'CLASP.Ruby examples',
16
+ :version,
17
+ "Illustrates use of CLASP.Ruby's CLASP.show_usage() and CLASP.show_version() methods",
18
+ '',
19
+ ]
20
+
21
+ # Specify aliases, parse, and checking standard flags
22
+
23
+ Aliases = [
24
+
25
+ CLASP::FlagAlias.Help,
26
+ CLASP::FlagAlias.Version,
27
+ ]
28
+
29
+ args = CLASP::Arguments.new ARGV, Aliases
30
+
31
+ if args.flags.include?('--help')
32
+
33
+ CLASP.show_usage(Aliases, exit_code: 0, version: ProgramVersion, stream: $stdout, info_lines: InfoLines)
34
+ end
35
+
36
+ if args.flags.include?('--version')
37
+
38
+ CLASP.show_version(Aliases, exit_code: 0, version: ProgramVersion, stream: $stdout)
39
+ end
40
+
41
+
42
+ # Check for any unrecognised flags or options
43
+
44
+ if (unused = args.find_first_unknown())
45
+
46
+ $stderr.puts "#{args.program_name}: unrecognised flag/option: #{unused}"
47
+
48
+ exit 1
49
+ end
50
+
51
+
52
+ $stdout.puts 'no flags specified'
53
+
54
+
@@ -205,6 +205,7 @@ class Arguments
205
205
 
206
206
  init_opts[:mutate_argv] = true unless init_opts.has_key? :mutate_argv
207
207
 
208
+ @program_name = init_opts[:program_name] || Arguments.derive_program_name_
208
209
 
209
210
  @argv = argv
210
211
  argv = argv.dup
@@ -268,6 +269,11 @@ class Arguments
268
269
  end
269
270
 
270
271
  private
272
+ def self.derive_program_name_
273
+
274
+ $0
275
+ end
276
+
271
277
  def self.parse(argv, aliases)
272
278
 
273
279
  flags = []
@@ -427,6 +433,8 @@ class Arguments
427
433
  # unchanged copy of the original array of arguments passed to new
428
434
  attr_reader :argv_original_copy
429
435
 
436
+ attr_reader :program_name
437
+
430
438
 
431
439
  # finds the first unknown flag or option; +nil+ if all used
432
440
  def find_first_unknown options = {}
@@ -452,6 +460,42 @@ class Arguments
452
460
  nil
453
461
  end
454
462
 
463
+ # Searches for a flag that matches the given id, returning the flag if
464
+ # found; +nil+ otherwise
465
+ #
466
+ # === Signature
467
+ #
468
+ # * *Parameters*:
469
+ # - +id+:: (String, CLASP::Flag) The name of a flag, or the flag
470
+ # itself
471
+ def find_flag(id)
472
+
473
+ flags.each do |flag|
474
+
475
+ return flag if flag == id
476
+ end
477
+
478
+ nil
479
+ end
480
+
481
+ # Searches for a option that matches the given id, returning the option
482
+ # if found; +nil+ otherwise
483
+ #
484
+ # === Signature
485
+ #
486
+ # * *Parameters*:
487
+ # - +id+:: (String, CLASP::Flag) The name of a option, or the option
488
+ # itself
489
+ def find_option(id)
490
+
491
+ options.each do |option|
492
+
493
+ return option if option == id
494
+ end
495
+
496
+ nil
497
+ end
498
+
455
499
  # #################################################################### #
456
500
  # backwards-compatible
457
501
 
@@ -5,7 +5,7 @@
5
5
  # Purpose: Command-line interface
6
6
  #
7
7
  # Created: 27th July 2015
8
- # Updated: 14th February 2019
8
+ # Updated: 19th March 2019
9
9
  #
10
10
  # Home: http://github.com/synesissoftware/CLASP.Ruby
11
11
  #
@@ -244,7 +244,9 @@ def self.show_usage aliases, options={}
244
244
  end
245
245
  end
246
246
 
247
- exit options[:exit] if options[:exit]
247
+ exit_code = options[:exit_code] || options[:exit]
248
+
249
+ exit exit_code if exit_code
248
250
  end
249
251
 
250
252
  # Displays version for the program according to the given aliases and options
@@ -281,7 +283,9 @@ def self.show_version aliases, options = {}
281
283
 
282
284
  stream.puts version_string
283
285
 
284
- exit options[:exit] if options[:exit]
286
+ exit_code = options[:exit_code] || options[:exit]
287
+
288
+ exit exit_code if exit_code
285
289
  end
286
290
 
287
291
  # ######################################################################## #
@@ -51,7 +51,7 @@
51
51
  module CLASP
52
52
 
53
53
  # Current version of the CLASP.Ruby library
54
- VERSION = '0.15.2'
54
+ VERSION = '0.16.0'
55
55
 
56
56
  private
57
57
  VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i } # :nodoc:
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clasp-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.2
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Wilson
@@ -35,6 +35,10 @@ files:
35
35
  - LICENSE
36
36
  - README.md
37
37
  - examples/cr-example.rb
38
+ - examples/flag_and_option_aliases.md
39
+ - examples/flag_and_option_aliases.rb
40
+ - examples/show_usage_and_version.md
41
+ - examples/show_usage_and_version.rb
38
42
  - lib/clasp-ruby.rb
39
43
  - lib/clasp.rb
40
44
  - lib/clasp/aliases.rb