argy 0.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 +7 -0
- data/.github/workflows/build.yml +22 -0
- data/.github/workflows/publish.yml +38 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.yardopts +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +113 -0
- data/Rakefile +9 -0
- data/argy.gemspec +29 -0
- data/bin/console +14 -0
- data/bin/example +32 -0
- data/bin/setup +8 -0
- data/lib/argy.rb +51 -0
- data/lib/argy/argument.rb +12 -0
- data/lib/argy/help.rb +115 -0
- data/lib/argy/option.rb +50 -0
- data/lib/argy/options.rb +53 -0
- data/lib/argy/parameter.rb +84 -0
- data/lib/argy/parser.rb +189 -0
- data/lib/argy/version.rb +3 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ec2b0f6174b9f0028d6faea23e75dd14feaefdf78e6f75d0755340b1af6939cd
|
4
|
+
data.tar.gz: 962b784ed0a400ddd3aa447b7c21c0db27045078c057b130ffd748b99fc864f5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6afb2e4476f88bb3c6b9b56be0aaa3ecad8cf902bcce45b4cb3e121775d5d3fc4dd718b8112c70021a94a400007cc1cfbb8feca06d4779508a81d7293aa81ea0
|
7
|
+
data.tar.gz: 3111687814a6fa0358b2530cde1e3b19c89be0999903d9a999e93a6d3e24f996816629595f1c90e9d8cfa74267acd5d605a89d3363b4320e68395151d7efc967
|
@@ -0,0 +1,22 @@
|
|
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
|
@@ -0,0 +1,38 @@
|
|
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/.gitignore
ADDED
data/.rspec
ADDED
data/.yardopts
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at raymondzane@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Ray Zane
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
<h1 align="center">Argy</h1>
|
2
|
+
|
3
|
+
<div align="center">
|
4
|
+
|
5
|
+

|
6
|
+

