Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from cloudflare/cyclops
Browse files Browse the repository at this point in the history
refactor(bin): have a tool create the help message.
  • Loading branch information
terinjokes committed May 14, 2015
2 parents 8724236 + adb1b26 commit 91916de
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 64 deletions.
62 changes: 33 additions & 29 deletions bin/cli.js
Expand Up @@ -2,39 +2,43 @@
'use strict';
var VERSION = require('../lib/version');
var byte = require('8bits');
var parseArgs = require('minimist');
var argv = parseArgs(process.argv.slice(2), {
'boolean': [
'help',
'version'
],
string: [
'headers',
'verbose',
'forbidden'
],
alias: {
h: 'help',
H: 'headers',
v: 'verbose',
V: 'version',
x: 'forbidden'
},
'default': {
depth: 10,
verbose: 0,
forbidden: '^(?:https?:)?(?:\/+)?(localhost|(?:127|192.168|172.16|10).[0-9.]+)'
}
var allowedArgs = [{
name: 'forbidden',
abbr: 'x',
'default': '^(?:https?:)?(?:/+)?(localhost|(?:127|192.168|172.16|10).[0-9.]+)',
help: 'Forbidden URLs (passed to the RegExp constructor).'
}, {
name: 'headers',
abbr: 'H',
help: 'Custom headers (curl style) to set on all requests.'
}, {
name: 'verbose',
abbr: 'V',
'default': 0,
help: 'Verbosity of logging output. 1 is errors, 2 is all.'
}, {
name: 'version',
abbr: 'v',
'boolean': true,
help: 'Print the version number.'
}, {
name: 'help',
abbr: 'h',
'boolean': true,
help: 'Show this usage information.'
}];

var clopts = require('cliclopts')(allowedArgs);
var argv = require('minimist')(process.argv.slice(2), {
alias: clopts.alias(),
'boolean': clopts.boolean(),
'default': clopts.default()
});

if (argv.help) {
console.log('Usage: ' + process.argv.slice(0, 2).join(' ') + ' <options>\n');
console.log('Usage: ' + process.argv.slice(1, 2).join(' ') + ' [options]\n');
console.log('Options:');
console.log('-h, --help Show this usage information.');
console.log('-H, --headers Custom headers (curl style) to set on all requests.');
console.log('-v, --verbose Verbosity of logging output. 1 is errors, 2 is all. [default: 0]');
console.log('-V, --version Print the version number.');
console.log('--forbidden Forbidden URLs (passed to the RegExp constructor) [default: "^(?:https?:)?(?:/+)?(localhost|(?:127|192.168|172.16|10).[0-9.]+)"]');
clopts.print();
/* eslint-disable no-process-exit */
process.exit(0);
/* eslint-enable no-process-exit */
Expand Down
72 changes: 38 additions & 34 deletions bin/server.js
Expand Up @@ -5,45 +5,49 @@ var http = require('http');
var url = require('url');
var collapsify = require('../');
var systemdSocket = require('systemd-socket');
var parseArgs = require('minimist');
var fds = require('fds');
var argv = parseArgs(process.argv.slice(2), {
'boolean': [
'help',
'version'
],
string: [
'headers',
'verbose',
'forbidden',
'port'
],
alias: {
h: 'help',
H: 'headers',
v: 'verbose',
V: 'version',
p: 'port',
x: 'forbidden'
},
'default': {
port: 8020,
depth: 10,
verbose: 0,
forbidden: '^(?:https?:)?(?:\/+)?(localhost|(?:127|192.168|172.16|10).[0-9.]+)'
}
var allowedArgs = [{
name: 'forbidden',
abbr: 'x',
'default': '^(?:https?:)?(?:/+)?(localhost|(?:127|192.168|172.16|10).[0-9.]+)',
help: 'Forbidden URLs (passed to the RegExp constructor).'
}, {
name: 'headers',
abbr: 'H',
help: 'Custom headers (curl style) to set on all requests.'
}, {
name: 'port',
abbr: 'p',
'default': 8020,
help: 'Port that Collapsify should listen on. Ignored when running as a systemd service.'
}, {
name: 'verbose',
abbr: 'V',
'default': 0,
help: 'Verbosity of logging output. 1 is errors, 2 is all.'
}, {
name: 'version',
abbr: 'v',
'boolean': true,
help: 'Print the version number.'
}, {
name: 'help',
abbr: 'h',
'boolean': true,
help: 'Show this usage information.'
}];

var clopts = require('cliclopts')(allowedArgs);
var argv = require('minimist')(process.argv.slice(2), {
alias: clopts.alias(),
'boolean': clopts.boolean(),
'default': clopts.default()
});

if (argv.help) {
console.log('Usage: ' + process.argv.slice(0, 2).join(' ') + ' <options>\n');
console.log('Usage: ' + process.argv.slice(1, 2).join(' ') + ' <options>\n');
console.log('Options:');
console.log('-h, --help Show this usage information.');
console.log('-H, --headers Custom headers (curl style) to set on all requests.');
console.log('-p, --port The port for Collapsify to listen on. [default: 8020]');
console.log(' Ignored when running under systemd');
console.log('-v, --verbose Verbosity of logging output. 1 is errors, 2 is all. [default: 0]');
console.log('-V, --version Print the version number.');
console.log('--forbidden Forbidden URLs (passed to the RegExp constructor) [default: "^(?:https?:)?(?:/+)?(localhost|(?:127|192.168|172.16|10).[0-9.]+)"]');
clopts.print();
/* eslint-disable no-process-exit */
process.exit(0);
/* eslint-enable no-process-exit */
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/logger.js
Expand Up @@ -9,7 +9,7 @@ module.exports = function(options) {
];
var logger = bunyan.createLogger({
name: 'collapsify',
level: levels[options.v] || 'warn',
level: levels[options.verbose] || 'warn',
serializers: bunyan.stdSerializers
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -18,6 +18,7 @@
"bluebird": "^2.9.10",
"bunyan": "^1.3.2",
"clean-css": "^2.1.6",
"cliclopts": "^1.1.0",
"fds": "^1.0.0",
"he": "^0.5.0",
"htmlparser2": "^3.5.1",
Expand Down

0 comments on commit 91916de

Please sign in to comment.