arg-parser 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -84,4 +84,9 @@ list of features:
84
84
  * On-parse handler: A proc can be passed that will be called when the argument value is encountered
85
85
  during parsing. The return value of the proc will be used as the argument result.
86
86
  `on_parse: lambda{ |val, arg, hsh| val.split(',') }`
87
-
87
+ * On-parse handler reuse: Common parse handlers can be registered and used on multiple arguments.
88
+ Handlers are registered for reuse via the DSL method #register_parse_handler. To use a registered
89
+ parse handler for a particular argument, just pass the key under which the handler is registered.
90
+ * Pre-defined arguments: Arguments can be registered under a key, and then re-used across multiple
91
+ definitions via the #predefined_arg DSL method. Common arguments would typically be defined in a
92
+ shared file that was included into each job. See Argument.register and DSL.predefined_arg.
@@ -5,6 +5,8 @@ module ArgParser
5
5
  OnParseHandlers = {
6
6
  :split_to_array => lambda{ |val, arg, hsh| val.split(',') }
7
7
  }
8
+ # Hash containing registered arguments available via #predefined_arg
9
+ PredefinedArguments = { }
8
10
 
9
11
 
10
12
  # Abstract base class of all command-line argument types.
@@ -62,6 +64,47 @@ module ArgParser
62
64
  end
63
65
 
64
66
 
67
+ # Register a common argument for use in multiple argument definitions. The
68
+ # registered argument is a completely defined argument that can be added to
69
+ # any argument definition via Definition#predefined_arg.
70
+ #
71
+ # @see Definition#predefined_arg
72
+ #
73
+ # @param lookup_key [String, Symbol] The key with which to register the
74
+ # argument for subsequent lookup. This can be different from the key
75
+ # which will represent the argument when parsed etc; this makes it
76
+ # possible to register several alternate versions of the same argument
77
+ # for use in different circumstances.
78
+ # @param arg [Argument] An Argument sub-class that represents the arg
79
+ # that is to be registered for later use. #TODO: Document how arg is
80
+ # created.
81
+ def self.register(lookup_key, arg)
82
+ key = self.to_key(lookup_key)
83
+ if PredefinedArguments.has_key?(key)
84
+ raise ArgumentError, "An argument has already been registered under key '#{lookup_key}'"
85
+ end
86
+ PredefinedArguments[key] = arg
87
+ end
88
+
89
+
90
+ # Return a copy of a pre-defined argument for use in an argument
91
+ # definition.
92
+ #
93
+ # @param lookup_key [String, Symbol] The key with which to register the
94
+ # argument for subsequent lookup. This can be different from the key
95
+ # which will represent the argument when parsed etc; this makes it
96
+ # possible to register several alternate versions of the same argument
97
+ # for use in different circumstances.
98
+ # @return [Argument] A copy of the registered argument.
99
+ def self.lookup(lookup_key)
100
+ key = self.to_key(lookup_key)
101
+ unless arg = PredefinedArguments[key]
102
+ raise ArgumentError, "No pre-defined argument has been registered under key '#{lookup_key}'"
103
+ end
104
+ arg.clone
105
+ end
106
+
107
+
65
108
  private
66
109
 
67
110
  def initialize(key, desc, opts = {}, &block)
@@ -308,6 +351,9 @@ module ArgParser
308
351
  # /? flags on the command-line.
309
352
  # @param [Hash] opts Contains any options that are desired for this
310
353
  # argument.
354
+ # @option opts [Fixnum] :min_values The minimum number of rest values
355
+ # that must be supplied. Defaults to 1 if the RestArgument is
356
+ # required, or 0 if it is not.
311
357
  # @param [Block] block If supplied, the block passed will be invoked
312
358
  # after this argument value has been parsed from the command-line.
313
359
  # The block will be called with three arguments: this argument
@@ -101,6 +101,36 @@ module ArgParser
101
101
  end
102
102
 
103
103
 
104
+ # Lookup a pre-defined argument (created earlier via Argument#register),
105
+ # and add it to this arguments definition.
106
+ #
107
+ # @see Argument#register
108
+ #
109
+ # @param lookup_key [String, Symbol] The key under which the pre-defined
110
+ # argument was registered.
111
+ # @param desc [String] An optional override for the argument description
112
+ # for this use of the pre-defined argument.
113
+ # @param opts [Hash] An options hash for those select properties that
114
+ # can be overridden on a pre-defined argument.
115
+ # @option opts [String] :description The argument description for this
116
+ # use of the pre-defined argument.
117
+ # @option opts [String] :usage_break The usage break for this use of
118
+ # the pre-defined argument.
119
+ # @option opts [Boolean] :required Whether this argument is a required
120
+ # (i.e. mandatory) argument.
121
+ # @option opts [String] :default The default value for the argument,
122
+ # returned in the command-line parse results if no other value is
123
+ # specified.
124
+ def predefined_arg(lookup_key, opts = {})
125
+ arg = Argument.lookup(lookup_key)
126
+ arg.description = opts[:description] if opts[:description]
127
+ arg.useage_break = opts[:usage_break] if opts.has_key?(:usage_break)
128
+ arg.required = opts[:required] if opts.has_key?(:required)
129
+ arg.default = opts[:default] if opts.has_key?(:default)
130
+ self << arg
131
+ end
132
+
133
+
104
134
  # Individual arguments are optional, but exactly one of +keys+ arguments