|
7
|
+
[](https://coveralls.io/github/rzane/argy?branch=master)
|
8
|
+
|
9
|
+
</div>
|
10
|
+
|
11
|
+
Yet another command-line option parser.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'argy'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install argy
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Here's an example:
|
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`
|
79
|
+
|
80
|
+
However, Argy also supports custom option types. For example:
|
81
|
+
|
82
|
+
```ruby
|
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
|
+
```
|
100
|
+
|
101
|
+
An option type is anything that responds to `call`. So, your option type could just be a lambda.
|
102
|
+
|
103
|
+
## Contributing
|
104
|
+
|
105
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rzane/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
|
+
|
107
|
+
## License
|
108
|
+
|
109
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
110
|
+
|
111
|
+
## Code of Conduct
|
112
|
+
|
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/rzane/argy/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/argy.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "argy/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "argy"
|
7
|
+
spec.version = Argy::VERSION
|
8
|
+
spec.authors = ["Ray Zane"]
|
9
|
+
spec.email = ["raymondzane@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Yet another command-line option parser.}
|
12
|
+
spec.license = "MIT"
|
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
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
27
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "argy"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/example
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "argy"
|
5
|
+
|
6
|
+
parser = Argy.new do |o|
|
7
|
+
o.description "Prints messages"
|
8
|
+
o.usage "example"
|
9
|
+
o.example "$ example hello"
|
10
|
+
o.argument :message, desc: "message to print", required: true
|
11
|
+
o.option :loud, type: :boolean, desc: "say the message loudly"
|
12
|
+
o.option :times, type: :integer, desc: "number of times to print", default: 1
|
13
|
+
o.on "-v", "print the verison and exit" do
|
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
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
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
|
data/bin/setup
ADDED
data/lib/argy.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "argy/version"
|
2
|
+
require "argy/parser"
|
3
|
+
|
4
|
+
module Argy
|
5
|
+
# Base class for all of Argy's errors.
|
6
|
+
Error = Class.new(StandardError)
|
7
|
+
|
8
|
+
# An error that is raised when an option
|
9
|
+
# cannot be coerced to the correct type
|
10
|
+
CoersionError = Class.new(Error)
|
11
|
+
|
12
|
+
# An error that is raised when an option
|
13
|
+
# is not valid.
|
14
|
+
ValidationError = Class.new(Error)
|
15
|
+
|
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
|
+
def self.new(&block)
|
38
|
+
Argy::Parser.new(&block)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Define a parser and return the options in one go.
|
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)
|
49
|
+
new(&block).parse(argv)
|
50
|
+
end
|
51
|
+
end
|
data/lib/argy/help.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
module Argy
|
2
|
+
# Builds help information
|
3
|
+
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
|
+
def initialize(parser, column: 30, color: $stdout.tty?)
|
9
|
+
@parser = parser
|
10
|
+
@column = column
|
11
|
+
@color = color
|
12
|
+
end
|
13
|
+
|
14
|
+
# The help information
|
15
|
+
# @return [String]
|
16
|
+
def to_s
|
17
|
+
out = []
|
18
|
+
|
19
|
+
description(out)
|
20
|
+
usage(out)
|
21
|
+
examples(out)
|
22
|
+
arguments(out)
|
23
|
+
options(out)
|
24
|
+
flags(out)
|
25
|
+
|
26
|
+
out.join("\n") + "\n"
|
27
|
+
end
|
28
|
+
|
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
|
+
private
|
50
|
+
|
51
|
+
def description(out)
|
52
|
+
out << "#{parser.description}\n" if parser.description
|
53
|
+
end
|
54
|
+
|
55
|
+
def usage(out)
|
56
|
+
out << bold("USAGE")
|
57
|
+
out << " #{parser.usage}"
|
58
|
+
end
|
59
|
+
|
60
|
+
def examples(out)
|
61
|
+
out << bold("\nEXAMPLES") if parser.examples.any?
|
62
|
+
out.concat parser.examples.map { |ex| " #{ex}" }
|
63
|
+
end
|
64
|
+
|
65
|
+
def arguments(out)
|
66
|
+
out << bold("\nARGUMENTS") if parser.arguments.any?
|
67
|
+
out.concat parser.arguments.map { |a| argument(a) }
|
68
|
+
end
|
69
|
+
|
70
|
+
def argument(a)
|
71
|
+
entry(a.label, desc: a.desc, required: a.required?, default: a.default)
|
72
|
+
end
|
73
|
+
|
74
|
+
def options(out)
|
75
|
+
out << bold("\nOPTIONS") if parser.options.any?
|
76
|
+
out.concat parser.options.map { |o| option(o) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def option(o)
|
80
|
+
label = [option_label(o.label, o.type)]
|
81
|
+
label += o.aliases.map { |a| option_label(a, o.type) }
|
82
|
+
entry(label.join(", "), desc: o.desc, required: o.required?, default: o.default)
|
83
|
+
end
|
84
|
+
|
85
|
+
def flags(out)
|
86
|
+
out << bold("\nFLAGS") if parser.flags.any?
|
87
|
+
out.concat parser.flags.map { |f, _| flag(f) }
|
88
|
+
end
|
89
|
+
|
90
|
+
def flag(flag)
|
91
|
+
flag = flag.dup
|
92
|
+
desc = flag.pop unless flag.last.match?(/^-/)
|
93
|
+
entry(flag.reverse.join(", "), desc: desc)
|
94
|
+
end
|
95
|
+
|
96
|
+
def option_label(label, type)
|
97
|
+
return label if type == :boolean
|
98
|
+
label.start_with?("--") ? "#{label}=VALUE" : "#{label} VALUE"
|
99
|
+
end
|
100
|
+
|
101
|
+
def bold(text)
|
102
|
+
color? ? "\e[1m#{text}\e[0m" : text
|
103
|
+
end
|
104
|
+
|
105
|
+
def dim(text)
|
106
|
+
color? ? "\e[2m#{text}\e[0m" : text
|
107
|
+
end
|
108
|
+
|
109
|
+
def color?
|
110
|
+
@color
|
111
|
+
end
|
112
|
+
|
113
|
+
attr_reader :parser, :column
|
114
|
+
end
|
115
|
+
end
|
data/lib/argy/option.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require "argy/parameter"
|
2
|
+
|
3
|
+
module Argy
|
4
|
+
# An option to be parsed from the command line
|
5
|
+
class Option < Parameter
|
6
|
+
# A list of alternative flags
|
7
|
+
# @return [Array<String>]
|
8
|
+
attr_reader :aliases
|
9
|
+
|
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
|
+
def initialize(*args, aliases: [], **opts)
|
18
|
+
super(*args, **opts)
|
19
|
+
@aliases = aliases
|
20
|
+
end
|
21
|
+
|
22
|
+
# The display label for the argument
|
23
|
+
# @return [String]
|
24
|
+
def label
|
25
|
+
case type
|
26
|
+
when :boolean
|
27
|
+
"--[no-]#{name.to_s.tr("_", "-")}"
|
28
|
+
else
|
29
|
+
"--#{name.to_s.tr("_", "-")}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# @private
|
34
|
+
def to_option_parser
|
35
|
+
options = []
|
36
|
+
options << aliases.join(" ") unless aliases.empty?
|
37
|
+
|
38
|
+
case type
|
39
|
+
when :boolean
|
40
|
+
options << label
|
41
|
+
else
|
42
|
+
options << "#{label}=#{name.to_s.upcase}"
|
43
|
+
options << "#{label} #{name.to_s.upcase}"
|
44
|
+
end
|
45
|
+
|
46
|
+
options << desc if desc
|
47
|
+
options
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/argy/options.rb
ADDED
@@ -0,0 +1,53 @@
|
|
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
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require "pathname"
|
2
|
+
require "argy/parameter"
|
3
|
+
|
4
|
+
module Argy
|
5
|
+
# @abstract Subclasses must implement {#label}
|
6
|
+
class Parameter
|
7
|
+
# The name of the parameter
|
8
|
+
# @return [String]
|
9
|
+
attr_reader :name
|
10
|
+
|
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
|
+
def initialize(name, desc: nil, type: :string, default: nil, required: false)
|
30
|
+
@name = name
|
31
|
+
@type = type
|
32
|
+
@desc = desc
|
33
|
+
@default = default
|
34
|
+
@required = required
|
35
|
+
end
|
36
|
+
|
37
|
+
# The display label for the paramter
|
38
|
+
# @abstract
|
39
|
+
# @return [String]
|
40
|
+
def label
|
41
|
+
raise NotImplementedError, __method__
|
42
|
+
end
|
43
|
+
|
44
|
+
# Check if the parameter is required
|
45
|
+
# @return [TrueClass,FalseClass]
|
46
|
+
def required?
|
47
|
+
@required
|
48
|
+
end
|
49
|
+
|
50
|
+
# Validates a value.
|
51
|
+
# @return [Object] the value
|
52
|
+
# @raise [ValidationError] if the valid is invalid
|
53
|
+
def validate(value)
|
54
|
+
if required? && value.nil?
|
55
|
+
raise ValidationError, "`#{label}` is a required parameter"
|
56
|
+
end
|
57
|
+
|
58
|
+
value
|
59
|
+
end
|
60
|
+
|
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
|
+
def coerce(value)
|
65
|
+
case type
|
66
|
+
when :string, :boolean
|
67
|
+
value
|
68
|
+
when :integer
|
69
|
+
Integer(value)
|
70
|
+
when :float
|
71
|
+
Float(value)
|
72
|
+
when :array
|
73
|
+
value.split(",")
|
74
|
+
when :pathname
|
75
|
+
Pathname.new(value).expand_path(Dir.pwd)
|
76
|
+
else
|
77
|
+
raise "Invalid type: #{type.inspect}" unless type.respond_to?(:call)
|
78
|
+
type.call(value)
|
79
|
+
end
|
80
|
+
rescue ArgumentError
|
81
|
+
raise CoersionError, "`#{label}` received an invalid value"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/argy/parser.rb
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
require "optparse"
|
2
|
+
require "argy/help"
|
3
|
+
require "argy/option"
|
4
|
+
require "argy/argument"
|
5
|
+
require "argy/options"
|
6
|
+
|
7
|
+
module Argy
|
8
|
+
# Parses command line arguments.
|
9
|
+
class Parser
|
10
|
+
# The examples that were declared
|
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
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@usage = $0
|
28
|
+
@description = nil
|
29
|
+
@arguments = []
|
30
|
+
@options = []
|
31
|
+
@flags = []
|
32
|
+
@examples = []
|
33
|
+
yield self if block_given?
|
34
|
+
end
|
35
|
+
|
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
|
+
def usage(usage = nil)
|
45
|
+
@usage = usage if usage
|
46
|
+
@usage
|
47
|
+
end
|
48
|
+
|
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
|
+
def description(description = nil)
|
59
|
+
@description = description if description
|
60
|
+
@description
|
61
|
+
end
|
62
|
+
|
63
|
+
# Adds an example
|
64
|
+
# @example
|
65
|
+
# Argy.new do |o|
|
66
|
+
# o.example "$ example foo"
|
67
|
+
# end
|
68
|
+
def example(example)
|
69
|
+
@examples << example
|
70
|
+
end
|
71
|
+
|
72
|
+
# Adds an argument
|
73
|
+
# @see Argument#initialize
|
74
|
+
# @example
|
75
|
+
# Argy.new do |o|
|
76
|
+
# o.argument :input
|
77
|
+
# end
|
78
|
+
def argument(*args, **opts)
|
79
|
+
@arguments << Argument.new(*args, **opts)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Adds an option
|
83
|
+
# @see Option#initialize
|
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)
|
90
|
+
end
|
91
|
+
|
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
|
+
def on(*args, &action)
|
101
|
+
@flags << [args, action]
|
102
|
+
end
|
103
|
+
|
104
|
+
# All parameters that are defined
|
105
|
+
# @return [Array<Argument, Option>]
|
106
|
+
def parameters
|
107
|
+
arguments + options
|
108
|
+
end
|
109
|
+
|
110
|
+
# Generate help for this parser.
|
111
|
+
# @see Help#initialize
|
112
|
+
# @return [Help]
|
113
|
+
def help(**opts)
|
114
|
+
Help.new(self, **opts)
|
115
|
+
end
|
116
|
+
|
117
|
+
# Build the default values for the declared paramters.
|
118
|
+
# @return [Hash{Symbol => Object}]
|
119
|
+
def default_values
|
120
|
+
parameters.reduce(unused_args: []) do |acc, opt|
|
121
|
+
acc[opt.name] = opt.default
|
122
|
+
acc
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Build the default values for the declared paramters.
|
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)
|
133
|
+
argv = argv.dup
|
134
|
+
values = default_values
|
135
|
+
parser = build_parser(values)
|
136
|
+
|
137
|
+
case strategy
|
138
|
+
when :order
|
139
|
+
parser.order!(argv)
|
140
|
+
when :permute
|
141
|
+
parser.permute!(argv)
|
142
|
+
else
|
143
|
+
parser.parse!(argv)
|
144
|
+
end
|
145
|
+
|
146
|
+
populate_arguments(values, argv)
|
147
|
+
Options.new validate!(values)
|
148
|
+
rescue OptionParser::ParseError => error
|
149
|
+
raise ParseError.new(error)
|
150
|
+
end
|
151
|
+
|
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
|
+
values
|
161
|
+
end
|
162
|
+
|
163
|
+
private
|
164
|
+
|
165
|
+
def populate_arguments(values, argv)
|
166
|
+
argv.zip(arguments).each do |value, arg|
|
167
|
+
if arg.nil?
|
168
|
+
values[:unused_args] << value
|
169
|
+
else
|
170
|
+
values[arg.name] = arg.coerce(value)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def build_parser(values)
|
176
|
+
OptionParser.new do |o|
|
177
|
+
options.each do |opt|
|
178
|
+
o.on(*opt.to_option_parser) do |value|
|
179
|
+
values[opt.name] = opt.coerce(value)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
flags.each do |flag, action|
|
184
|
+
o.on(*flag, &action)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
data/lib/argy/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: argy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ray Zane
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- raymondzane@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".github/workflows/build.yml"
|
63
|
+
- ".github/workflows/publish.yml"
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".yardopts"
|
67
|
+
- CODE_OF_CONDUCT.md
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- argy.gemspec
|
73
|
+
- bin/console
|
74
|
+
- bin/example
|
75
|
+
- bin/setup
|
76
|
+
- lib/argy.rb
|
77
|
+
- lib/argy/argument.rb
|
78
|
+
- lib/argy/help.rb
|
79
|
+
- lib/argy/option.rb
|
80
|
+
- lib/argy/options.rb
|
81
|
+
- lib/argy/parameter.rb
|
82
|
+
- lib/argy/parser.rb
|
83
|
+
- lib/argy/version.rb
|
84
|
+
homepage:
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata:
|
88
|
+
source_code_uri: https://github.com/rzane/argy
|
89
|
+
documentation_uri: https://rubydoc.info/github/rzane/argy
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubygems_version: 3.1.2
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Yet another command-line option parser.
|
109
|
+
test_files: []
|