Skip to content

Commit d4ef4b1

Browse files
committed
refactor: code cleanup for the --negate flag
No change to logic. This is a small refactor/cleanup for the '--negate' option added in PR #189. This also adds documentation to the README.
1 parent 6184003 commit d4ef4b1

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ $ shx rm -r sub # options work as well
6060

6161
$ shx --silent ls fakeFileName # silence error output
6262

63-
$ shx --negate test -d dir # Negate status code output for boolean conditions
63+
$ shx --negate test -d dir # Negate status code output (e.g., failed commands will now have status 0)
6464
```
6565

6666
All commands internally call the ShellJS corresponding function, guaranteeing
@@ -149,6 +149,9 @@ supported options:
149149
| `-v` | `config.verbose = true` | `shx --verbose cd foo` | Log the command as it's run |
150150
| `-f` | `config.noglob = true` | `shx --noglob cat '*.txt'` | Don't expand wildcards |
151151
| N/A | `config.silent = true` | `shx --silent cd noexist` | Don't show error output |
152+
| N/A | N/A | `shx --help` | Show help text |
153+
| N/A | N/A | `shx --version` | Print the shx version |
154+
| N/A | N/A | `shx --negate test -d dir` | Runs the specified command but negates the exit status (failed command = status 0, successful command = status 1) |
152155

153156
## Team
154157

test/specs/cli.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,11 @@ describe('cli', () => {
145145

146146
it('uses --negate to "NOT" a "0 output ', () => {
147147
const withoutNegate = cli('true');
148-
// Sanity check
149148
withoutNegate.code.should.equal(0);
150-
// Minifist opts.boolean always treat --* as boolean
151-
// Thus only using "--negate true" will be interpreted as "--negate=true"
152-
// without any actual command. Hence --negate true true
153-
const withNegate = cli('--negate', 'true', 'true');
149+
// We need an extra '--' otherwise Minimist will interprt this like `shx
150+
// --negate=true` without invoking the actual 'true' subcommand. This is
151+
// because of Minimist's opts.boolean option.
152+
const withNegate = cli('--negate', '--', 'true');
154153
withNegate.stdout.should.equal('');
155154
withNegate.stderr.should.equal('');
156155
withNegate.code.should.equal(1);
@@ -160,8 +159,9 @@ describe('cli', () => {
160159
const output = cli('help');
161160
output.stderr.should.equal('');
162161
output.stdout.should.match(/Usage/); // make sure help is printed
163-
output.stdout.should.match(/\* --verbose/);
162+
output.stdout.should.match(/\* --negate/);
164163
output.stdout.should.match(/\* --silent/);
164+
output.stdout.should.match(/\* --verbose/);
165165
output.stdout.should.match(/\* --version/);
166166
});
167167
});

0 commit comments

Comments
 (0)