shellopts 0.9.5 → 0.9.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +41 -12
- data/bin/mkdoc +11 -6
- data/lib/shellopts.rb +24 -5
- data/lib/shellopts/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 943b9d0c44ba0291937be24cbc79d0155dfc22ac788c69a76639850d78af3303
|
4
|
+
data.tar.gz: f38e038f613c577b06e9d2facc6096b39eb18f28266ba8c58a8280adcf75fe47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42cc6102c0c66dcffda0b03f9c584c32abc0e62a65dbe79e1389753b74af11f189e27a456e79fb2cb082595622ddbce0840dd22419204e276a48134ac8afd610
|
7
|
+
data.tar.gz: 6a3b2e35718a5ee1f52e5d56a50ea067da90fef35a4ac3322738e24d5ba80f4aba4ac4a692caace4bb094ccfb84432e9211fef2b15dfef2e194a95569ca80686
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
# Shellopts
|
2
2
|
|
3
|
-
`ShellOpts` is a simple command line parsing libray that covers most modern use
|
3
|
+
`ShellOpts` is a simple Linux command line parsing libray that covers most modern use
|
4
4
|
cases incl. sub-commands. Options and commands are specified using a
|
5
5
|
getopt(1)-like string that is interpreted by the library to process the command
|
6
6
|
line
|
7
7
|
|
8
8
|
## Usage
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
`--verbose`
|
10
|
+
Program that accepts the options -a or --all, --count, --file, and -v or
|
11
|
+
--verbose. The usage definition expects `--count` to have an optional integer
|
12
|
+
argument, `--file` to have a mandatory argument, and allows `-v` and
|
13
|
+
`--verbose` to be repeated:
|
14
14
|
|
15
|
-
```ruby
|
16
|
-
require 'shellopts'
|
17
15
|
|
16
|
+
```ruby
|
17
|
+
|
18
18
|
# Define options
|
19
19
|
USAGE = "a,all count=#? file= +v,verbose -- FILE..."
|
20
20
|
|
@@ -36,7 +36,7 @@ args = ShellOpts.process(USAGE, ARGV) do |opt, arg|
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
# Process remaining arguments
|
39
|
+
# Process remaining command line arguments
|
40
40
|
args.each { |arg| ... }
|
41
41
|
```
|
42
42
|
|
@@ -50,10 +50,10 @@ error
|
|
50
50
|
|
51
51
|
## Processing
|
52
52
|
|
53
|
-
`ShellOpts.process` compiles a usage definition string into a grammar and use
|
54
|
-
parse the command line. If given a block, the block is called with a
|
55
|
-
pair for each option or command and return a list of the remaining
|
56
|
-
arguments
|
53
|
+
`ShellOpts.process` compiles a usage definition string into a grammar and use
|
54
|
+
that to parse the command line. If given a block, the block is called with a
|
55
|
+
name/value pair for each option or command and return a list of the remaining
|
56
|
+
non-option arguments
|
57
57
|
|
58
58
|
```ruby
|
59
59
|
args = ShellOpts.process(USAGE, ARGV) do |opt, arg|
|
@@ -101,6 +101,18 @@ An option is defined by a list of comma-separated names optionally prefixed by a
|
|
101
101
|
[ "+" ] name-list [ "=" [ "#" | "$" ] [ label ] [ "?" ] ]
|
102
102
|
```
|
103
103
|
|
104
|
+
#### Flags
|
105
|
+
|
106
|
+
There are the following flags:
|
107
|
+
|
108
|
+
|Flag|Effect|
|
109
|
+
|---|---|
|
110
|
+
|+|Repeated option (prefix)|
|
111
|
+
|=|Argument. Mandatory unless `?` is also used|
|
112
|
+
|#|Integer argument|
|
113
|
+
|$|Floating point argument|
|
114
|
+
|?|Optional argument|
|
115
|
+
|
104
116
|
#### Repeated options
|
105
117
|
|
106
118
|
Options are unique by default and the user will get an error if an option is
|
@@ -260,6 +272,23 @@ The methods are defined as instance methods on `ShellOpts::ShellOpts` and as
|
|
260
272
|
class methods on `ShellOpts`. They can also be included in the global scope by
|
261
273
|
`include ShellOpts::Utils`
|
262
274
|
|
275
|
+
#### Usage string
|
276
|
+
|
277
|
+
The error handling methods prints a prettified version of the usage string
|
278
|
+
given to `ShellOpts.parse`. It can be overridden by assigning to
|
279
|
+
`ShellOpts.usage`. You'll often assign to the usage string when it needs to be
|
280
|
+
split over several lines:
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
|
284
|
+
USAGE="long-and-complex-usage-string"
|
285
|
+
ShellOpts.usage = %(
|
286
|
+
usage explanation
|
287
|
+
split over
|
288
|
+
multiple lines
|
289
|
+
)
|
290
|
+
```
|
291
|
+
|
263
292
|
## Example
|
264
293
|
|
265
294
|
The rm(1) command could be implemented like this
|
data/bin/mkdoc
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
#!/usr/bin/bash
|
2
2
|
|
3
|
-
|
3
|
+
set -e
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
# Generate github-like page
|
6
|
+
(
|
7
|
+
cd doc
|
8
|
+
{
|
9
|
+
echo '<link rel="stylesheet" type="text/css" href="stylesheet.css">'
|
10
|
+
pandoc ../README.md
|
11
|
+
} >index.html
|
12
|
+
)
|
9
13
|
|
10
|
-
rdoc
|
14
|
+
# Generate rdoc
|
15
|
+
rdoc --output=rdoc --force-output lib
|
data/lib/shellopts.rb
CHANGED
@@ -12,6 +12,18 @@ require 'shellopts/utils.rb'
|
|
12
12
|
# name of the program
|
13
13
|
#
|
14
14
|
module ShellOpts
|
15
|
+
# Return the hidden +ShellOpts::ShellOpts+ object (see .process)
|
16
|
+
def self.shellopts()
|
17
|
+
@shellopts
|
18
|
+
end
|
19
|
+
|
20
|
+
# Prettified usage string used by #error and #fail. Default is +usage+ of
|
21
|
+
# the current +ShellOpts::ShellOpts+ object
|
22
|
+
def self.usage() @usage ||= @shellopts&.usage end
|
23
|
+
|
24
|
+
# Set the usage string
|
25
|
+
def self.usage=(usage) @usage = usage end
|
26
|
+
|
15
27
|
# Process command line options and arguments. #process takes a usage string
|
16
28
|
# defining the options and the array of command line arguments to be parsed
|
17
29
|
# as arguments
|
@@ -72,14 +84,16 @@ module ShellOpts
|
|
72
84
|
# #process saves a hidden {ShellOpts::ShellOpts} class variable used by the
|
73
85
|
# class methods #error and #fail. Call #reset to clear the global object if
|
74
86
|
# you really need to parse more than one command line. Alternatively you can
|
75
|
-
# create +ShellOpts::ShellOpts+ objects yourself and use the object methods
|
76
|
-
# #error and #fail
|
87
|
+
# create +ShellOpts::ShellOpts+ objects yourself and also use the object methods
|
88
|
+
# #error and #fail:
|
77
89
|
#
|
78
90
|
# shellopts = ShellOpts::ShellOpts.new(USAGE, ARGS)
|
79
91
|
# shellopts.each { |name, value| ... }
|
80
92
|
# shellopts.args.each { |arg| ... }
|
81
93
|
# shellopts.error("Something went wrong")
|
82
94
|
#
|
95
|
+
# Use #shellopts to get the hidden +ShellOpts::ShellOpts+ object
|
96
|
+
#
|
83
97
|
def self.process(usage, argv, program_name: PROGRAM, &block)
|
84
98
|
if !block_given?
|
85
99
|
ShellOpts.new(usage, argv, program_name: program_name)
|
@@ -95,15 +109,20 @@ module ShellOpts
|
|
95
109
|
# another command line
|
96
110
|
def self.reset()
|
97
111
|
@shellopts = nil
|
112
|
+
@usage = nil
|
98
113
|
end
|
99
114
|
|
100
115
|
# Print error message and usage string and exit with status 1. It use the
|
101
116
|
# current ShellOpts object if defined. This method should be called in
|
102
117
|
# response to user-errors (eg. specifying an illegal option)
|
118
|
+
#
|
119
|
+
# If there is no current ShellOpts object +error+ will look for USAGE to make
|
120
|
+
# it possible to use +error+ before the command line is processed and also as
|
121
|
+
# a stand-alone error reporting method
|
103
122
|
def self.error(*msgs)
|
104
123
|
program = @shellopts&.program_name || PROGRAM
|
105
|
-
|
106
|
-
emit_and_exit(program,
|
124
|
+
usage_string = usage || (defined?(USAGE) && USAGE ? Grammar.compile(PROGRAM, USAGE).usage : nil)
|
125
|
+
emit_and_exit(program, usage_string, *msgs)
|
107
126
|
end
|
108
127
|
|
109
128
|
# Print error message and exit with status 1. It use the current ShellOpts
|
@@ -119,7 +138,7 @@ module ShellOpts
|
|
119
138
|
# Name of program
|
120
139
|
attr_reader :program_name
|
121
140
|
|
122
|
-
#
|
141
|
+
# Prettified usage string used by #error and #fail. Shorthand for +grammar.usage+
|
123
142
|
def usage() @grammar.usage end
|
124
143
|
|
125
144
|
# The grammar compiled from the usage string. If #ast is defined, it's
|
data/lib/shellopts/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shellopts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claus Rasmussen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -103,6 +103,7 @@ files:
|
|
103
103
|
- bin/console
|
104
104
|
- bin/mkdoc
|
105
105
|
- bin/setup
|
106
|
+
- doc/stylesheet.css
|
106
107
|
- lib/ext/array.rb
|
107
108
|
- lib/shellopts.rb
|
108
109
|
- lib/shellopts/ast/command.rb
|
@@ -137,8 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
138
|
- !ruby/object:Gem::Version
|
138
139
|
version: '0'
|
139
140
|
requirements: []
|
140
|
-
|
141
|
-
rubygems_version: 2.7.6
|
141
|
+
rubygems_version: 3.0.8
|
142
142
|
signing_key:
|
143
143
|
specification_version: 4
|
144
144
|
summary: Parse command line options and arguments
|