argparser 1.0.rc1 → 1.0.0

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: 3cff1283a52db9d44ad99667cf617ce8ca7c9a3f
4
- data.tar.gz: 7d5976fa654b6d36a2a02a3daf2deefe97f86703
3
+ metadata.gz: dfd904524c50a0940710fd85f7739820409c9e10
4
+ data.tar.gz: a17ea81dc8409f8bd8eaab57a7c2a31ef87dade9
5
5
  SHA512:
6
- metadata.gz: 3f7c962841f1173cd972ed7dff430f99f67c78822aed82df1af28242299c2961b034548dbd8913c0aafb48a3cdadfb348ec15a9fffdebf238e0639e0f849d498
7
- data.tar.gz: 6384e7a3e0be42426ca87b022f6ef8899163c22166c1a47f12e9295bfcafbd436cadd8688b6e467513c7ce2ff159302b65ca6b4735944e2b50a06776dd4cca0c
6
+ metadata.gz: 90663f798ea4eced3de8423b969f61203f921e9a32a794d1f4b9b040e70ffe40db88275744da3f7e225288600e5a0e4ce64a2ccb93d840ddb9e2079ff4d15bfb
7
+ data.tar.gz: 2cdcea748f35be75a1cf26a78cb7b550a94656b4b60ccc1251000211d05afa0f31f63b4b0cd0e6b7c02054ffc8c326169ce6465e594d6e009708add47768752f
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ thumbs.db
17
17
  .ruby-version
18
18
  Gemfile.lock
19
19
  core
20
+ *.gem
data/README.md CHANGED
@@ -0,0 +1,102 @@
1
+ # argparser
2
+ Command line argument parser library, trying to follow POSIX and GNU guidelines.
3
+
4
+ ## Installation
5
+ `gem install argparser`, as usual.
6
+
7
+ ## Usage by example
8
+ Suppose there's a file named `example.rb` like this:
9
+ ````ruby
10
+ require 'argparser'
11
+ args= ArgParser.new( # Here goes the manifest.
12
+ :program => 'example.rb', # Use additional properties like these:
13
+ :version => '1.0', # :info, :copyright, :license,
14
+ :options => [{ # :package, :bugs, :homepage
15
+ :names => %w[m mode],
16
+ :argument => 'first|second|third',
17
+ :default => 'first',
18
+ :multiple => true,
19
+ :help => 'Example mode.',
20
+ :validate => (lambda {|this, parser| # Validating value in-line
21
+ possible = this.argument.split('|')
22
+ this.value.select{|v| possible.include?(v)}.size == this.value.size })
23
+ }, {
24
+ :names => 'file',
25
+ :input => true,
26
+ :required => true,
27
+ :help => 'Filename or - for stdin.',
28
+ :validate => (lambda {|this, parser|
29
+ if this.value == '-'
30
+ this.value = $stdin.read
31
+ else
32
+ parser.terminate(2, 'No such file') unless File.exists?(this.value)
33
+ this.value = File.read(this.value)
34
+ end
35
+ true })
36
+ }]
37
+ ).parse! # Uses ARGV by default, you may supply your own arguments.
38
+ # It exits if bad arguments given or they aren't validated.
39
+
40
+ puts args['mode'].value.inspect # So we could use our options...
41
+ puts args['file'].value # Prints contents of a file
42
+ ````
43
+
44
+ Now, let's look at the output of example given in various cases.
45
+
46
+ `$ ruby example.rb` is unsufficient:
47
+ ````
48
+ example.rb [-m, --mode first|second|third]... file
49
+ Expected argument: file
50
+ ````
51
+
52
+ `$ ruby example.rb --help` helps:
53
+ ````
54
+ example.rb [-m, --mode first|second|third]... file
55
+ [-m, --mode first|second|third]...
56
+ Example mode.
57
+ Defaults to: first
58
+ [--help]
59
+ Print this help and exit.
60
+ [--version]
61
+ Print version and exit.
62
+ file
63
+ Filename or - for stdin.
64
+ ````
65
+
66
+ `$ echo "content" | ruby example.rb -` does the trick:
67
+ ````
68
+ ["first"]
69
+ content
70
+ ````
71
+
72
+ `$ echo "content" | ruby example.rb --mode fourth -` oopses:
73
+ ````
74
+ example.rb [-m, --mode first|second|third]... file
75
+ Invalid option: m
76
+ ````
77
+
78
+ `$ echo "content" | ruby example.rb -abcm first -`:
79
+ ````
80
+ example.rb [-m, --mode first|second|third]... file
81
+ Unknown option: a
82
+ ````
83
+
84
+ ## Consider more rules
85
+ * `--help` and `--version` options provided for free unless specified.
86
+ * printed synopsys provided for free unless specified
87
+ * `:default` used if option has :argument and no value given, lowest priority
88
+ * `:env => 'ENV_VAR'` to pick default value from ENV, high priority
89
+ * `:eval => 'ruby expr'` to pick default from eval(...), useful for read defaults from config files, so it has low priority
90
+ * `--`-argument honored
91
+
92
+ ## Documentation
93
+ This README is all i could say in a rush. No other documentation provided at this moment, see the sources.
94
+
95
+ ## If you've found a bug or drawback
96
+ Don't hesistate to leave a report.
97
+
98
+ ## License
99
+ MIT for now.
100
+
101
+ ## TODO
102
+ * Go steal milk for the hazards applied.
data/argparser.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  Gem::Specification.new do |spec|
3
3
  spec.name = 'argparser'
4
- spec.version = '1.0.rc1'
4
+ spec.version = '1.0.0'
5
5
  spec.authors = ['sinm']
6
6
  spec.email = 'sinm.sinm@gmail.com'
7
7
  spec.summary = 'Command line argument parser'
