toys 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +33 -19
- data/builtins/do.rb +9 -4
- data/docs/guide.md +188 -8
- data/lib/toys/standard_cli.rb +12 -6
- data/lib/toys/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: 4090b70821f47975fbfd82cd40651b3ee93e43ecf77cb7a820f20322524a3447
|
4
|
+
data.tar.gz: 70440b824c11d8c7b14e0cda8bfd57f8a74df0aa084c5f47b63efbd3047a7706
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2a58b4eea98ed65f797dcccdef7bc5abbb3df45aa9771263e9c69bc809b3d15507521cfc012753285dc9464c441db0d08e7a31c15c098e610fffe959888e621
|
7
|
+
data.tar.gz: 41a9dbfee92b60d34cb74b07ac8599a8bf01da0203d386dcfd73a3dac8711030d97c6548ff40caa3dd63dc9fb123c14eb590dbb6a5809679978ebc081328141d
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# Release History
|
2
2
|
|
3
|
+
### 0.3.6 / 2018-05-21
|
4
|
+
|
5
|
+
* CHANGED: Removed Context#new_cli and exposed Context#cli instead.
|
6
|
+
* CHANGED: Raises ToolDefinitionError if you declare a duplicate flag.
|
7
|
+
* IMPROVED: Provide more details in default descriptions.
|
8
|
+
* IMPROVED: Optional parameters are now supported for flags.
|
9
|
+
* IMPROVED: Support custom acceptors.
|
10
|
+
* IMPROVED: Highline helper automatically sets use_color based on the type of stdout.
|
11
|
+
|
3
12
|
### 0.3.5 / 2018-05-15
|
4
13
|
|
5
14
|
* CHANGED: Flag and arg blocks in the DSL have an interface more similar to the rest of the DSL.
|
data/README.md
CHANGED
@@ -7,15 +7,28 @@ for software developers, IT specialists, and other power users who want to
|
|
7
7
|
write and organize scripts to automate their workflows.
|
8
8
|
|
9
9
|
I wrote Toys because I was accumulating dozens of ad hoc Ruby scripts that I
|
10
|
-
had written to automate
|
11
|
-
git history in my favorite format, to
|
12
|
-
multi-component projects. It was becoming
|
13
|
-
did what, and what arguments
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
10
|
+
had written to automate various tasks in my workflow, everything from
|
11
|
+
refreshing credentials, to displaying git history in my favorite format, to
|
12
|
+
running builds and tests of complex multi-component projects. It was becoming
|
13
|
+
difficult to remember which scripts did what, and what arguments each required,
|
14
|
+
and I was constantly digging back into their source just to remember how to use
|
15
|
+
them. Furthermore, when writing new scripts, I was repeating the same
|
16
|
+
OptionParser boilerplate and common functionality.
|
17
|
+
|
18
|
+
Toys was designed to address those problems by providing a framework for
|
19
|
+
writing and organizing command line scripts. You provide the actual script
|
20
|
+
functionality, and Toys takes care of all the other details expected from a
|
21
|
+
good command line tool. It provides a streamlined interface for defining and
|
22
|
+
handling command line flags and positional arguments, and sensible ways to
|
23
|
+
organize shared code. It automatically generates help text, so you can see
|
24
|
+
usage information at a glance, and it also provides a search feature to help
|
25
|
+
you find the script you need.
|
26
|
+
|
27
|
+
Toys can also be used to share scripts. For example, it can be used instead of
|
28
|
+
Rake to provide build and test scripts for a project--- tools that, unlike Rake
|
29
|
+
tasks, can be invoked and passed arguments using familiar unix command line
|
30
|
+
arguments and flags. The Toys github repo itself comes with Toys configs
|
31
|
+
instead of Rakefiles.
|
19
32
|
|
20
33
|
You can also use the core functionality of Toys to create your own command line
|
21
34
|
binaries, by using the *toys-core* API, which is available as a separate gem.
|
@@ -24,7 +37,7 @@ For more info on using toys-core, see
|
|
24
37
|
|
25
38
|
## Quick Start
|
26
39
|
|
27
|
-
Here's a five-minute tutorial to get
|
40
|
+
Here's a five-minute tutorial to get a feel of what Toys can do.
|
28
41
|
|
29
42
|
### Install toys
|
30
43
|
|
@@ -40,8 +53,8 @@ You can run the binary immediately:
|
|
40
53
|
This displays overall help for the Toys binary. If you have `less` installed,
|
41
54
|
Toys will use it to display the help screen. Press `q` to exit.
|
42
55
|
|
43
|
-
You may notice that the help
|
44
|
-
run one of them:
|
56
|
+
You may notice that the help screen lists some tools that are preinstalled.
|
57
|
+
Let's run one of them:
|
45
58
|
|
46
59
|
toys system version
|
47
60
|
|
@@ -74,8 +87,8 @@ flags. It can also validate arguments. Try this:
|
|
74
87
|
|
75
88
|
toys greet --hello
|
76
89
|
|
77
|
-
Notice that Toys automatically generated a usage summary for your tool. It
|
78
|
-
automatically
|
90
|
+
Notice that Toys automatically generated a usage summary for your tool. It can
|
91
|
+
also automatically generate a full help screen, which you can view using the
|
79
92
|
`--help` flag:
|
80
93
|
|
81
94
|
toys greet --help
|
@@ -83,10 +96,10 @@ automatically generates a full help screen, which you can view using the
|
|
83
96
|
### Next steps
|
84
97
|
|
85
98
|
You can add any number of additional tools to your `.toys.rb` file. Note also
|
86
|
-
that the tools you
|
87
|
-
and its subdirectories
|
88
|
-
|
89
|
-
|
99
|
+
that the tools you create in that file are available only in this directory
|
100
|
+
and its subdirectories. If you move outside the directory tree, Toys will not
|
101
|
+
use that file. Thus, you can define tools scoped to particular directories and
|
102
|
+
projects.
|
90
103
|
|
91
104
|
Toys also lets you create hierarchies of tools. The "system version" tool you
|
92
105
|
tried earlier is an example. The "system" tool is treated as a namespace, and
|
@@ -99,7 +112,8 @@ console-based interfaces, and special effects. It also includes a library that
|
|
99
112
|
makes it easy to control subprocesses.
|
100
113
|
|
101
114
|
For a more detailed look at Toys, see the
|
102
|
-
{file:docs/tutorial.md Extended Tutorial} and
|
115
|
+
{file:docs/tutorial.md Extended Tutorial} and the
|
116
|
+
{file:docs/guide.md User Guide}.
|
103
117
|
|
104
118
|
## Contributing
|
105
119
|
|
data/builtins/do.rb
CHANGED
@@ -35,12 +35,17 @@ long_desc \
|
|
35
35
|
" the string \",\" by default). Toys will run them in order, stopping if any tool" \
|
36
36
|
" returns a nonzero exit code.",
|
37
37
|
"",
|
38
|
-
"Example: Suppose you have a \"rails build\" tool and a \"deploy\" tool
|
39
|
-
"
|
40
|
-
[" toys do rails build
|
38
|
+
"Example: Suppose you have a \"rails build\" tool and a \"deploy\" tool. You could run them" \
|
39
|
+
" in order like this:",
|
40
|
+
[" toys do rails build , deploy"],
|
41
|
+
"",
|
42
|
+
"However, if you want to pass flags to the tools to run, you need to preface the arguments" \
|
43
|
+
" with \"--\" in order to prevent \"do\" from trying to use them as its own flags. That" \
|
44
|
+
" might look something like this:",
|
45
|
+
[" toys do -- rails build --staging , deploy --migrate"],
|
41
46
|
"",
|
42
47
|
"You may change the delimiter using the --delim flag. For example:",
|
43
|
-
[" toys do --delim=/ rails build --staging / deploy --migrate"]
|
48
|
+
[" toys do --delim=/ -- rails build --staging / deploy --migrate"]
|
44
49
|
|
45
50
|
flag :delim, "-d", "--delim=VALUE",
|
46
51
|
default: ",",
|
data/docs/guide.md
CHANGED
@@ -21,9 +21,9 @@ Toys is a multi-command binary. You may define a collection of commands, called
|
|
21
21
|
`toys` binary. Tools are arranged in a hierarchy; a tool may be a *namespace*
|
22
22
|
that has *subtools*.
|
23
23
|
|
24
|
-
|
25
|
-
*positional arguments
|
26
|
-
|
24
|
+
Tools may recognize command line arguments in the form of *flags* and
|
25
|
+
*positional arguments*. Flags can optionally take *values*, while positional
|
26
|
+
arguments may be *required* or *optional*.
|
27
27
|
|
28
28
|
The configuration of a tool may define *descriptions*, for the tool itself, and
|
29
29
|
for each command line argument. These descriptions are displayed in the tool's
|
@@ -42,18 +42,198 @@ configuration.
|
|
42
42
|
Finally, Toys provides certain *built-in behavior*, including automatically
|
43
43
|
providing flags to display help screens and set verbosity. It also includes a
|
44
44
|
built-in namespace of *system tools* that let you inspect and configure the
|
45
|
-
Toys system.
|
45
|
+
Toys system itself.
|
46
46
|
|
47
47
|
## The Toys Command Line
|
48
48
|
|
49
|
-
|
49
|
+
In this section, you will learn how Toys parses its command line, identifies a
|
50
|
+
tool to run, and interprets flags and other command line arguments.
|
50
51
|
|
51
|
-
|
52
|
+
The general form of the `toys` command line is:
|
52
53
|
|
53
|
-
|
54
|
+
toys [TOOL...] [FLAGS...] [ARGS...]
|
55
|
+
|
56
|
+
### Tools
|
57
|
+
|
58
|
+
The *tool name* consists of all the command line arguments until the first
|
59
|
+
argument that begins with a hyphen (which is interpreted as a *flag*), until
|
60
|
+
no tool with that name exists (in which case the argument is treated as the
|
61
|
+
first *positional argument*), or until there are no more arguments.
|
62
|
+
|
63
|
+
For example, in the following command:
|
64
|
+
|
65
|
+
|----TOOL----|
|
66
|
+
toys system version
|
67
|
+
|
68
|
+
The tool name is `system version`. Notice that the tool name may have multiple
|
69
|
+
words. Tools are arranged hierarchically. In this case, `system` is a
|
70
|
+
*namespace* for tools related to the Toys system, and `version` is one of its
|
71
|
+
*subtools*. It prints the current Toys version.
|
72
|
+
|
73
|
+
In the following command:
|
74
|
+
|
75
|
+
|TOOL| |ARG|
|
76
|
+
toys system blah
|
77
|
+
|
78
|
+
There is no subtool `blah` under the `system` namespace, so Toys works backward
|
79
|
+
until it finds a legitimate tool. In this case, the `system` namespace itself
|
80
|
+
does exist, so it is interpreted as the tool, and `blah` is interpreted as an
|
81
|
+
argument passed to it.
|
82
|
+
|
83
|
+
Namespaces such as `system` are themselves tools and can be executed like any
|
84
|
+
other tool. In the above case, its function is to take the argument `blah`,
|
85
|
+
note that it has no subtool of that name, and print an error message. Most
|
86
|
+
commonly, though, you might execute a namespace without arguments:
|
87
|
+
|
88
|
+
toys system
|
89
|
+
|
90
|
+
This displays the *online help screen* for the `system` namespace, which
|
91
|
+
includes a list of all its subtools and what they do.
|
92
|
+
|
93
|
+
It is also legitimate for the tool name to be empty. This invokes the "root"
|
94
|
+
tool, the toplevel namespace:
|
95
|
+
|
96
|
+
toys
|
97
|
+
|
98
|
+
Like any namespace, the root tool displays its help screen, including the list
|
99
|
+
of its subtools.
|
100
|
+
|
101
|
+
One last example:
|
102
|
+
|
103
|
+
toys blah
|
104
|
+
|
105
|
+
If there is no tool called `blah` in the toplevel namespace, then once again,
|
106
|
+
`blah` is interpreted as an argument to the root tool. The root tool responds
|
107
|
+
by printing an error message that the `blah` tool does not exist.
|
108
|
+
|
109
|
+
### Flags
|
110
|
+
|
111
|
+
Flags are generally arguments that begin with a hyphen, and are used to set
|
112
|
+
options for a tool.
|
113
|
+
|
114
|
+
Each tool recognizes a specific set of flags. If you pass an unknown flag to a
|
115
|
+
tool, the tool will generally display an error message.
|
116
|
+
|
117
|
+
Toys follows the typical unix conventions for flags: you can provide short
|
118
|
+
(single-character) flags with a single hyphen, or long flags with a double
|
119
|
+
hyphen. You can also provide optional values for flags. Following are a few
|
120
|
+
examples.
|
121
|
+
|
122
|
+
Pass a single short flag (for verbose output).
|
123
|
+
|
124
|
+
toys -v
|
125
|
+
|
126
|
+
Pass multiple long flags (verbose output, and recursive subtool search).
|
127
|
+
|
128
|
+
toys --verbose --recursive
|
129
|
+
|
130
|
+
You can combine short flags. This does the same as the previous example.
|
131
|
+
|
132
|
+
toys -rv
|
133
|
+
|
134
|
+
Pass a value using a long flag. This searches subtools for the keyword `build`.
|
135
|
+
|
136
|
+
toys --search=build
|
137
|
+
toys --search build
|
138
|
+
|
139
|
+
Pass a value using a short flag.
|
140
|
+
|
141
|
+
toys -s build
|
142
|
+
toys -sbuild
|
143
|
+
|
144
|
+
If a double hyphen `--` appears by itself in the arguments, it disables flag
|
145
|
+
parsing from that point. Any further arguments are treated as positional
|
146
|
+
arguments, even if they begin with hyphens. For example:
|
147
|
+
|
148
|
+
|--FLAG--| |---ARG---|
|
149
|
+
toys --verbose -- --recursive
|
150
|
+
|
151
|
+
That will cause `--recursive` to be treated as a positional argument. (In this
|
152
|
+
case, as we saw earlier, the root tool will respond by printing an error
|
153
|
+
message that no subtool named `--recursive` exists.)
|
154
|
+
|
155
|
+
Note that a single hyphen by itself `-` is not considered a flag and is treated
|
156
|
+
as a positional argument.
|
157
|
+
|
158
|
+
#### Standard Flags
|
159
|
+
|
160
|
+
For the most part, each tool specifies which flags and arguments it recognizes.
|
161
|
+
However, Toys adds a few standard flags globally to every tool. (It is possible
|
162
|
+
for individual tools to override these flags, but most tools should support
|
163
|
+
them.) These standard flags include:
|
164
|
+
|
165
|
+
* `--help` (also `-?`) which displays the full help screen for the tool.
|
166
|
+
* `--usage` which displays a shorter usage screen for the tool.
|
167
|
+
* `--verbose` (also `-v`) which increases the verbosity. This affects the
|
168
|
+
tool's logging display, increasing the number of log levels shown. This
|
169
|
+
flag may be issued multiple times.
|
170
|
+
* `--quiet` (also `-q`) which decreases the verbosity. This affects the
|
171
|
+
tool's logging display, decreasing the number of log levels shown. This
|
172
|
+
flag may also be issued multiple times.
|
173
|
+
|
174
|
+
Namespace tools (tools that have subtools but no explicit functionality of
|
175
|
+
their own) always behave as though `--help` is invoked. (They do recognize the
|
176
|
+
flag, but it has no additional effect.) Namespaces also support the following
|
177
|
+
additional flags:
|
178
|
+
|
179
|
+
* `--[no-]recursive` (also `-r`) which displays all subtools recursively,
|
180
|
+
instead of only the immediate subtools.
|
181
|
+
* `--search=TERM` which displays only subtools whose name or description
|
182
|
+
contain the specified search term.
|
183
|
+
|
184
|
+
Finally, the root tool also supports:
|
185
|
+
|
186
|
+
* `--version` which displays the current Toys version.
|
187
|
+
|
188
|
+
### Positional Arguments
|
189
|
+
|
190
|
+
Any arguments not recognized as flags or flag arguments, are interpreted as
|
191
|
+
positional arguments. Positional arguments are recognized in order and may be
|
192
|
+
required or optional.
|
193
|
+
|
194
|
+
Each tool recognizes a specific set of positional arguments. If you do not pass
|
195
|
+
a value for a required argument, or you pass too many arguments, the tool will
|
196
|
+
generally display an error message.
|
197
|
+
|
198
|
+
For example, the `do` tool runs multiple tools in sequence. It recognizes any
|
199
|
+
number of positional arguments. Those arguments specify which tools to run and
|
200
|
+
what arguments to pass to them. If, for example, you had a `build` tool and a
|
201
|
+
`test` tool, you could run them in sequence with:
|
202
|
+
|
203
|
+
|---ARGS---|
|
204
|
+
toys do build , test
|
205
|
+
|
206
|
+
The arguments `build`, `,`, and `test` are positional arguments to the `do`
|
207
|
+
tool. (The `do` tool splits them using `,` as the delimiter.)
|
208
|
+
|
209
|
+
Here is a more complex example illustrating the interaction between flags and
|
210
|
+
positional arguments. Suppose we want to use `do` to display the help screens
|
211
|
+
for the root tool and the system tool in sequence. That is, we want to run
|
212
|
+
`toys --help` and `toys system --help` in sequence. We might start by trying:
|
213
|
+
|
214
|
+
toys do --help , system --help
|
215
|
+
|
216
|
+
However, this simply displays the help for the `do` tool itself, because the
|
217
|
+
first `--help` is interpreted as a flag for `do` instead of a positional
|
218
|
+
argument specifying the first tool for `do` to run. We need to force `do` to
|
219
|
+
treat all its arguments as positional, and we can do that by starting with `--`
|
220
|
+
like so:
|
221
|
+
|
222
|
+
|--------ARGS--------|
|
223
|
+
toys do -- --help , system --help
|
224
|
+
|
225
|
+
## Defining Tools
|
226
|
+
|
227
|
+
## Understanding Configurations
|
228
|
+
|
229
|
+
## Helper Methods and Modules
|
230
|
+
|
231
|
+
## The Standard Helpers
|
232
|
+
|
233
|
+
## Prefabricated Tools with Templates
|
54
234
|
|
55
235
|
## Defining Templates
|
56
236
|
|
57
|
-
##
|
237
|
+
## Advanced Tool Definition Techniques
|
58
238
|
|
59
239
|
## Embedding Toys
|
data/lib/toys/standard_cli.rb
CHANGED
@@ -29,9 +29,6 @@
|
|
29
29
|
|
30
30
|
require "logger"
|
31
31
|
|
32
|
-
require "toys/middleware/show_version"
|
33
|
-
require "toys/utils/wrappable_string"
|
34
|
-
|
35
32
|
module Toys
|
36
33
|
##
|
37
34
|
# Helpers that configure the toys-core CLI with the behavior for the
|
@@ -86,10 +83,16 @@ module Toys
|
|
86
83
|
#
|
87
84
|
DEFAULT_ROOT_LONG_DESC =
|
88
85
|
"Toys is your personal command line tool. You can add to the list of commands below by" \
|
89
|
-
" writing scripts in Ruby using a simple DSL, and
|
86
|
+
" writing scripts in Ruby using a simple DSL, and Toys will organize and document them" \
|
90
87
|
" and make them available globally or scoped to specific directories that you choose." \
|
91
88
|
" For detailed information, see https://www.rubydoc.info/gems/toys".freeze
|
92
89
|
|
90
|
+
##
|
91
|
+
# Short description for the verion flag
|
92
|
+
# @return [String]
|
93
|
+
#
|
94
|
+
DEFAULT_VERSION_FLAG_DESC = "Show the version of Toys.".freeze
|
95
|
+
|
93
96
|
##
|
94
97
|
# Create a standard CLI, configured with the appropriate paths and
|
95
98
|
# middleware.
|
@@ -160,11 +163,13 @@ module Toys
|
|
160
163
|
search_flags: true,
|
161
164
|
default_recursive: true,
|
162
165
|
allow_root_args: true,
|
166
|
+
show_source_path: true,
|
163
167
|
use_less: true
|
164
168
|
],
|
165
169
|
[
|
166
|
-
:
|
167
|
-
|
170
|
+
:show_root_version,
|
171
|
+
version_string: ::Toys::VERSION,
|
172
|
+
version_flag_desc: DEFAULT_VERSION_FLAG_DESC
|
168
173
|
],
|
169
174
|
[
|
170
175
|
:handle_usage_errors
|
@@ -175,6 +180,7 @@ module Toys
|
|
175
180
|
recursive_flags: true,
|
176
181
|
search_flags: true,
|
177
182
|
default_recursive: true,
|
183
|
+
show_source_path: true,
|
178
184
|
use_less: true
|
179
185
|
],
|
180
186
|
[
|
data/lib/toys/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Azuma
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: toys-core
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.3.
|
19
|
+
version: 0.3.6
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.3.
|
26
|
+
version: 0.3.6
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: minitest
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|