Skip to content

Commit 908c0bb

Browse files
authored
Improving the documentation
1 parent 82c3919 commit 908c0bb

1 file changed

Lines changed: 44 additions & 45 deletions

File tree

README.md

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ $builder
4848
1. [Installation](#installation)
4949
2. [Usage](#usage)
5050
1. [Simple Commands](#simple-commands)
51-
2. [Pipelines, Lists and Redirections](#pipelines-lists-and-redirections)
51+
2. [Pipelines, Lists, and Redirections](#pipelines-lists-and-redirections)
5252
4. [Complex Commands](#complex-commands)
5353
5. [Conditional Expressions](#conditional-expressions)
5454
6. [Coprocess](#coprocess)
@@ -82,7 +82,7 @@ The ShellBuilder is the glue that holds a collection of commands together.
8282
The glue is one of the control operators like `||` or `&&`.<br/>
8383
Commands are represented by the ShellCommand-Class.
8484
The ShellCommand is responsible for the arguments and options etc.<br />
85-
A ShellCommand is composed of ShellWords, they represent the tokens that make up command.
85+
A ShellCommand is composed of ShellWords, they represent the tokens that make up a command.
8686

8787
Let's look at an example:
8888
```shell script
@@ -104,21 +104,21 @@ Taking apart each of those commands returns the following ShellWords:
104104

105105
Much of the API is marked internal, it is meant to be accessed through the `ShellBuilder`-Class.
106106
<br />This should make it very straight-forward to build simple and more complex commands from one basis.<br />
107-
Additionally does the `ShellBuilder` have Factory-style methods, that help building commands top to bottom in a go.
107+
Additionally, the `ShellBuilder` has factory-style methods that help building commands top to bottom in an instant.
108108

109109
That means, creating a `ShellBuilder` can look like this:
110110
```php
111111
$builder = new ShellBuilder();
112112
```
113-
or alternatively like this:
113+
or like this:
114114
```php
115115
$builder = ShellBuilder::new();
116116
```
117-
and a ShellCommand can be created like this
117+
A ShellCommand can be created like this:
118118
```php
119119
$command = ShellBuilder::command('name-of-command');
120120
```
121-
or, if an an instance of ShellBuilder is already available, like this
121+
or, if there is already a ShellBuilder-object available, like this
122122
```php
123123
/** @var \PHPSu\ShellCommandBuilder\ShellBuilder $builder */
124124
$builder->createCommand('name-of-command');
@@ -145,19 +145,18 @@ $echo->addArgument('Hello World');
145145
$grep = ShellBuilder::command('grep');
146146

147147
// 4. and add the option '-e "world"'.
148-
// the option "e" is preceeded by a single hypen, therefore it's a *short* option
149-
// if it would have two hypens e.g --color, then its a regular option.
150-
// But in both cases, typing the hypens is not necessary and might be forbidden in a future version.
148+
// the single hyphen that is before the option "e" marks it as a *short* option (addShortOption)
149+
// Having two hyphens like --color makes it a regular option (addOption)
151150
$grep->addShortOption('e', 'world');
152151

153-
// 5. Now we need to glow the two commands together
152+
// 5. Now we need combine those two commands together
154153
// We do that, by creating a ShellBuilder
155154
$builder = ShellBuilder::new();
156155

157156
// 6. And then adding the echo-command into it
158157
$builder->add($echo);
159158

160-
// 7. Earlier we saw, that these two commands where hold together by the pipe-Operator
159+
// 7. Earlier we saw, that these two commands where held together by the pipe-Operator
161160
// This can be accomplished by using the pipe-Method
162161
$builder->pipe($grep);
163162

@@ -166,9 +165,8 @@ shell_exec((string)$builder); // -> echo 'hello world' | echo -e 'world'
166165
```
167166
> Note: Every argument and option is escaped by default.
168167
169-
Every method is fluid. Alternatively to creating the commands one by one, everything can be done written top to bottom.
170-
171-
The following example returns the exact same thing as the one above.
168+
All methods implement the fluent interface.
169+
For this library that means that you can rewrite the example above by chaining everything together:
172170

173171
```php
174172
<?php
@@ -186,20 +184,20 @@ $builder = ShellBuilder::new()
186184
shell_exec((string)$builder); // -> echo 'hello world' | echo -e 'world'
187185
```
188186

189-
How this works, is, that `createCommand` passes the current ShellBuilder into the ShellCommand-Instance.
190-
Through `addToBuilder` that ShellBuilder can be accessed again and the command is automatically added to the ShellBuilder.
187+
The `createCommand` passes the current ShellBuilder into the ShellCommand-Instance.
188+
Through `addToBuilder` that ShellBuilder can be accessed again, and the command is automatically added to the ShellBuilder.
191189
This currently only works for `and`.
192190

193-
#### Pipelines, Lists and Redirections
191+
#### Pipelines, Lists, and Redirections
194192

195-
The ShellBuilder is the representation of what holds commands together.
196-
Whether it is to execute commands sequentially or to connect input and output.
193+
The ShellBuilder is a representation of what holds commands together.
194+
Whether it is to execute commands sequentially, or to connect input and output.
197195

198196
Let's look at this following fake example:<br/>
199197
`a; b && c | d || e |& f 2>&1`<br />
200-
It containts various ways of connecting commands together
198+
It illustrates the various ways of connecting commands together.
201199

202-
To re-build this command,
200+
Rebuilding this command could look like this:
203201

204202
```php
205203
<?php
@@ -227,14 +225,14 @@ The full list of methods can be found here: [API Docs](/docs/api.md)
227225

228226
#### Complex Commands
229227

230-
The intend for this library is to make generating larger and complex shell commands more approachable.<br />
228+
The idea behind this library is to make generating larger and complex shell commands more readable and maintainable.<br />
231229
The following example is taken out of PHPsu. This command syncs a database from a remote source to a local database.
232230

233231
```shell script
234232
ssh -F 'php://temp' 'hostc' 'mysqldump --opt --skip-comments --single-transaction --lock-tables=false -h '\''database'\'' -u '\''root'\'' -p '\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)' | mysql -h '127.0.0.1' -P 2206 -u 'root' -p 'root'
235233
```
236234

237-
First, we have to think about the components that this command is build of.
235+
First, we have to think about the components that this command is composed of.
238236
This results in these commands:
239237
```shell script
240238
ssh -F 'php://temp' 'hostc'
@@ -248,16 +246,17 @@ cat
248246
mysql -h '127.0.0.1' -P 2206 -u 'root' -p 'root'
249247
```
250248

251-
Now let's look how that would look like:
249+
Now, we build this in PHP:
252250

253251
```php
254252
<?php
255253

256254
use PHPSu\ShellCommandBuilder\ShellBuilder;
257255

258256
$builder = new ShellBuilder();
259-
// creating a command. The 'true' indicates, that the command does not know of its builder
260-
// This is the same result, as is we did ShellBuilder::command()
257+
// creating the first command.
258+
// The 'true' removes the connection between ShellBuilder and ShellCommand and makes it anonymous.
259+
// This is the same result as ShellBuilder::command()
261260
$mysqlDump = $builder->createCommand('mysqldump', true)
262261
// adding the options and short-options
263262
->addOption('opt')
@@ -300,10 +299,10 @@ $builder->createCommand('ssh')
300299
;
301300
```
302301

303-
In the next example, we take a look at how to achieve process and command substition.
304-
The following is again a complete nonsense example.
302+
Next, we take a look at how to achieve process and command substition.
303+
The following is again a mock example.
305304
It creates a list of all php-files in the current and all below directories, sorted and enriched with the size.
306-
Those file-list is redirected into a txt-file with the current month-name as filename.
305+
This file-list is redirected into a txt-file with the current month-name as filename.
307306

308307
```shell script
309308
cat <(ls -1ARSsD | grep ".*\.php") >> $(date +%B).txt
@@ -324,9 +323,9 @@ $builder = ShellBuilder::new()
324323
// the same would work with `createCommandSubstition` resulting in something like this $(command ...)
325324
->createProcessSubstition()
326325
->createCommand('ls')
327-
// currently combining short-options has to be done manually, this might change in the future
328-
// but doing it like this will always be possible, since its impossible to evaluate the correctness
329-
// without having the man-page of all commands available
326+
// currently combining short-options has to be done manually, although it could change in the future
327+
// but doing it like this will always be possible, since it's impossible to evaluate the correctness
328+
// without having the man-page of all the commands available
330329
->addShortOption('1ARSsD')
331330
->addToBuilder()
332331
->pipe(
@@ -342,8 +341,8 @@ $builder = ShellBuilder::new()
342341
ShellBuilder::new()
343342
->createCommand('date')
344343
->addArgument('+%B', false)
345-
// this is similar to the process/command-substitition from above but here applied on a command instead
346-
// toggling means that instead of taking true or false as argument it flips the internal state back and forth
344+
// this is similar to the process/command-substitition from above but here it is applied on a command instead
345+
// toggling means that instead of taking true or false as an argument it flips the internal state back and forth
347346
->toggleCommandSubstitution()
348347
->addToBuilder()
349348
->addFileEnding('txt'),
@@ -354,12 +353,12 @@ $builder = ShellBuilder::new()
354353

355354
#### Conditional Expressions
356355

357-
Conditional Expressions are currently work in progress. The basic API stand but the overall usage might change, especially when it comes down to escaping.
356+
Conditional Expressions are currently a work in progress. The basic API stands, but the overall usage might change, especially when it comes down to escaping.
358357

359-
There are multiple possible conditional-expression-types, that can be used to build expressions.
358+
There are multiple conditional-expression-types that can be used to built expressions.
360359
They are build upon the [Shell-Syntax Bash Reference](https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html).
361360

362-
These types exist:
361+
The following expression-types exist:
363362
- Artihmetic: ArithmeticExpression::class
364363
- File: FileExpression::class
365364
- Shell: ShellExpression::class
@@ -402,7 +401,7 @@ ShellBuilder::new()
402401

403402
#### Coprocess
404403

405-
To run commands in background, the ShellBuilder class supports the `coproc` keyword.
404+
To run commands in the background, the ShellBuilder class supports the `coproc` keyword.
406405
<br /> This keyword lets the command run asynchronously in a subshell and can be combined with pipes and redirections.
407406

408407
More information on Coprocesses can be found [in the Bash Reference](https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html).
@@ -437,17 +436,17 @@ $builder->runAsynchronously(true)
437436
false
438437
)
439438
->addToBuilder()
440-
// redirectDescriptor is the more powerfull way of writing redirects bewtween File Descriptors
439+
// redirectDescriptor is the more powerful way of writing redirects between File Descriptors
441440
// argument 1: command that we redirect from/to
442441
// argument 2: direction of the redirect (true: >&, false <&)
443442
// argument 3: file descriptor before redirection
444443
// argument 4: file descriptor after redirection
445-
// so this example below would render: >&3
444+
// the example below would render: >&3
446445
->redirectDescriptor('', true, null, 3);
447446
ShellBuilder::new()->add($builder)->redirectDescriptor('', true, 3, 1);
448447
```
449448

450-
If you want to direct a single or command or a list of commands into the background, you can achieve that by appending an ampersand `&` at the end of a command.
449+
If you want to direct a single command or a list of commands into the background, you can achieve that by appending an ampersand `&` at the end of a command.
451450

452451
So maybe you want to do this:
453452
`./import-script & ./import-script2 &`
@@ -464,8 +463,8 @@ ShellBuilder::new()->add('./import-script')->async('./import-script2')->async();
464463

465464
#### Pattern-Class - ShellWord parsing
466465

467-
The pattern-class is to validate string inputs as valid Bourne Shellwords.
468-
It is based on its equivalents in the [Ruby](https://ruby-doc.org/stdlib-2.5.1/libdoc/shellwords/rdoc/Shellwords.html) and Rust languages.
466+
The pattern-class validates string inputs as valid Bourne Shellwords.
467+
It is based on its equivalent implementations in the [Ruby](https://ruby-doc.org/stdlib-2.5.1/libdoc/shellwords/rdoc/Shellwords.html) and Rust languages.
469468
<br/>
470469
It takes a string and applies the word parsing rules of shell to split it into an array.
471470

@@ -488,7 +487,7 @@ Pattern::split('a "b b" a');
488487
// ['a', 'b b', 'a']
489488
```
490489

491-
The method will throw an exception if there is invalid input.
490+
The method will throw an exception if there is an invalid input.
492491
<br />
493492
For example the following has an unmatched quoting:
494493

@@ -503,7 +502,7 @@ Pattern::split("a \"b c d e");
503502

504503
Sometimes there is a need to better understand why the output is rendered the way it is.
505504
<br />
506-
For those situations, all classes implement a `__toArray()`-method, that take the current class-state and print it as array.
505+
For those situations, all classes implement a `__toArray()`-method, that take the current class-state and print it as an array.
507506
The `ShellBuilder` additionally implements `jsonSerializable`.
508507
It itself calls the `__toArray`-method and is meant as a shortcut for outputting to a client.
509508

0 commit comments

Comments
 (0)