argy 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +0 -1
- data/.travis.yml +7 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +47 -0
- data/README.md +9 -79
- data/Rakefile +2 -5
- data/argy.gemspec +2 -5
- data/bin/example +7 -21
- data/lib/argy.rb +2 -36
- data/lib/argy/argument.rb +0 -3
- data/lib/argy/help.rb +13 -31
- data/lib/argy/option.rb +0 -13
- data/lib/argy/parameter.rb +1 -36
- data/lib/argy/parser.rb +24 -100
- data/lib/argy/version.rb +1 -1
- metadata +10 -13
- data/.github/workflows/build.yml +0 -22
- data/.github/workflows/publish.yml +0 -38
- data/.yardopts +0 -6
- data/lib/argy/options.rb +0 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dff9d90b687d261bae2c0820b5ea7b7f812d472d
|
4
|
+
data.tar.gz: 5543ca5b69b14891cbd8e1744df8b7b7480edcd0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ee7ec946a02ce9200fbf9318d708e5ae1a652092f9891050253e34b0ad628ba1aad81e0aa1431e655ebb20a1805b79d3e0800be67e9ed881cb7ad4a5c98673e
|
7
|
+
data.tar.gz: 8372f49e4daca19886b4850bfd1e6f04929a81a6584af4c57babccc0a9aa561bf5eaac035147c786a555e954639fe6ad5b35b301d603514db388d91a185c21a8
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
argy (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.2)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
equatable (0.6.1)
|
12
|
+
method_source (0.9.2)
|
13
|
+
pastel (0.7.3)
|
14
|
+
equatable (~> 0.6)
|
15
|
+
tty-color (~> 0.5)
|
16
|
+
pry (0.12.2)
|
17
|
+
coderay (~> 1.1.0)
|
18
|
+
method_source (~> 0.9.0)
|
19
|
+
rake (10.5.0)
|
20
|
+
rspec (3.9.0)
|
21
|
+
rspec-core (~> 3.9.0)
|
22
|
+
rspec-expectations (~> 3.9.0)
|
23
|
+
rspec-mocks (~> 3.9.0)
|
24
|
+
rspec-core (3.9.0)
|
25
|
+
rspec-support (~> 3.9.0)
|
26
|
+
rspec-expectations (3.9.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.9.0)
|
29
|
+
rspec-mocks (3.9.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.9.0)
|
32
|
+
rspec-support (3.9.0)
|
33
|
+
tty-color (0.5.0)
|
34
|
+
|
35
|
+
PLATFORMS
|
36
|
+
ruby
|
37
|
+
|
38
|
+
DEPENDENCIES
|
39
|
+
argy!
|
40
|
+
bundler (~> 2.0)
|
41
|
+
pastel
|
42
|
+
pry
|
43
|
+
rake (~> 10.0)
|
44
|
+
rspec (~> 3.0)
|
45
|
+
|
46
|
+
BUNDLED WITH
|
47
|
+
2.0.2
|
data/README.md
CHANGED
@@ -1,14 +1,8 @@
|
|
1
|
-
|
1
|
+
# Argy
|
2
2
|
|
3
|
-
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/argy`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
4
|
|
5
|
-
|
6
|
-
![Version](https://img.shields.io/gem/v/argy)
|
7
|
-
[![Coverage Status](https://coveralls.io/repos/github/rzane/argy/badge.svg?branch=master)](https://coveralls.io/github/rzane/argy?branch=master)
|
8
|
-
|
9
|
-
</div>
|
10
|
-
|
11
|
-
Yet another command-line option parser.
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
12
6
|
|
13
7
|
## Installation
|
14
8
|
|
@@ -28,81 +22,17 @@ Or install it yourself as:
|
|
28
22
|
|
29
23
|
## Usage
|
30
24
|
|
31
|
-
|
32
|
-
|
33
|
-
```ruby
|
34
|
-
require "argy"
|
35
|
-
|
36
|
-
parser = Argy.new do |o|
|
37
|
-
o.description "Prints messages"
|
38
|
-
o.usage "example"
|
39
|
-
o.example "$ example hello"
|
40
|
-
|
41
|
-
o.argument :message, desc: "message to print", required: true
|
42
|
-
|
43
|
-
o.option :loud, type: :boolean, desc: "say the message loudly"
|
44
|
-
o.option :count, type: :integer, desc: "number of times to print", default: 1
|
45
|
-
|
46
|
-
o.on "-v", "print the verison and exit" do
|
47
|
-
puts Example::VERSION
|
48
|
-
exit
|
49
|
-
end
|
50
|
-
|
51
|
-
o.on "-h", "--help", "show this help and exit" do
|
52
|
-
puts o.help
|
53
|
-
puts o.help.section "SECTION"
|
54
|
-
puts o.help.entry "foo", desc: "bar"
|
55
|
-
exit
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
begin
|
60
|
-
options = parser.parse(ARGV)
|
61
|
-
message = options.message
|
62
|
-
message = message.upcase if options.loud?
|
63
|
-
options.count.times { puts message }
|
64
|
-
rescue Argy::Error => err
|
65
|
-
abort err.message
|
66
|
-
end
|
67
|
-
```
|
68
|
-
|
69
|
-
## Option Types
|
70
|
-
|
71
|
-
Argy supports the following option types:
|
72
|
-
|
73
|
-
- `:string`
|
74
|
-
- `:boolean`
|
75
|
-
- `:integer`
|
76
|
-
- `:float`
|
77
|
-
- `:array`
|
78
|
-
- `:pathname`
|
25
|
+
TODO: Write usage instructions here
|
79
26
|
|
80
|
-
|
27
|
+
## Development
|
81
28
|
|
82
|
-
|
83
|
-
class NameOption
|
84
|
-
def self.call(input)
|
85
|
-
parts = input.split(" ")
|
86
|
-
raise Argy::CoersionError, "Invalid name" if parts.length != 2
|
87
|
-
new(*parts)
|
88
|
-
end
|
89
|
-
|
90
|
-
def initialize(first, last)
|
91
|
-
@first = first
|
92
|
-
@last = last
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
Argy.new do |o|
|
97
|
-
o.option :name, type: NameOption
|
98
|
-
end
|
99
|
-
```
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
100
30
|
|
101
|
-
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
102
32
|
|
103
33
|
## Contributing
|
104
34
|
|
105
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/argy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
106
36
|
|
107
37
|
## License
|
108
38
|
|
@@ -110,4 +40,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
110
40
|
|
111
41
|
## Code of Conduct
|
112
42
|
|
113
|
-
Everyone interacting in the Argy project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
43
|
+
Everyone interacting in the Argy project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/argy/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
data/argy.gemspec
CHANGED
@@ -8,12 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ["Ray Zane"]
|
9
9
|
spec.email = ["raymondzane@gmail.com"]
|
10
10
|
|
11
|
-
spec.summary = %q{
|
11
|
+
spec.summary = %q{A fully featured option parser.}
|
12
12
|
spec.license = "MIT"
|
13
13
|
|
14
|
-
spec.metadata["source_code_uri"] = "https://github.com/rzane/argy"
|
15
|
-
spec.metadata["documentation_uri"] = "https://rubydoc.info/github/rzane/argy"
|
16
|
-
|
17
14
|
# Specify which files should be added to the gem when it is released.
|
18
15
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
16
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
@@ -24,6 +21,6 @@ Gem::Specification.new do |spec|
|
|
24
21
|
spec.require_paths = ["lib"]
|
25
22
|
|
26
23
|
spec.add_development_dependency "bundler", "~> 2.0"
|
27
|
-
spec.add_development_dependency "rake", "~>
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
25
|
spec.add_development_dependency "rspec", "~> 3.0"
|
29
26
|
end
|
data/bin/example
CHANGED
@@ -3,30 +3,16 @@
|
|
3
3
|
require "bundler/setup"
|
4
4
|
require "argy"
|
5
5
|
|
6
|
-
|
7
|
-
o.description "Prints messages"
|
6
|
+
options = Argy.parse(ARGV) do |o|
|
8
7
|
o.usage "example"
|
9
|
-
o.
|
10
|
-
o.
|
11
|
-
o.
|
12
|
-
o.option :
|
8
|
+
o.description "A cool thing that does stuff"
|
9
|
+
o.example "$ example foo"
|
10
|
+
o.argument :jint, desc: "do a thing"
|
11
|
+
o.option :fizz, required: true, desc: "blah"
|
12
|
+
o.option :foo_bar, aliases: ["-f"], default: "jawn"
|
13
13
|
o.on "-v", "print the verison and exit" do
|
14
14
|
puts Argy::VERSION
|
15
|
-
exit
|
16
|
-
end
|
17
|
-
o.on "-h", "--help", "show this help and exit" do
|
18
|
-
puts o.help
|
19
|
-
puts o.help.section "SECTION"
|
20
|
-
puts o.help.entry "foo", desc: "bar"
|
21
|
-
exit
|
22
15
|
end
|
23
16
|
end
|
24
17
|
|
25
|
-
|
26
|
-
options = parser.parse(ARGV)
|
27
|
-
message = options.message
|
28
|
-
message = message.upcase if options.loud?
|
29
|
-
options.times.times { puts message }
|
30
|
-
rescue Argy::Error => err
|
31
|
-
abort err.message
|
32
|
-
end
|
18
|
+
puts options
|
data/lib/argy.rb
CHANGED
@@ -2,50 +2,16 @@ require "argy/version"
|
|
2
2
|
require "argy/parser"
|
3
3
|
|
4
4
|
module Argy
|
5
|
-
# Base class for all of Argy's errors.
|
6
5
|
Error = Class.new(StandardError)
|
7
|
-
|
8
|
-
# An error that is raised when an option
|
9
|
-
# cannot be coerced to the correct type
|
10
6
|
CoersionError = Class.new(Error)
|
11
|
-
|
12
|
-
# An error that is raised when an option
|
13
|
-
# is not valid.
|
14
7
|
ValidationError = Class.new(Error)
|
8
|
+
MissingArgumentError = Class.new(Error)
|
15
9
|
|
16
|
-
# An error that is raised when parsing fails.
|
17
|
-
class ParseError < Error
|
18
|
-
# The original error from OptionParser.
|
19
|
-
# @return [OptionParser::ParseError]
|
20
|
-
attr_reader :original
|
21
|
-
|
22
|
-
def initialize(original)
|
23
|
-
@original = original
|
24
|
-
super(original.message)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
# Define a new parser.
|
29
|
-
# @see Parser
|
30
|
-
# @example
|
31
|
-
# parser = Argy.new do |o|
|
32
|
-
# o.argument :input, desc: "the input file"
|
33
|
-
# o.option :verbose, type: :boolean
|
34
|
-
# end
|
35
|
-
#
|
36
|
-
# options = parser.parse(ARGV)
|
37
10
|
def self.new(&block)
|
38
11
|
Argy::Parser.new(&block)
|
39
12
|
end
|
40
13
|
|
41
|
-
|
42
|
-
# @see Parser
|
43
|
-
# @example
|
44
|
-
# options = Argy.parse do
|
45
|
-
# o.argument :input, desc: "the input file"
|
46
|
-
# o.option :verbose, type: :boolean
|
47
|
-
# end
|
48
|
-
def self.parse(argv: ARGV, &block)
|
14
|
+
def self.parse(argv, &block)
|
49
15
|
new(&block).parse(argv)
|
50
16
|
end
|
51
17
|
end
|
data/lib/argy/argument.rb
CHANGED
data/lib/argy/help.rb
CHANGED
@@ -1,18 +1,11 @@
|
|
1
1
|
module Argy
|
2
|
-
# Builds help information
|
3
2
|
class Help
|
4
|
-
# Create a new Help
|
5
|
-
# @param parser [Parser] the parser to generate help for
|
6
|
-
# @param column [Integer] the column width of the help
|
7
|
-
# @param color [TrueClass,FalseClass] whether or not to print with color
|
8
3
|
def initialize(parser, column: 30, color: $stdout.tty?)
|
9
4
|
@parser = parser
|
10
5
|
@column = column
|
11
6
|
@color = color
|
12
7
|
end
|
13
8
|
|
14
|
-
# The help information
|
15
|
-
# @return [String]
|
16
9
|
def to_s
|
17
10
|
out = []
|
18
11
|
|
@@ -26,26 +19,6 @@ module Argy
|
|
26
19
|
out.join("\n") + "\n"
|
27
20
|
end
|
28
21
|
|
29
|
-
# Format the title of a custom section
|
30
|
-
# @return [String]
|
31
|
-
def section(title)
|
32
|
-
bold "\n#{title}"
|
33
|
-
end
|
34
|
-
|
35
|
-
# Format an entry of a section
|
36
|
-
# @param name [String] left column of the entry
|
37
|
-
# @param desc [String] right column of the entry
|
38
|
-
# @param required [TrueClass,FalseClass] whether or not the entry is required
|
39
|
-
# @param default [Object] default value for the entry
|
40
|
-
# @return [String]
|
41
|
-
def entry(name, desc: nil, required: false, default: nil)
|
42
|
-
out = " #{name.ljust(column)}"
|
43
|
-
out += dim("#{desc} ") if desc
|
44
|
-
out += dim("(required) ") if required
|
45
|
-
out += dim("[default: #{default.inspect}]") if default
|
46
|
-
out.rstrip
|
47
|
-
end
|
48
|
-
|
49
22
|
private
|
50
23
|
|
51
24
|
def description(out)
|
@@ -68,7 +41,7 @@ module Argy
|
|
68
41
|
end
|
69
42
|
|
70
43
|
def argument(a)
|
71
|
-
|
44
|
+
parameter(a.label, a.desc, a.required?, a.default)
|
72
45
|
end
|
73
46
|
|
74
47
|
def options(out)
|
@@ -79,18 +52,19 @@ module Argy
|
|
79
52
|
def option(o)
|
80
53
|
label = [option_label(o.label, o.type)]
|
81
54
|
label += o.aliases.map { |a| option_label(a, o.type) }
|
82
|
-
|
55
|
+
parameter(label.join(", "), o.desc, o.required?, o.default)
|
83
56
|
end
|
84
57
|
|
85
58
|
def flags(out)
|
86
|
-
out << bold("\nFLAGS")
|
59
|
+
out << bold("\nFLAGS")
|
87
60
|
out.concat parser.flags.map { |f, _| flag(f) }
|
61
|
+
out << parameter("--help, -h", "show this help and exit")
|
88
62
|
end
|
89
63
|
|
90
64
|
def flag(flag)
|
91
65
|
flag = flag.dup
|
92
66
|
desc = flag.pop unless flag.last.match?(/^-/)
|
93
|
-
|
67
|
+
parameter(flag.reverse.join(", "), desc)
|
94
68
|
end
|
95
69
|
|
96
70
|
def option_label(label, type)
|
@@ -98,6 +72,14 @@ module Argy
|
|
98
72
|
label.start_with?("--") ? "#{label}=VALUE" : "#{label} VALUE"
|
99
73
|
end
|
100
74
|
|
75
|
+
def parameter(left, right, required = false, default = nil)
|
76
|
+
label = " #{left.ljust(column)}"
|
77
|
+
label += dim("#{right} ") if right
|
78
|
+
label += dim("(required) ") if required
|
79
|
+
label += dim("[default: #{default.inspect}]") if default
|
80
|
+
label.rstrip
|
81
|
+
end
|
82
|
+
|
101
83
|
def bold(text)
|
102
84
|
color? ? "\e[1m#{text}\e[0m" : text
|
103
85
|
end
|
data/lib/argy/option.rb
CHANGED
@@ -1,26 +1,14 @@
|
|
1
1
|
require "argy/parameter"
|
2
2
|
|
3
3
|
module Argy
|
4
|
-
# An option to be parsed from the command line
|
5
4
|
class Option < Parameter
|
6
|
-
# A list of alternative flags
|
7
|
-
# @return [Array<String>]
|
8
5
|
attr_reader :aliases
|
9
6
|
|
10
|
-
# Create a new Option
|
11
|
-
# @param name [Symbol] name of the parameter
|
12
|
-
# @param aliases [Array<String>] a list of alternative flags
|
13
|
-
# @param desc [String,nil] description for the parameter
|
14
|
-
# @param type [Symbol,#call] type of parameter
|
15
|
-
# @param default [Object] default value for the parameter
|
16
|
-
# @param required [TrueClass,FalseClass] whether or not the field is required
|
17
7
|
def initialize(*args, aliases: [], **opts)
|
18
8
|
super(*args, **opts)
|
19
9
|
@aliases = aliases
|
20
10
|
end
|
21
11
|
|
22
|
-
# The display label for the argument
|
23
|
-
# @return [String]
|
24
12
|
def label
|
25
13
|
case type
|
26
14
|
when :boolean
|
@@ -30,7 +18,6 @@ module Argy
|
|
30
18
|
end
|
31
19
|
end
|
32
20
|
|
33
|
-
# @private
|
34
21
|
def to_option_parser
|
35
22
|
options = []
|
36
23
|
options << aliases.join(" ") unless aliases.empty?
|
data/lib/argy/parameter.rb
CHANGED
@@ -1,31 +1,9 @@
|
|
1
|
-
require "pathname"
|
2
1
|
require "argy/parameter"
|
3
2
|
|
4
3
|
module Argy
|
5
|
-
# @abstract Subclasses must implement {#label}
|
6
4
|
class Parameter
|
7
|
-
|
8
|
-
# @return [String]
|
9
|
-
attr_reader :name
|
5
|
+
attr_reader :name, :type, :default, :desc
|
10
6
|
|
11
|
-
# The type of the parameter
|
12
|
-
# @return [String]
|
13
|
-
attr_reader :type
|
14
|
-
|
15
|
-
# The default value for the parameter
|
16
|
-
# @return [Object]
|
17
|
-
attr_reader :default
|
18
|
-
|
19
|
-
# The description for the parameter
|
20
|
-
# @return [String]
|
21
|
-
attr_reader :desc
|
22
|
-
|
23
|
-
# Create a new Parameter
|
24
|
-
# @param name [Symbol] name of the parameter
|
25
|
-
# @param desc [String,nil] description for the parameter
|
26
|
-
# @param type [Symbol,#call] type of parameter
|
27
|
-
# @param default [Object] default value for the parameter
|
28
|
-
# @param required [TrueClass,FalseClass] whether or not the field is required
|
29
7
|
def initialize(name, desc: nil, type: :string, default: nil, required: false)
|
30
8
|
@name = name
|
31
9
|
@type = type
|
@@ -34,33 +12,20 @@ module Argy
|
|
34
12
|
@required = required
|
35
13
|
end
|
36
14
|
|
37
|
-
# The display label for the paramter
|
38
|
-
# @abstract
|
39
|
-
# @return [String]
|
40
15
|
def label
|
41
16
|
raise NotImplementedError, __method__
|
42
17
|
end
|
43
18
|
|
44
|
-
# Check if the parameter is required
|
45
|
-
# @return [TrueClass,FalseClass]
|
46
19
|
def required?
|
47
20
|
@required
|
48
21
|
end
|
49
22
|
|
50
|
-
# Validates a value.
|
51
|
-
# @return [Object] the value
|
52
|
-
# @raise [ValidationError] if the valid is invalid
|
53
23
|
def validate(value)
|
54
24
|
if required? && value.nil?
|
55
25
|
raise ValidationError, "`#{label}` is a required parameter"
|
56
26
|
end
|
57
|
-
|
58
|
-
value
|
59
27
|
end
|
60
28
|
|
61
|
-
# Coerces a value to the correct type.
|
62
|
-
# @param value [Object] the value to coerce
|
63
|
-
# @raise [CoersionError] if the value cannot be coerced
|
64
29
|
def coerce(value)
|
65
30
|
case type
|
66
31
|
when :string, :boolean
|
data/lib/argy/parser.rb
CHANGED
@@ -2,26 +2,10 @@ require "optparse"
|
|
2
2
|
require "argy/help"
|
3
3
|
require "argy/option"
|
4
4
|
require "argy/argument"
|
5
|
-
require "argy/options"
|
6
5
|
|
7
6
|
module Argy
|
8
|
-
# Parses command line arguments.
|
9
7
|
class Parser
|
10
|
-
|
11
|
-
# @return [Array<String>]
|
12
|
-
attr_reader :examples
|
13
|
-
|
14
|
-
# The arguments that were declared
|
15
|
-
# @return [Array<Argument>]
|
16
|
-
attr_reader :arguments
|
17
|
-
|
18
|
-
# The options that were declared
|
19
|
-
# @return [Array<Option>]
|
20
|
-
attr_reader :options
|
21
|
-
|
22
|
-
# The flags that were declared
|
23
|
-
# @return [Array<Array(Array<String>, Proc)>]
|
24
|
-
attr_reader :flags
|
8
|
+
attr_reader :examples, :arguments, :options, :flags
|
25
9
|
|
26
10
|
def initialize
|
27
11
|
@usage = $0
|
@@ -33,131 +17,60 @@ module Argy
|
|
33
17
|
yield self if block_given?
|
34
18
|
end
|
35
19
|
|
36
|
-
# Gets or sets the usage for your program. If the
|
37
|
-
# provided usage is nil, the usage will not change.
|
38
|
-
# @param usage [String,nil] sets the usage if not nil
|
39
|
-
# @return [String] usage
|
40
|
-
# @example
|
41
|
-
# Argy.new do |o|
|
42
|
-
# o.usage "example [INPUT]"
|
43
|
-
# end
|
44
20
|
def usage(usage = nil)
|
45
21
|
@usage = usage if usage
|
46
22
|
@usage
|
47
23
|
end
|
48
24
|
|
49
|
-
# Gets or sets a description for your program. If the
|
50
|
-
# provided description is nil, the description will
|
51
|
-
# not change.
|
52
|
-
# @param description [String,nil]
|
53
|
-
# @return [String]
|
54
|
-
# @example
|
55
|
-
# Argy.new do |o|
|
56
|
-
# o.description "a really useful program"
|
57
|
-
# end
|
58
25
|
def description(description = nil)
|
59
26
|
@description = description if description
|
60
27
|
@description
|
61
28
|
end
|
62
29
|
|
63
|
-
# Adds an example
|
64
|
-
# @example
|
65
|
-
# Argy.new do |o|
|
66
|
-
# o.example "$ example foo"
|
67
|
-
# end
|
68
30
|
def example(example)
|
69
31
|
@examples << example
|
70
32
|
end
|
71
33
|
|
72
|
-
|
73
|
-
|
74
|
-
# @example
|
75
|
-
# Argy.new do |o|
|
76
|
-
# o.argument :input
|
77
|
-
# end
|
78
|
-
def argument(*args, **opts)
|
79
|
-
@arguments << Argument.new(*args, **opts)
|
34
|
+
def argument(*args)
|
35
|
+
@arguments << Argument.new(*args)
|
80
36
|
end
|
81
37
|
|
82
|
-
|
83
|
-
|
84
|
-
# @example
|
85
|
-
# Argy.new do |o|
|
86
|
-
# o.option :verbose, type: :boolean
|
87
|
-
# end
|
88
|
-
def option(*args, **opts)
|
89
|
-
@options << Option.new(*args, **opts)
|
38
|
+
def option(*args)
|
39
|
+
@options << Option.new(*args)
|
90
40
|
end
|
91
41
|
|
92
|
-
# Adds a flag
|
93
|
-
# @example
|
94
|
-
# Argy.new do |o|
|
95
|
-
# o.on "-v", "--version" do
|
96
|
-
# puts Example::VERSION
|
97
|
-
# exit
|
98
|
-
# end
|
99
|
-
# end
|
100
42
|
def on(*args, &action)
|
101
43
|
@flags << [args, action]
|
102
44
|
end
|
103
45
|
|
104
|
-
# All parameters that are defined
|
105
|
-
# @return [Array<Argument, Option>]
|
106
46
|
def parameters
|
107
47
|
arguments + options
|
108
48
|
end
|
109
49
|
|
110
|
-
# Generate help for this parser.
|
111
|
-
# @see Help#initialize
|
112
|
-
# @return [Help]
|
113
50
|
def help(**opts)
|
114
51
|
Help.new(self, **opts)
|
115
52
|
end
|
116
53
|
|
117
|
-
# Build the default values for the declared paramters.
|
118
|
-
# @return [Hash{Symbol => Object}]
|
119
54
|
def default_values
|
120
|
-
|
55
|
+
(arguments + options).reduce(unused_arguments: []) do |acc, opt|
|
121
56
|
acc[opt.name] = opt.default
|
122
57
|
acc
|
123
58
|
end
|
124
59
|
end
|
125
60
|
|
126
|
-
|
127
|
-
# @param argv [Array<String>] the command line arguments to parse
|
128
|
-
# @param strategy [Symbol,nil] can be either `:order` or `:permute`. See
|
129
|
-
# `OptionParser#order!` and `OptionParser#permute!` for more info.
|
130
|
-
# @raise [ParseError] when the arguments can't be parsed
|
131
|
-
# @return [Hash{Symbol => Object}]
|
132
|
-
def parse(argv, strategy: nil)
|
61
|
+
def parse(argv)
|
133
62
|
argv = argv.dup
|
134
63
|
values = default_values
|
135
|
-
parser = build_parser(values)
|
136
64
|
|
137
|
-
|
138
|
-
|
139
|
-
parser.order!(argv)
|
140
|
-
when :permute
|
141
|
-
parser.permute!(argv)
|
142
|
-
else
|
143
|
-
parser.parse!(argv)
|
144
|
-
end
|
65
|
+
parser = build_parser(values)
|
66
|
+
parser.parse!(argv)
|
145
67
|
|
146
68
|
populate_arguments(values, argv)
|
147
|
-
|
148
|
-
rescue OptionParser::ParseError => error
|
149
|
-
raise ParseError.new(error)
|
150
|
-
end
|
69
|
+
validate!(values)
|
151
70
|
|
152
|
-
# Validate the values
|
153
|
-
# @param values [Hash{Symbol => Object}]
|
154
|
-
# @return [Hash{Symbol => Object}]
|
155
|
-
# @raise [ValidationError] when the input is invalid
|
156
|
-
def validate!(values)
|
157
|
-
parameters.each do |param|
|
158
|
-
param.validate(values[param.name])
|
159
|
-
end
|
160
71
|
values
|
72
|
+
rescue OptionParser::MissingArgument => error
|
73
|
+
raise MissingArgumentError, error.message
|
161
74
|
end
|
162
75
|
|
163
76
|
private
|
@@ -165,13 +78,19 @@ module Argy
|
|
165
78
|
def populate_arguments(values, argv)
|
166
79
|
argv.zip(arguments).each do |value, arg|
|
167
80
|
if arg.nil?
|
168
|
-
values[:
|
81
|
+
values[:unused_arguments] << value
|
169
82
|
else
|
170
83
|
values[arg.name] = arg.coerce(value)
|
171
84
|
end
|
172
85
|
end
|
173
86
|
end
|
174
87
|
|
88
|
+
def validate!(values)
|
89
|
+
parameters.each do |param|
|
90
|
+
param.validate(values[param.name])
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
175
94
|
def build_parser(values)
|
176
95
|
OptionParser.new do |o|
|
177
96
|
options.each do |opt|
|
@@ -183,6 +102,11 @@ module Argy
|
|
183
102
|
flags.each do |flag, action|
|
184
103
|
o.on(*flag, &action)
|
185
104
|
end
|
105
|
+
|
106
|
+
o.on("-h", "--help") do
|
107
|
+
puts help
|
108
|
+
exit
|
109
|
+
end
|
186
110
|
end
|
187
111
|
end
|
188
112
|
end
|
data/lib/argy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: argy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ray Zane
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,13 +59,12 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- ".github/workflows/build.yml"
|
63
|
-
- ".github/workflows/publish.yml"
|
64
62
|
- ".gitignore"
|
65
63
|
- ".rspec"
|
66
|
-
- ".
|
64
|
+
- ".travis.yml"
|
67
65
|
- CODE_OF_CONDUCT.md
|
68
66
|
- Gemfile
|
67
|
+
- Gemfile.lock
|
69
68
|
- LICENSE.txt
|
70
69
|
- README.md
|
71
70
|
- Rakefile
|
@@ -77,16 +76,13 @@ files:
|
|
77
76
|
- lib/argy/argument.rb
|
78
77
|
- lib/argy/help.rb
|
79
78
|
- lib/argy/option.rb
|
80
|
-
- lib/argy/options.rb
|
81
79
|
- lib/argy/parameter.rb
|
82
80
|
- lib/argy/parser.rb
|
83
81
|
- lib/argy/version.rb
|
84
82
|
homepage:
|
85
83
|
licenses:
|
86
84
|
- MIT
|
87
|
-
metadata:
|
88
|
-
source_code_uri: https://github.com/rzane/argy
|
89
|
-
documentation_uri: https://rubydoc.info/github/rzane/argy
|
85
|
+
metadata: {}
|
90
86
|
post_install_message:
|
91
87
|
rdoc_options: []
|
92
88
|
require_paths:
|
@@ -102,8 +98,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
98
|
- !ruby/object:Gem::Version
|
103
99
|
version: '0'
|
104
100
|
requirements: []
|
105
|
-
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.6.14
|
106
103
|
signing_key:
|
107
104
|
specification_version: 4
|
108
|
-
summary:
|
105
|
+
summary: A fully featured option parser.
|
109
106
|
test_files: []
|
data/.github/workflows/build.yml
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
name: Build
|
2
|
-
on: [push]
|
3
|
-
jobs:
|
4
|
-
build:
|
5
|
-
runs-on: ubuntu-latest
|
6
|
-
steps:
|
7
|
-
- name: Checkout
|
8
|
-
uses: actions/checkout@v2
|
9
|
-
|
10
|
-
- name: Setup Ruby
|
11
|
-
uses: actions/setup-ruby@v1
|
12
|
-
with:
|
13
|
-
ruby-version: 2.7.x
|
14
|
-
|
15
|
-
- name: Install bundler
|
16
|
-
run: gem install bundler
|
17
|
-
|
18
|
-
- name: Install dependencies
|
19
|
-
run: bundle install
|
20
|
-
|
21
|
-
- name: Test
|
22
|
-
run: bundle exec rspec
|
@@ -1,38 +0,0 @@
|
|
1
|
-
name: Publish
|
2
|
-
on:
|
3
|
-
release:
|
4
|
-
types: [published]
|
5
|
-
jobs:
|
6
|
-
publish:
|
7
|
-
runs-on: ubuntu-latest
|
8
|
-
steps:
|
9
|
-
- name: Checkout
|
10
|
-
uses: actions/checkout@v2
|
11
|
-
|
12
|
-
- name: Setup Ruby
|
13
|
-
uses: actions/setup-ruby@v1
|
14
|
-
with:
|
15
|
-
ruby-version: 2.7.x
|
16
|
-
|
17
|
-
- name: Install bundler
|
18
|
-
run: gem install bundler
|
19
|
-
|
20
|
-
- name: Install dependencies
|
21
|
-
run: bundle install
|
22
|
-
|
23
|
-
- name: Test
|
24
|
-
run: bundle exec rspec
|
25
|
-
|
26
|
-
- name: Set version
|
27
|
-
run: perl -pi -e "s/0\.0\.0/${GITHUB_REF:11}/" lib/graphql/extras/version.rb
|
28
|
-
|
29
|
-
- name: Publish
|
30
|
-
run: |
|
31
|
-
mkdir -p $HOME/.gem
|
32
|
-
touch $HOME/.gem/credentials
|
33
|
-
chmod 0600 $HOME/.gem/credentials
|
34
|
-
printf -- "---\n:rubygems_api_key: ${RUBYGEMS_TOKEN}\n" > $HOME/.gem/credentials
|
35
|
-
gem build *.gemspec
|
36
|
-
gem push *.gem
|
37
|
-
env:
|
38
|
-
RUBYGEMS_TOKEN: ${{ secrets.RUBYGEMS_TOKEN }}
|
data/.yardopts
DELETED
data/lib/argy/options.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
module Argy
|
2
|
-
# The resulting options that were parsed from the command line.
|
3
|
-
# @example Getting a value
|
4
|
-
# options = Options.new(foo: "bar")
|
5
|
-
# options.foo #=> "bar"
|
6
|
-
# @example Querying for a value's truthiness
|
7
|
-
# options = Options.new(foo: "bar")
|
8
|
-
# options.foo? #=> true
|
9
|
-
class Options
|
10
|
-
# Create a new Options
|
11
|
-
# @param values [Hash{Symbol => Object}]
|
12
|
-
def initialize(values)
|
13
|
-
@values = values
|
14
|
-
end
|
15
|
-
|
16
|
-
# The values as a hash
|
17
|
-
# @return [Hash{Symbol => Object}]
|
18
|
-
def to_h
|
19
|
-
@values
|
20
|
-
end
|
21
|
-
|
22
|
-
# Get a value by key
|
23
|
-
# @see Hash#[]
|
24
|
-
def [](key)
|
25
|
-
@values[key]
|
26
|
-
end
|
27
|
-
|
28
|
-
# Fetch a value by key or provide a default.
|
29
|
-
# @see Hash#fetch
|
30
|
-
def fetch(*args, &block)
|
31
|
-
@values.fetch(*args, &block)
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def respond_to_missing?(meth, *)
|
37
|
-
@values.key?(meth.to_s.sub(/\?$/, "").to_sym) || super
|
38
|
-
end
|
39
|
-
|
40
|
-
def method_missing(meth, *args)
|
41
|
-
query = meth[-1] == "?"
|
42
|
-
key = query ? meth[0..-2].to_sym : meth.to_sym
|
43
|
-
|
44
|
-
return super unless @values.key?(key)
|
45
|
-
|
46
|
-
unless args.empty?
|
47
|
-
raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0)"
|
48
|
-
end
|
49
|
-
|
50
|
-
query ? !!@values[key] : @values[key]
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|