sprinkles-opts 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aa12d8ee5410cdbd3e47d4f1b2e1cec293b9097208dbbaae043d7804da3153cf
4
+ data.tar.gz: 3ae63c4d2bc388276778ae46809571f4b4e975a90f2618ab7ce73427245fc67b
5
+ SHA512:
6
+ metadata.gz: fa177fc8e6752839665457657eff697776e12324dfab2ce47b0f59be0c28729346ec246818a4c135cea07ca7b4aefb6b5da2ccb12ca513c72324751751e37ecf
7
+ data.tar.gz: 63963328fb25de91205f032fc86332b1dc25066b5cc8bf2756e8cf7300e4af330eabe17a4448bd69104fa6bbcea6512cae6494139f9090ff20d93c431845c396
@@ -0,0 +1,37 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ main ]
13
+ pull_request:
14
+ branches: [ main ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.6', '2.7']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - name: Set up Ruby
27
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
+ # uses: ruby/setup-ruby@v1
30
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
31
+ with:
32
+ ruby-version: ${{ matrix.ruby-version }}
33
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
+ - name: Run tests
35
+ run: bundle exec rake
36
+ - name: Typecheck
37
+ run: bundle exec srb tc
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *~
@@ -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 tristero@infinitenegativeutility.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
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sprinkles-defaultdict.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sprinkles-opts (0.1.0)
5
+ sorbet-runtime
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ minitest (5.14.4)
12
+ parallel (1.20.1)
13
+ parser (3.0.1.1)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.0.0)
16
+ rake (10.5.0)
17
+ regexp_parser (2.1.1)
18
+ rexml (3.2.5)
19
+ rubocop (1.17.0)
20
+ parallel (~> 1.10)
21
+ parser (>= 3.0.0.0)
22
+ rainbow (>= 2.2.2, < 4.0)
23
+ regexp_parser (>= 1.8, < 3.0)
24
+ rexml
25
+ rubocop-ast (>= 1.7.0, < 2.0)
26
+ ruby-progressbar (~> 1.7)
27
+ unicode-display_width (>= 1.4.0, < 3.0)
28
+ rubocop-ast (1.7.0)
29
+ parser (>= 3.0.1.1)
30
+ ruby-progressbar (1.11.0)
31
+ sorbet (0.5.6346)
32
+ sorbet-static (= 0.5.6346)
33
+ sorbet-runtime (0.5.6433)
34
+ sorbet-static (0.5.6346-universal-darwin-14)
35
+ unicode-display_width (2.0.0)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler (~> 1.17)
42
+ minitest (~> 5.0)
43
+ rake (~> 10.0)
44
+ rubocop
45
+ sorbet
46
+ sprinkles-opts!
47
+
48
+ BUNDLED WITH
49
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Getty Ritter
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,137 @@
1
+ # sprinkles-opts
2
+
3
+ The `sprinkles-opts` library is a convenient Sorbet-aware way of doing
4
+ argument parsing.
5
+
6
+ ## Basic usage
7
+
8
+ Create a class that is a subclass of `Spinkles::Opts::GetOpt`. Define fields and their types with `const`, analogously to [how you would with `T::Struct`](), but those fields can have `short:` and `long:` options that map to the flags used to provide them. You'll also have to provide a value for `program_name` by overriding an abstract method:
9
+
10
+ ```ruby
11
+ class MyOptions < Sprinkles::Opts::GetOpt
12
+ sig {override.returns(String)}
13
+ def self.program_name; "my-program"; end
14
+
15
+ const :input, String, short: 'i', long: 'input'
16
+ const :num_iterations, Integer, short: 'n', placeholder: 'N'
17
+ const :verbose, T::Boolean, short: 'v', long: 'verbose', factory: -> {false}
18
+ end
19
+ ```
20
+
21
+ You can then call `MyOptions.parse(ARGV)` in order to get a value of type `MyOptions` with the defined fields initialized.
22
+
23
+ ```ruby
24
+ opts = MyOptions.parse(%w{-i foo -n 8 --verbose})
25
+ assert_equal('foo', opts.input)
26
+ assert_equal(8, opts.num_iterations)
27
+ assert_equal(true, opts.verbose)
28
+ ```
29
+
30
+ The field type will affect the behavior of the option parser. Fields whose type is `T::Boolean` are implicitly treated as flags that do not take more arguments, and a `T::Boolean` field with a long argument name like `--foo` will also automatically get a corresponding `--no-foo` which sets the flag to false. Values with other built-in types like `Symbol` or `Integer` will be converted to the appropriate type.
31
+
32
+ Fields without a `short:` or `long:` parameter will be understood to be positional arguments. Ordering is important here: positional arguments will be filled in the order that they appear in the definition.
33
+
34
+ ```ruby
35
+ class PosOptions < Sprinkles::Opts::GetOpt
36
+ sig {override.returns(String)}
37
+ def self.program_name; "positional-options"; end
38
+
39
+ const :source, String
40
+ const :destination, String
41
+ end
42
+
43
+ opts = PosOptions.parse(%w{this that})
44
+ assert_equal('this', opts.source)
45
+ assert_equal(that, opts.destination)
46
+ ```
47
+
48
+ Parsing will fail and exit the program with a usage statement if either too many or too few positional parameters are provided.
49
+
50
+ ```ruby
51
+ opts = PosOptions.parse(%w{this})
52
+ # this will exit and print the following:
53
+ # Not enough arguments!
54
+ # Usage: positional-options SOURCE DESTINATION
55
+ # -h, --help Prints this help
56
+ ```
57
+
58
+ ## Optional arguments
59
+
60
+ There are two ways of making arguments optional:
61
+ - A field whose type is marked as `T.nilable` will implicitly be initialized as `nil` if it is not provided.
62
+ - A field can have a `factory:` which should be a lambda that will be called to initialize the field if the argument is not provided.
63
+
64
+ Fields that are not `T.nilable` and do not have a `factory:` must be provided when parsing arguments.
65
+
66
+ For _positional_ arguments, there's currently an extra restriction: all mandatory positional arguments _must come first_, and will throw a definition-time error if they appear later. This means that positional parameters are matched in-order as they appear, and once we're out of positional parameters the remaining optional parameters will be initialized to `nil` (for `T.nilable` fields) or their provided defaults (if they have a `factory:` parameter.)
67
+
68
+ ```ruby
69
+ class PosOptions < Sprinkles::Opts::GetOpt
70
+ sig {override.returns(String)}
71
+ def self.program_name; "positional-options"; end
72
+
73
+ const :a, String
74
+ const :b, T.nilable(String)
75
+ const :c, T.nilable(String)
76
+ end
77
+
78
+ PosOptions.parse(%w{1 2 3}) # a is 1, b is 2, c is 3
79
+ PosOptions.parse(%w{1 2}) # a is 1, b is 2, c is nil
80
+ PosOptions.parse(%w{1}) # a is 1, b is nil, c is nil
81
+ ```
82
+
83
+ It is still an error to pass too few positional parameters (i.e. fewer than there are mandatory positional parameters) or too many (i.e. more than there are total positional parameters, mandatory and optional).
84
+
85
+ ## Repeated arguments
86
+
87
+ Fields whose types are either `T::Array[...]` or `T::Set[...]` are implicitly treated as repeated fields.
88
+
89
+ When a positional field has type `T::Array[...]` or `T::Set[...]`, then it is subject to two major restrictions:
90
+ - It must be the last positional field specified, which also implies that it must be the _only_ repeated positional field.
91
+ - None of the other fields can be optional.
92
+
93
+ The second restriction is because of the ambiguity as to where extra fields go when choosing how to fill in optional fields, but may eventually be lifted in the future.
94
+
95
+ When a positional field is `T::Array[...]` or `T::Set[...]`, then any trailing arguments will be added to the array contained in that field. For example:
96
+
97
+ ```ruby
98
+ class PosArray < Sprinkles::Opts::GetOpt
99
+ const :a, Integer
100
+ const :b, T::Array[String]
101
+ end
102
+
103
+ PosArray.parse(%w{1}) # a is 1, b is []
104
+ PosArray.parse(%w{1 2}) # a is 1, b is ['2']
105
+ PosArray.parse(%w{1 2 3}) # a is 1, b is ['2', '3']
106
+ ```
107
+
108
+ When a non-positional field is `T::Array[...]` or `T::Set[...]`, then it can be specified multiple times (in any order) to add to that collection. For example:
109
+
110
+ ```ruby
111
+ class OptArray < Sprinkles::Opts::GetOpt
112
+ const :a, T::Array[Integer]
113
+ end
114
+
115
+ PosArray.parse(%w{}) # a is []
116
+ PosArray.parse(%w{-a 5}) # a is [5]
117
+ PosArray.parse(%w{-a 22 -a 33}) # a is [22, 33]
118
+ ```
119
+
120
+ ## Help text and descriptions
121
+
122
+ The option names `-h` and `--help` are reserved, and when they are provided the program will print a usage panel and exit:
123
+
124
+ ```
125
+ Usage: my-program --input=VALUE -nN
126
+ -h, --help Print this help
127
+ -i, --input=VALUE
128
+ -nN
129
+ -v, --[no-]verbose
130
+ ```
131
+
132
+ Individual fields can customize their default placeholder text away from the default `VALUE` using the `placeholder:` argument, and can provide more extensive descriptions using the `description:` argument.
133
+
134
+
135
+ ## Why sprinkles?
136
+
137
+ Well, because it's a Sorbet topping. I have other unfinished ideas for how to leverage Sorbet to write certain abstractions, and my thought was that it might be nice to put them in a common namespace.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,8 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Sprinkles
5
+ module Opts
6
+ VERSION = '0.1.0'
7
+ end
8
+ end