usage_by_example 0.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -21
- data/lib/usage_by_example/version.rb +27 -1
- data/lib/usage_by_example.rb +21 -8
- metadata +3 -6
- data/.rspec +0 -3
- data/Gemfile +0 -8
- data/Gemfile.lock +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c62f93055ec627514c29cf3c8c1415f09eeb300c99e78f05f16a73bfae365c69
|
4
|
+
data.tar.gz: 5b9d24119f0ad3390a7a9b995a6e93540e0157c2c0a06e2e4453b94c42f8db4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac405ec17ecf15f372b3849c18e29598d56237f625e01906a85afb528c99273836b305873901cbcd2cf2869e907c3085a7225fc43478697d06814bafe8be5dea
|
7
|
+
data.tar.gz: b334f114b4aab13a32e07a700104215075d718efc93589859ca969f69ca21a9f93edc0ee872b0b201ceedc6f3bea4e2218f21d0dbd5d175cd70e9a785c13211d
|
data/README.md
CHANGED
@@ -1,35 +1,48 @@
|
|
1
|
-
#
|
1
|
+
# Usage by Example
|
2
2
|
|
3
|
-
|
3
|
+
No-code options parser that automatically detects command-line options from the usage text of your application. This intuitive parser identifies optional and required argument names as well as option names without requiring any additional code, making it easy to manage user input for your command-line applications.
|
4
4
|
|
5
|
-
|
5
|
+
Features
|
6
6
|
|
7
|
-
|
7
|
+
- Automatically detects optional and required argument names from usage text
|
8
|
+
- Automatically detects option names and associated arguments (if any) from usage text
|
9
|
+
- Parses those arguments and options from the command line (ARGV)
|
10
|
+
- Raises errors for unknown options or missing required arguments
|
8
11
|
|
9
|
-
Add this line to your application's Gemfile:
|
10
12
|
|
11
|
-
|
12
|
-
gem 'usage_by_example'
|
13
|
-
```
|
14
|
-
|
15
|
-
And then execute:
|
13
|
+
Example
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
Or install it yourself as:
|
15
|
+
```ruby
|
16
|
+
require 'usage_by_example'
|
20
17
|
|
21
|
-
|
18
|
+
Options = UsageByExample.read(DATA).parse(ARGV)
|
22
19
|
|
23
|
-
|
20
|
+
puts Options.include_secure?
|
21
|
+
puts Options.include_verbose?
|
22
|
+
puts Options.include_retries?
|
23
|
+
puts Options.include_timeout?
|
24
|
+
puts Options.argument_retries
|
25
|
+
puts Options.argument_timeout
|
26
|
+
puts Options.argument_mode
|
27
|
+
puts Options.argument_host
|
28
|
+
puts Options.argument_port
|
24
29
|
|
25
|
-
TODO: Write usage instructions here
|
26
30
|
|
27
|
-
|
31
|
+
__END__
|
32
|
+
Establishes a network connection to a designated host and port, enabling
|
33
|
+
users to assess network connectivity and diagnose potential problems.
|
28
34
|
|
29
|
-
|
35
|
+
Usage: connect [options] [mode] host port
|
30
36
|
|
31
|
-
|
37
|
+
Options:
|
38
|
+
-s, --secure Establish a secure connection (SSL/TSL)
|
39
|
+
-v, --verbose Enable verbose output for detailed information
|
40
|
+
-r, --retries NUM Specify the number of connection retries (default 3)
|
41
|
+
-t, --timeout NUM Set the connection timeout in seconds (default 10)
|
32
42
|
|
33
|
-
|
43
|
+
Arguments:
|
44
|
+
[mode] Optional connection mode (active or passive)
|
45
|
+
host The target host to connect to (e.g., example.com)
|
46
|
+
port The target port to connect to (e.g., 80)
|
47
|
+
```
|
34
48
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/usage_by_example.
|
@@ -1,5 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class UsageByExample
|
4
|
-
VERSION =
|
4
|
+
VERSION = '1.1.0'
|
5
5
|
end
|
6
|
+
|
7
|
+
|
8
|
+
__END__
|
9
|
+
|
10
|
+
# Major version bump when breaking changes or new features
|
11
|
+
# Minor version bump when backward-compatible changes or enhancements
|
12
|
+
# Patch version bump when backward-compatible bug fixes, security updates etc
|
13
|
+
|
14
|
+
1.0.1
|
15
|
+
|
16
|
+
- Update readme file with features and an example
|
17
|
+
- Update the gemspec to include readme and ruby files only
|
18
|
+
|
19
|
+
1.0.0
|
20
|
+
|
21
|
+
- Extract optional and required argument names from a usage text
|
22
|
+
- Extract option names and associated argument names (if any) from a usage text
|
23
|
+
- Include help option by default
|
24
|
+
- Parse options and arguments from command-line arguments aka ARGV
|
25
|
+
- Exit gracefully or raise exceptions, depending on the exit_on_error parameter
|
26
|
+
- Implement dynamic methods for checking options and getting arguments
|
27
|
+
- Ensure correct git tag and all changes committed when building gem
|
28
|
+
|
29
|
+
0.0.0
|
30
|
+
|
31
|
+
- Prehistory starts here...
|
data/lib/usage_by_example.rb
CHANGED
@@ -5,6 +5,14 @@ require_relative "usage_by_example/version"
|
|
5
5
|
|
6
6
|
class UsageByExample
|
7
7
|
|
8
|
+
attr_reader :argument_names_optional
|
9
|
+
attr_reader :argument_names_required
|
10
|
+
attr_reader :option_names
|
11
|
+
|
12
|
+
attr_reader :arguments
|
13
|
+
attr_reader :options
|
14
|
+
|
15
|
+
|
8
16
|
def self.read(data)
|
9
17
|
return new data.read
|
10
18
|
end
|
@@ -46,7 +54,7 @@ class UsageByExample
|
|
46
54
|
@option_names.update("-h" => :help, "--help" => :help)
|
47
55
|
end
|
48
56
|
|
49
|
-
def parse(argv)
|
57
|
+
def parse(argv, exit_on_error: true)
|
50
58
|
array = argv.dup
|
51
59
|
@arguments = {}
|
52
60
|
@options = {}
|
@@ -73,6 +81,9 @@ class UsageByExample
|
|
73
81
|
|
74
82
|
# --- 2) Parse optional arguments ---------------------------------
|
75
83
|
|
84
|
+
# Check any start with --, ie excess options
|
85
|
+
# Check min_length - max_length here
|
86
|
+
|
76
87
|
stash = array.pop(@argument_names_required.length)
|
77
88
|
@argument_names_optional.each do |argument_name|
|
78
89
|
break if array.empty?
|
@@ -95,25 +106,27 @@ class UsageByExample
|
|
95
106
|
if not array.empty?
|
96
107
|
# Custom error message if most recent option did not require argument
|
97
108
|
raise "Got unexpected argument for option #{most_recent_option}" if most_recent_option
|
98
|
-
min_length = @argument_names.length
|
99
|
-
max_length = @argument_names.length + @argument_names_optional.length
|
100
109
|
raise "Expected #{min_length}#{"-#{max_length}" if max_length > min_length} arguments, got more"
|
101
110
|
end
|
102
111
|
|
103
112
|
return self
|
104
113
|
|
105
114
|
rescue RuntimeError => err
|
106
|
-
|
107
|
-
|
108
|
-
|
115
|
+
if exit_on_error
|
116
|
+
puts "ERROR: #{err.message}\n\n" unless err.message.empty?
|
117
|
+
puts @usage
|
118
|
+
exit
|
119
|
+
else
|
120
|
+
raise # Reraise the same exception
|
121
|
+
end
|
109
122
|
end
|
110
123
|
|
111
124
|
def method_missing(sym, *args, &block)
|
112
125
|
case sym
|
113
|
-
when /^argument_(
|
126
|
+
when /^argument_(\w+)$/
|
114
127
|
val = @arguments[$1]
|
115
128
|
block && val ? block.call(val) : val
|
116
|
-
when /^include_(
|
129
|
+
when /^include_(\w+)\?$/
|
117
130
|
@options[$1]
|
118
131
|
else
|
119
132
|
super
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: usage_by_example
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrian Kuhn
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-04-
|
11
|
+
date: 2023-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -17,9 +17,6 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
- ".rspec"
|
21
|
-
- Gemfile
|
22
|
-
- Gemfile.lock
|
23
20
|
- README.md
|
24
21
|
- lib/usage_by_example.rb
|
25
22
|
- lib/usage_by_example/version.rb
|
data/.rspec
DELETED
data/Gemfile
DELETED
data/Gemfile.lock
DELETED