@@ -0,0 +1,32 @@
1
+ require 'argparser'
2
+ args= ArgParser.new( # Here goes the manifest.
3
+ :program => 'example.rb', # Use additional properties like these:
4
+ :version => '1.0', # :info, :copyright, :license,
5
+ :options => [{ # :package, :bugs, :homepage
6
+ :names => %w[m mode],
7
+ :argument => 'first|second|third',
8
+ :default => 'first',
9
+ :multiple => true,
10
+ :help => 'Example mode.',
11
+ :validate => (lambda {|this, parser| # Validating value in-line
12
+ possible = this.argument.split('|')
13
+ this.value.select{|v| possible.include?(v)}.size == this.value.size })
14
+ }, {
15
+ :names => 'file',
16
+ :input => true,
17
+ :required => true,
18
+ :help => 'Filename or - for stdin.',
19
+ :validate => (lambda {|this, parser|
20
+ if this.value == '-'
21
+ this.value = $stdin.read
22
+ else
23
+ parser.terminate(2, 'No such file') unless File.exists?(this.value)
24
+ this.value = File.read(this.value)
25
+ end
26
+ true })
27
+ }]
28
+ ).parse! # Uses ARGV by default, you may supply your own arguments.
29
+ # It exits if bad arguments given or they aren't validated.
30
+
31
+ puts args['mode'].value.inspect # So we could use our options...
32
+ puts args['file'].value # Prints contents of a file
data/lib/argparser.rb CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  #__END__
3
2
  module Tulz
4
3
  def hash2vars!(hash)
@@ -42,9 +41,13 @@ class ArgParser
42
41
  OUT_ARGUMENT_EXPECTED = 'Expected argument: %s'
43
42
  OUT_UNIQUE_NAME = 'Option name should be unique: %s'
44
43
  INVALID_OPTION = 'Invalid option: %s'
44
+ OPT_ENOUGH = '--'
45
+
46
+ # These options don't display their synopsis and given for free unless
47
+ # explicitly specified in the manifest.
45
48
  OPT_HELP = 'help'
46
49
  OPT_VERSION = 'version'
47
- OPT_ENOUGH = '--'
50
+ OPTS_RESERVED = [OPT_HELP, OPT_VERSION]
48
51
 
49
52
  class Option
50
53
  include Tulz
@@ -200,7 +203,7 @@ class ArgParser
200
203
  end
201
204
  end
202
205
 
203
- [OPT_VERSION, OPT_HELP].each { |o|
206
+ OPTS_RESERVED.each { |o|
204
207
  next unless arguments.include?("--#{o}")
205
208
  o = self[o]
206
209
  o.set_value(nil)
@@ -229,7 +232,7 @@ class ArgParser
229
232
  option.set_value(nil)
230
233
  end
231
234
  else
232
- terminate(2, OUT_UNKNOWN_OPTION % a)
235
+ terminate(2, OUT_UNKNOWN_OPTION % $1)
233
236
  end
234
237
  elsif a =~ /^-([^-].*)/ # short option, may combine and has an arg at end
235
238
  (opts = $1).chars.to_a.each_with_index do |char, index|
@@ -249,7 +252,7 @@ class ArgParser
249
252
  option.set_value(nil)
250
253
  end
251
254
  else
252
- terminate(2, OUT_UNKNOWN_OPTION % a)
255
+ terminate(2, OUT_UNKNOWN_OPTION % char)
253
256
  end
254
257
  end
255
258
  else
@@ -334,33 +337,18 @@ class ArgParser
334
337
 
335
338
  def printed_synopsis
336
339
  s = synopsis ||
337
- (options.select{|o| !o.input} + inputs).map{|o| o.synopsis}.join(' ')
340
+ (options.select{|o| !o.input} + inputs).map{|o|
341
+ OPTS_RESERVED.include?(o.name) ? nil : o.synopsis}.compact.join(' ')
338
342
  "#{program} #{s}"
339
343
  end
340
344
 
341
- if __FILE__ == $0 # Some selftests
345
+ if __FILE__ == $0 # Some selftests... while hakin in an editor
342
346
  $stdout.sync = true
343
347
  $stderr.sync = true
344
- args = ArgParser.new(
345
- :version => '00',
346
- :program => 'exec',
347
- :info => 'executes command over a group of servers',
348
- :options => [{
349
- :names => 'group',
350
- :input => true,
351
- :required => true,
352
- :help => 'Name of a group'
353
- },{
354
- :names => 'command',
355
- :input => true,
356
- :required => true,
357
- :multiple => true,
358
- :help => 'command to execute'
359
- }]
360
- ).parse!(%w[--help])
361
-
362
- puts args.options.map(&:to_s).join("\n")
363
- puts 'OK!'
348
+ ARGV = %w[--abcm first]
349
+ example = File.read('argparser/examples/example.rb')
350
+ example = example.split("\n")[1..-1].join("\n") # Cut 'require' string
351
+ eval(example) # Last line
364
352
  end
365
353
 
366
- end # class
354
+ end # the very end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: argparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.rc1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sinm
@@ -21,6 +21,7 @@ files:
21
21
  - README.md
22
22
  - argparser.gemspec
23
23
  - lib/argparser.rb
24
+ - lib/argparser/examples/example.rb
24
25
  homepage: https://github.com/sinm/argparser
25
26
  licenses:
26
27
  - MIT
@@ -36,9 +37,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
36
37
  version: '0'
37
38
  required_rubygems_version: !ruby/object:Gem::Requirement
38
39
  requirements:
39
- - - ">"
40
+ - - ">="
40
41
  - !ruby/object:Gem::Version
41
- version: 1.3.1
42
+ version: '0'
42
43
  requirements: []
43
44
  rubyforge_project:
44
45
  rubygems_version: 2.2.2