samovar 2.4.0 → 2.4.1

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
  SHA256:
3
- metadata.gz: 85e0df9ca2ed86284ab62c49ef724f93818ecb8571a33cccca98759f0a6b2612
4
- data.tar.gz: 833c98f6619c7fb73243f6bbbbbb84e25a8d100c03af55392bd97f20a4246803
3
+ metadata.gz: e1051db069257cf9d25e3599a5ddb5c5547a921cb90d04552a777d3af5c335b2
4
+ data.tar.gz: 632d0819eb51db7f9640feb8491e543706721a4313243527bb5380da576a578b
5
5
  SHA512:
6
- metadata.gz: 0aff1d4bc6f8c2154dcd7e216a8f082fc50b44239321a122b139edff54e0749ab7109a45ab87e2615677e181426717b4912d81118f87f01cd4bf7032653d2041
7
- data.tar.gz: d3c94b2986da89eba71d18d06df18dd0541775f2f1c2ba2c327e8407a38e05757e073b5f97c137c8c0241ab29811fd9e0a3fed3c2613034ca7db15ef0e5aff12
6
+ metadata.gz: f5daa04c5dff0500f95c9dec3cabe02bbbb10fd5a9d5a06f89f6d9e6687eb11af89325472df628170ea04d0458d3c8600849c4fab1299c83bf523e58e1e268ed
7
+ data.tar.gz: 304cc7d54293e3998dae9e3e3247506db3f82d2d0f6d66e988b4ad21cad0150846a1325a039c4dd3f4746fe0903e6e6afd5a6e2f615d724d7e7ee788da49310b
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,215 @@
1
+ # Getting Started
2
+
3
+ This guide explains how to use `samovar` to build command-line tools and applications.
4
+
5
+ ## Installation
6
+
7
+ Add the gem to your project:
8
+
9
+ ~~~ bash
10
+ $ bundle add samovar
11
+ ~~~
12
+
13
+ Or install it yourself as:
14
+
15
+ ~~~ bash
16
+ $ gem install samovar
17
+ ~~~
18
+
19
+ ## Core Concepts
20
+
21
+ Samovar provides a declarative class-based DSL for building command-line parsers. The main concepts include:
22
+
23
+ - **Commands**: Classes that represent specific functions in your program, inheriting from {ruby Samovar::Command}.
24
+ - **Options**: Command-line flags and arguments that can be parsed using the `options` block.
25
+ - **Nested Commands**: Sub-commands that can be composed using the `nested` method.
26
+ - **Tokens**: Positional arguments parsed using `one` and `many` methods.
27
+ - **Splits**: Separating arguments at a specific delimiter (e.g., `--`) using the `split` method.
28
+
29
+ ## Usage
30
+
31
+ Create `Command` classes that represent specific functions in your program. The top level command might look something like this:
32
+
33
+ ~~~ ruby
34
+ require "samovar"
35
+
36
+ class List < Samovar::Command
37
+ self.description = "List the current directory"
38
+
39
+ def call
40
+ system("ls -lah")
41
+ end
42
+ end
43
+
44
+ class Application < Samovar::Command
45
+ options do
46
+ option "--help", "Do you need help?"
47
+ end
48
+
49
+ nested :command, {
50
+ "list" => List
51
+ }, default: "list"
52
+
53
+ def call
54
+ if @options[:help]
55
+ self.print_usage
56
+ else
57
+ @command.call
58
+ end
59
+ end
60
+ end
61
+
62
+ Application.call # Defaults to ARGV.
63
+ ~~~
64
+
65
+ ### Basic Options
66
+
67
+ Options allow you to parse command-line flags and arguments:
68
+
69
+ ~~~ ruby
70
+ require "samovar"
71
+
72
+ class Application < Samovar::Command
73
+ options do
74
+ option "-f/--frobulate <text>", "Frobulate the text"
75
+ option "-x | -y", "Specify either x or y axis.", key: :axis
76
+ option "-F/--yeah/--flag", "A boolean flag with several forms."
77
+ option "--things <a,b,c>", "A list of things" do |value|
78
+ value.split(/\s*,\s*/)
79
+ end
80
+ end
81
+ end
82
+
83
+ application = Application.new(["-f", "Algebraic!"])
84
+ application.options[:frobulate] # 'Algebraic!'
85
+
86
+ application = Application.new(["-x", "-y"])
87
+ application.options[:axis] # :y
88
+
89
+ application = Application.new(["-F"])
90
+ application.options[:flag] # true
91
+
92
+ application = Application.new(["--things", "x,y,z"])
93
+ application.options[:things] # ['x', 'y', 'z']
94
+ ~~~
95
+
96
+ ### Nested Commands
97
+
98
+ You can create sub-commands that are nested within your main application:
99
+
100
+ ~~~ ruby
101
+ require "samovar"
102
+
103
+ class Create < Samovar::Command
104
+ def invoke(parent)
105
+ puts "Creating"
106
+ end
107
+ end
108
+
109
+ class Application < Samovar::Command
110
+ nested "<command>",
111
+ "create" => Create
112
+
113
+ def invoke(program_name: File.basename($0))
114
+ if @command
115
+ @command.invoke
116
+ else
117
+ print_usage(program_name)
118
+ end
119
+ end
120
+ end
121
+
122
+ Application.new(["create"]).invoke
123
+ ~~~
124
+
125
+ ### ARGV Splits
126
+
127
+ You can split arguments at a delimiter (typically `--`):
128
+
129
+ ~~~ ruby
130
+ require "samovar"
131
+
132
+ class Application < Samovar::Command
133
+ many :packages
134
+ split :argv
135
+ end
136
+
137
+ application = Application.new(["foo", "bar", "baz", "--", "apples", "oranges", "feijoas"])
138
+ application.packages # ['foo', 'bar', 'baz']
139
+ application.argv # ['apples', 'oranges', 'feijoas']
140
+ ~~~
141
+
142
+ ### Parsing Tokens
143
+
144
+ You can parse positional arguments using `one` and `many`:
145
+
146
+ ~~~ ruby
147
+ require "samovar"
148
+
149
+ class Application < Samovar::Command
150
+ self.description = "Mix together your favorite things."
151
+
152
+ one :fruit, "Name one fruit"
153
+ many :cakes, "Any cakes you like"
154
+ end
155
+
156
+ application = Application.new(["apple", "chocolate cake", "fruit cake"])
157
+ application.fruit # 'apple'
158
+ application.cakes # ['chocolate cake', 'fruit cake']
159
+ ~~~
160
+
161
+ ### Explicit Commands
162
+
163
+ Given a custom `Samovar::Command` subclass, you can instantiate it with options:
164
+
165
+ ~~~ ruby
166
+ application = Application["--root", path]
167
+ ~~~
168
+
169
+ You can also duplicate an existing command instance with additions/changes:
170
+
171
+ ~~~ ruby
172
+ concurrent_application = application["--threads", 12]
173
+ ~~~
174
+
175
+ These forms can be useful when invoking one command from another, or in unit tests.
176
+
177
+ ## Error Handling
178
+
179
+ Samovar provides two entry points with different error handling behaviors:
180
+
181
+ ### `call()` - High-Level Entry Point
182
+
183
+ Use `call()` for CLI applications. It handles parsing errors gracefully by printing usage information:
184
+
185
+ ~~~ ruby
186
+ # Automatically handles errors and prints usage
187
+ Application.call(ARGV)
188
+ ~~~
189
+
190
+ If parsing fails or the command raises a {ruby Samovar::Error}, it will:
191
+ - Print the usage information with the error highlighted
192
+ - Return `nil` instead of raising an exception
193
+
194
+ ### `parse()` - Low-Level Parsing
195
+
196
+ Use `parse()` when you need explicit error handling or for testing:
197
+
198
+ ~~~ ruby
199
+ begin
200
+ app = Application.parse(["--unknown-flag"])
201
+ rescue Samovar::InvalidInputError => error
202
+ # Handle the error yourself
203
+ puts "Invalid input: #{error.message}"
204
+ end
205
+ ~~~
206
+
207
+ The `parse()` method raises exceptions on parsing errors, giving you full control over error handling.
208
+
209
+ ### Error Types
210
+
211
+ Samovar defines several error types:
212
+
213
+ - {ruby Samovar::InvalidInputError}: Raised when unexpected command-line input is encountered
214
+ - {ruby Samovar::MissingValueError}: Raised when required arguments or options are missing
215
+
@@ -0,0 +1,14 @@
1
+ # Automatically generated context index for Utopia::Project guides.
2
+ # Do not edit then files in this directory directly, instead edit the guides and then run `bake utopia:project:agent:context:update`.
3
+ ---
4
+ description: Samovar is a flexible option parser excellent support for sub-commands
5
+ and help documentation.
6
+ metadata:
7
+ documentation_uri: https://ioquatix.github.io/samovar/
8
+ funding_uri: https://github.com/sponsors/ioquatix/
9
+ source_code_uri: https://github.com/ioquatix/samovar.git
10
+ files:
11
+ - path: getting-started.md
12
+ title: Getting Started
13
+ description: This guide explains how to use `samovar` to build command-line tools
14
+ and applications.
@@ -6,5 +6,5 @@
6
6
 
