Skip to content

Commit

Permalink
Replace optimist with yargs (#63)
Browse files Browse the repository at this point in the history
Add an option `--think` to run cowsay as cowthink
  • Loading branch information
piuccio authored Aug 24, 2020
1 parent 3f3e8e3 commit c82eaaa
Show file tree
Hide file tree
Showing 4 changed files with 488 additions and 80 deletions.
136 changes: 72 additions & 64 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,83 @@
#!/usr/bin/env node
var argv = require("optimist")
.usage("Usage: $0 [-e eye_string] [-f cowfile] [-h] [-l] [-n] [-T tongue_string] [-W column] [-bdgpstwy] text\n\n" +
"If any command-line arguments are left over after all switches have been processed, they become the cow's message.\n\n" +
"If the program is invoked as cowthink then the cow will think its message instead of saying it.")
.options({
"e" : {
default : "oo"
},
"T" : {
default : " "
},
"W" : {
default : 40
},
"f" : {
default : "default"
}
})
.describe({
"b" : "Mode: Borg",
"d" : "Mode: Dead",
"g" : "Mode: Greedy",
"p" : "Mode: Paranoia",
"s" : "Mode: Stoned",
"t" : "Mode: Tired",
"w" : "Mode: Wired",
"y" : "Mode: Youthful",
"e" : "Select the appearance of the cow's eyes.",
"T" : "The tongue is configurable similarly to the eyes through -T and tongue_string.",
"h" : "Display this help message",
"n" : "If it is specified, the given message will not be word-wrapped.",
"W" : "Specifies roughly where the message should be wrapped. The default is equivalent to -W 40 i.e. wrap words at or before the 40th column.",
"f" : "Specifies a cow picture file (''cowfile'') to use. It can be either a path to a cow file or the name of one of cows included in the package.",
"r" : "Select a random cow",
"l" : "List all cowfiles included in this package."
})
.boolean(["b", "d", "g", "p", "s", "t", "w", "y", "n", "h", "r", "l"])
.argv;
const yargs = require('yargs')
.usage(`
Usage: $0 [-e eye_string] [-f cowfile] [-h] [-l] [-n] [-T tongue_string] [-W column] [-bdgpstwy] text
If any command-line arguments are left over after all switches have been processed, they become the cow's message.
If the program is invoked as cowthink then the cow will think its message instead of saying it.
`)
.options({
e: {
default: 'oo',
},
T: {
default: ' ',
},
W: {
default: 40,
type: 'number',
},
f: {
default: 'default',
},
think: {
type: 'boolean',
},
})
.describe({
b: 'Mode: Borg',
d: 'Mode: Dead',
g: 'Mode: Greedy',
p: 'Mode: Paranoia',
s: 'Mode: Stoned',
t: 'Mode: Tired',
w: 'Mode: Wired',
y: 'Mode: Youthful',
e: "Select the appearance of the cow's eyes.",
T:
'The tongue is configurable similarly to the eyes through -T and tongue_string.',
h: 'Display this help message',
n: 'If it is specified, the given message will not be word-wrapped.',
W:
'Specifies roughly where the message should be wrapped. The default is equivalent to -W 40 i.e. wrap words at or before the 40th column.',
f:
"Specifies a cow picture file (''cowfile'') to use. It can be either a path to a cow file or the name of one of cows included in the package.",
r: 'Select a random cow',
l: 'List all cowfiles included in this package.',
think: 'Think the message instead of saying it aloud.',
})
.boolean(['b', 'd', 'g', 'p', 's', 't', 'w', 'y', 'n', 'h', 'r', 'l'])
.help()
.alias('h', 'help');

const argv = yargs.argv;

if (argv.l) {
listCows();
} else if (argv.h) {
showHelp();
listCows();
} else if (argv._.length) {
say();
say();
} else {
require("get-stdin")().then(function (data) {
if (data) {
argv._ = [require("strip-eof")(data)];
say();
} else {
showHelp();
}
});
require('get-stdin')().then((data) => {
if (data) {
argv._ = [require('strip-eof')(data)];
say();
} else {
yargs.showHelp();
}
});
}

function say () {
var module = require("./index");

var think = /think$/.test(argv["$0"]);

console.log(think ? module.think(argv) : module.say(argv));
}
function say() {
const module = require('./index');
const think = /think$/.test(argv['$0']) || argv.think;

function listCows () {
require("./index").list(function(err, list) {
if (err) throw new Error(err);
console.log(list.join(" "));
});
console.log(think ? module.think(argv) : module.say(argv));
}

function showHelp () {
require("optimist").showHelp();
function listCows() {
require('./index').list((err, list) => {
if (err) throw new Error(err);
console.log(list.join(' '));
});
}
Loading

0 comments on commit c82eaaa

Please sign in to comment.