Skip to content

Commit c1f6e98

Browse files
committed
option parsing now fully working
1 parent c147113 commit c1f6e98

File tree

4 files changed

+64
-49
lines changed

4 files changed

+64
-49
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ the `-u|--username` option:
235235
fly production --username=admin
236236
```
237237

238-
#### Storing and using properties depending on the target
238+
#### Defining and using properties depending on the target
239239

240-
`target()` takes an optional third argument to store properties for a
241-
specific target. These properties can be accessed during runtime:
240+
`target()` takes an optional third argument to define properties used by
241+
this target. Values defined in this way can be accessed during runtime.
242242

243243
```javascript
244244
plan.target('staging', {...}, {
@@ -254,16 +254,16 @@ plan.target('production', {...}, {
254254
plan.remote(function(remote) {
255255
var webRoot = plan.runtime.options.webRoot; // fly staging -> '/usr/local/www'
256256
var sudoUser = plan.runtime.options.sudoUser; // fly staging -> 'www'
257-
remote.sudo('ls -al ' + webRoot, { user: sudoUser });
257+
remote.sudo('ls -al ' + webRoot, {user: sudoUser});
258258
});
259259
```
260260

261-
These properties can be set or overwritten by passing them as named options
262-
to the `fly` command:
261+
Properties can be set and overwritten by passing them as named options to the
262+
`fly` command.
263263

264264
```bash
265265
$ fly staging --sudoUser=foo
266-
// plan.runtime.options.sudoUser -> 'foo'
266+
# plan.runtime.options.sudoUser -> 'foo'
267267
```
268268

269269
### <a name="flightplan.local(%5Btasks%2C%20%5Dfn)"></a>flightplan.local([tasks, ]fn) → this

bin/fly.js

+46-31
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,50 @@
33
var Liftoff = require('liftoff')
44
, v8flags = require('v8flags')
55
, semver = require('semver')
6-
, extend = require('util-extend')
76
, cliPackage = require('../package')
8-
, logger = require('../lib/logger')()
9-
, yargs = require('yargs');
7+
, nopt = require('nopt');
108

11-
var argv = yargs
12-
.usage('Usage: $0 [task:]destination [options]')
13-
.help('help').alias('help', 'h')
14-
.version(cliPackage.version, 'version').alias('version', 'v')
15-
.options('flightplan', {
16-
alias: 'f',
17-
//default: 'flightplan.js',
18-
describe: 'path to flightplan'
19-
})
20-
.options('username', {
21-
alias: 'u',
22-
describe: 'user for connecting to remote hosts'
23-
})
24-
.options('debug', {
25-
alias: 'd',
26-
describe: 'enable debug mode'
27-
})
28-
.options('no-color', {
29-
alias: 'C',
30-
describe: 'disable colors in output'
31-
}).argv;
9+
var knownOptions = {
10+
'flightplan': String,
11+
'username': String,
12+
'debug': Boolean,
13+
'color': Boolean,
14+
'version': Boolean,
15+
'help': Boolean
16+
};
17+
18+
var shortHands = {
19+
'f': ['--flightplan'],
20+
'u': ['--username'],
21+
'd': ['--debug'],
22+
'C': ['--no-color'],
23+
'v': ['--version'],
24+
'h': ['--help']
25+
};
26+
27+
var options = nopt(knownOptions, shortHands, process.argv, 2);
28+
29+
if(options.help) {
30+
console.log('\n' +
31+
' Usage: fly [task:]destination [options]\n\n' +
32+
' Options:\n\n' +
33+
' -h, --help output usage information\n' +
34+
' -V, --version output the version number\n' +
35+
' -f, --flightplan <file> path to flightplan\n' +
36+
' -u, --username <name> user for connecting to remote hosts\n' +
37+
' -d, --debug enable debug mode\n' +
38+
' -C, --no-color disable color output\n'
39+
);
40+
process.exit(1);
41+
}
42+
43+
if(options.version) {
44+
console.log(cliPackage.version);
45+
process.exit(1);
46+
}
3247

3348
var task = 'default';
34-
var target = argv._ ? argv._[0] : null;
49+
var target = options.argv.remain.length ? options.argv.remain[0] : null;
3550

3651
if(target && target.indexOf(':') !== -1) {
3752
target = target.split(':');
@@ -52,31 +67,31 @@ var cli = new Liftoff({
5267

5368
var invoke = function(env) {
5469
if(!target) {
55-
logger.error('Error: No target specified');
70+
console.error('Error: No target specified');
5671
process.exit(1);
5772
}
5873

5974
if(!env.configPath) {
60-
logger.error('Error: flightplan.js not found');
75+
console.error('Error: ' + (options.flightplan || 'flightplan.js') + ' not found');
6176
process.exit(1);
6277
}
6378

6479
if(!env.modulePath) {
65-
logger.error('Error: Local flightplan package not found in ' + env.cwd);
80+
console.error('Error: Local flightplan package not found in ' + env.cwd);
6681
process.exit(1);
6782
}
6883

6984
if(!semver.satisfies(env.modulePackage.version, '>=0.5.0')) {
70-
logger.error('Error: local flightplan package version should be >=0.5.0');
85+
console.error('Error: local flightplan package version should be >=0.5.0');
7186
process.exit(1);
7287
}
7388

7489
process.chdir(env.configBase);
7590
require(env.configPath);
7691
var instance = require(env.modulePath);
77-
instance.run(task, target, argv);
92+
instance.run(task, target, options);
7893
};
7994

8095
cli.launch({
81-
configPath: argv.flightplan
96+
configPath: options.flightplan
8297
}, invoke);

lib/index.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ function Flightplan() {
164164
* fly production --username=admin
165165
* ```
166166
*
167-
* #### Storing and using properties depending on the target
167+
* #### Defining and using properties depending on the target
168168
*
169-
* `target()` takes an optional third argument to store properties for a
170-
* specific target. These properties can be accessed during runtime:
169+
* `target()` takes an optional third argument to define properties used by
170+
* this target. Values defined in this way can be accessed during runtime.
171171
*
172172
* ```javascript
173173
* plan.target('staging', {...}, {
@@ -183,16 +183,16 @@ function Flightplan() {
183183
* plan.remote(function(remote) {
184184
* var webRoot = plan.runtime.options.webRoot; // fly staging -> '/usr/local/www'
185185
* var sudoUser = plan.runtime.options.sudoUser; // fly staging -> 'www'
186-
* remote.sudo('ls -al ' + webRoot, { user: sudoUser });
186+
* remote.sudo('ls -al ' + webRoot, {user: sudoUser});
187187
* });
188188
* ```
189189
*
190-
* These properties can be set or overwritten by passing them as named options
191-
* to the `fly` command:
190+
* Properties can be set and overwritten by passing them as named options to the
191+
* `fly` command.
192192
*
193193
* ```bash
194194
* $ fly staging --sudoUser=foo
195-
* // plan.runtime.options.sudoUser -> 'foo'
195+
* # plan.runtime.options.sudoUser -> 'foo'
196196
* ```
197197
*
198198
* @method target(name, hosts[, options])
@@ -253,7 +253,7 @@ Flightplan.prototype.remote = function(tasksOrFn, fn) {
253253
Flightplan.prototype.run = function(task, target, options) {
254254
options = options || {};
255255

256-
if(options.color) {
256+
if(options.color !== undefined) {
257257
logger.useColors(options.color);
258258
}
259259

@@ -268,7 +268,7 @@ Flightplan.prototype.run = function(task, target, options) {
268268
});
269269

270270
if(flights.length === 0) {
271-
logger.warn(format('"%s" is not a valid task', task));
271+
logger.error(format('No work to be done for task "%s"', task));
272272
process.exit(1);
273273
}
274274

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
"fibers": "^1.0.2",
4040
"liftoff": "^0.13.6",
4141
"minimist": "^1.1.0",
42+
"nopt": "^3.0.1",
4243
"pretty-hrtime": "^0.2.2",
4344
"prompt": "^0.2.14",
4445
"semver": "^4.1.0",
4546
"ssh2": "^0.3.6",
4647
"temp-write": "^1.1.0",
4748
"util-extend": "^1.0.1",
48-
"v8flags": "^1.0.1",
49-
"yargs": "^1.3.3"
49+
"v8flags": "^1.0.1"
5050
},
5151
"license": "MIT",
5252
"devDependencies": {

0 commit comments

Comments
 (0)