7
7
  # @namespace
8
8
  module Samovar
9
- VERSION = "2.4.0"
9
+ VERSION = "2.4.1"
10
10
  end
data/readme.md CHANGED
@@ -18,6 +18,30 @@ Please see the [project documentation](https://ioquatix.github.io/samovar/) for
18
18
 
19
19
  - [Getting Started](https://ioquatix.github.io/samovar/guides/getting-started/index) - This guide explains how to use `samovar` to build command-line tools and applications.
20
20
 
21
+ ## Releases
22
+
23
+ Please see the [project releases](https://ioquatix.github.io/samovar/releases/index) for all releases.
24
+
25
+ ### v2.4.1
26
+
27
+ ### v2.4.0
28
+
29
+ - Fix option parsing and validation: required options are now detected correctly and raise `Samovar::MissingValueError` when missing.
30
+ - Fix flag value parsing: flags that expect a value no longer consume a following flag as their value (e.g. `--config <path>` will not consume `--verbose`).
31
+ - Usage improvements: required options are marked as `(required)` in usage output.
32
+
33
+ ### v2.3.0
34
+
35
+ - Add support for `--[no]-thing` explicit boolean flags, allowing users to explicitly enable or disable boolean options.
36
+
37
+ ### v2.2.0
38
+
39
+ - Add support for explicit output: commands can now specify an output stream (e.g. `STDOUT`, `STDERR`, or custom IO objects) via the `output:` parameter in `Command.call`.
40
+
41
+ ### v2.1.4
42
+
43
+ - `Command#to_s` now returns the class name by default, improving debugging and introspection.
44
+
21
45
  ## See Also
22
46
 
23
47
  - [Teapot](https://github.com/ioquatix/teapot/blob/master/lib/teapot/command.rb) is a build system and uses multiple top-level commands.
data/releases.md ADDED
@@ -0,0 +1,21 @@
1
+ # Releases
2
+
3
+ ## v2.4.1
4
+
5
+ ## v2.4.0
6
+
7
+ - Fix option parsing and validation: required options are now detected correctly and raise `Samovar::MissingValueError` when missing.
8
+ - Fix flag value parsing: flags that expect a value no longer consume a following flag as their value (e.g. `--config <path>` will not consume `--verbose`).
9
+ - Usage improvements: required options are marked as `(required)` in usage output.
10
+
11
+ ## v2.3.0
12
+
13
+ - Add support for `--[no]-thing` explicit boolean flags, allowing users to explicitly enable or disable boolean options.
14
+
15
+ ## v2.2.0
16
+
17
+ - Add support for explicit output: commands can now specify an output stream (e.g. `STDOUT`, `STDERR`, or custom IO objects) via the `output:` parameter in `Command.call`.
18
+
19
+ ## v2.1.4
20
+
21
+ - `Command#to_s` now returns the class name by default, improving debugging and introspection.
data.tar.gz.sig CHANGED
@@ -1,4 +1,4 @@
1
- ~�I|!5<�P�*��oA��[L��ʏHT7!5���'��8��H��vBP�����P��
2
- mf`TQ����Dzbz����8
3
- @m0��� %���
4
- P�2����:!j�Ɨqk,FLmQER�GEY��T��?,x+����[OG� �{<ՀD;K��i�_��8g�t5൮��Ս�`�L��\!�]{��*٦d!/�]B3ί�L�sc���������nN�֢�a�ݙ�1��WZ�.�/S�@����k�ͫ6 BAA�7��İ�OcT�`y��AN����"USa�#�V�P!R)RU;)����" RxAhP��x�7�J�[_���%
1
+ �@/8
2
+ p���c\ld�?��8Bdÿ��ɹut&+�s�
3
+ ���HBT��p���}�•�f;����4�Z)#�xV��af D�SH���k'�c˭z���o���m���8��'ӓ�g��[s���2ԧMB�5U�Ho �Bʉ���S�*ր����,��v�@E�^�u1<[�^&��o
4
+ p�j}x2mV/2� �� �7��<�2���PEs�ϙEH8j��u����֝��_���)�&b���rD���@O�� g����G<��2SehMv�ލ����v/+ƀ�w��,�
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samovar
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -71,6 +71,8 @@ executables: []
71
71
  extensions: []
72
72
  extra_rdoc_files: []
73
73
  files:
74
+ - context/getting-started.md
75
+ - context/index.yaml
74
76
  - lib/samovar.rb
75
77
  - lib/samovar/command.rb
76
78
  - lib/samovar/error.rb
@@ -92,6 +94,7 @@ files:
92
94
  - lib/samovar/version.rb
93
95
  - license.md
94
96
  - readme.md
97
+ - releases.md
95
98
  homepage: https://github.com/ioquatix/samovar
96
99
  licenses:
97
100
  - MIT
metadata.gz.sig CHANGED
Binary file