105
135
  # is required.
106
136
  def require_one_of(*keys)
@@ -96,6 +96,19 @@ module ArgParser
96
96
  args_def.rest_arg(key, desc, opts, &block)
97
97
  end
98
98
 
99
+ # Use a pre-defined argument.
100
+ # @see Definition#predefined_arg
101
+ def predefined_arg(lookup_key, desc = nil, opts = {})
102
+ if desc.is_a?(Hash)
103
+ opts = desc
104
+ desc = nil
105
+ end
106
+ opts.merge!(@arg_opts){ |k, e, n| e || n } if @arg_opts
107
+ opts[:description] = desc if desc
108
+ @arg_opts = nil
109
+ args_def.predefined_arg(lookup_key, opts)
110
+ end
111
+
99
112
  # Set a label for a usage break to be applied on the next argument
100
113
  # that is defined.
101
114
  def usage_break(label)
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arg-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Adam Gardiner
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-02-25 00:00:00.000000000 Z
12
+ date: 2015-05-29 00:00:00.000000000 Z
12
13
  dependencies: []
13
14
  description: ! " ArgParser is a simple, yet powerful command-line argument
14
15
  parser, with\n support for positional, keyword, flag and rest arguments,
@@ -18,35 +19,36 @@ executables: []
18
19
  extensions: []
19
20
  extra_rdoc_files: []
20
21
  files:
21
- - LICENSE
22
22
  - README.md
23
- - lib/arg-parser.rb
23
+ - LICENSE
24
24
  - lib/arg-parser/argument.rb
25
25
  - lib/arg-parser/definition.rb
26
26
  - lib/arg-parser/dsl.rb
27
27
  - lib/arg-parser/parser.rb
28
+ - lib/arg-parser.rb
28
29
  - lib/arg_parser.rb
29
30
  homepage: https://github.com/agardiner/arg-parser
30
31
  licenses: []
31
- metadata: {}
32
32
  post_install_message:
33
33
  rdoc_options: []
34
34
  require_paths:
35
35
  - lib
36
36
  required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
37
38
  requirements:
38
39
  - - ! '>='
39
40
  - !ruby/object:Gem::Version
40
41
  version: '0'
41
42
  required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
42
44
  requirements:
43
45
  - - ! '>='
44
46
  - !ruby/object:Gem::Version
45
47
  version: '0'
46
48
  requirements: []
47
49
  rubyforge_project:
48
- rubygems_version: 2.4.1
50
+ rubygems_version: 1.8.21
49
51
  signing_key:
50
- specification_version: 4
52
+ specification_version: 3
51
53
  summary: ArgParser is a simple, yet powerful, command-line argument (option) parser
52
54
  test_files: []
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YjVkYzMxZDYxNTUzZjdlOTEwMjY3MThiYzNmYTc1OTQ4ZWQ4MjkxZQ==
5
- data.tar.gz: !binary |-
6
- ZWI1MTc5MDc3ZDE0YjZhZmJiODFlZDBjN2Q0OTFmMjY3ODQ4YzMxZQ==
7
- SHA512:
8
- metadata.gz: !binary |-
9
- ZWQ1ZmIzODNlMDI4YjFlZDcwODQ2MDIzODI4MmMyMTg3OTE2MjA4NTZjZmYz
10
- OTlkMzQ2NTBmM2YxNzFjOWZmMTM5NjZjM2RmZTZiYzU5MzQ2M2Y0NjVhMGMw
11
- ZDM2Zjk4MmVkODBmMjM1YWUxNGMyODllOTgxYmRlYWMwMjMxMGY=
12
- data.tar.gz: !binary |-
13
- NWMwOTk4NDU1NGQwNDNmMTc4Zjc2MGJhZGM4YmE3OWM1MDZmM2RkODQ1ZjU2
14
- OTdkOGQxYmJiNDFjZGU3MGRmMzc3MzFhOTA2MDc0MTU3YzRhMjA3NTY2YmY4
15
- NmJlYzdiZGVkYjc0MDY1ZTdjMTJhYjIxZTFkOTUwNWRmMWY4Mjk=