From 123f95c940e1413af68460a18df38f87bea7d307 Mon Sep 17 00:00:00 2001 From: David Konsumer Date: Fri, 7 Apr 2017 22:55:56 -0700 Subject: [PATCH 1/2] cleaner arg parsing --- cli.js | 66 ++++++++++++++++++++++++---------------------------- package.json | 3 ++- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/cli.js b/cli.js index 1d97c02..70f4a79 100755 --- a/cli.js +++ b/cli.js @@ -1,42 +1,36 @@ #!/usr/bin/env node var Blipper = require('./index.js') -var _ = require('highland') var client = new Blipper() -var command = process.argv[2] || 'help' - -var commands = { - profile: function () { - return client.getProfile().map(function (profile) { - return 'name: ' + profile.username + '\n' + - 'about: ' + profile.about + '\n' + - 'id: ' + profile.id - }) - }, - name: client.setUsername, - about: client.setAbout, - say: client.post, - add: client.addFollowee, - feed: function () { - return client.getFeed().map(function (post) { - return (post.author.username || post.author.id) + ' - ' + (new Date(post.time)).toString() + '\n' + - ' ' + post.content + '\n' - }) - }, - help: function () { - return _.of( - 'usage: blipper []\n' + - 'commands:\n' + - ' name Set your username\n' + - ' about Set your about info\n' + - ' profile View our username, about, and unique node id\n' + - ' say Say something which your followers will see in their feeds\n' + - ' add Add unique id of another node to follow\n' + - ' feed Show your feed of your posts and posts by nodes you follow\n' - ) - } -} - -commands[command].apply(client, process.argv.slice(3)).invoke('toString').append('\n').pipe(process.stdout) +const arg = require('yargs') + .usage('Usage: $0 [options]') + .command('name ', 'Set your username') + .command('about ', 'Set your about info') + .command('profile', 'View our username, about, and unique node id') + .command('say ', 'Say something which your followers will see in their feeds') + .command('add ', 'Add unique id of another node to follow') + .command('feed', 'Show your feed of your posts and posts by nodes you follow') + .help('h') + .alias('h', 'help') + .demandCommand(2) + .argv + +const commands = {} + +commands.profile = () => client.getProfile().map(profile => `name: ${profile.username} +about: ${profile.about} +id: ${profile.id} +`) + +commands.feed = client.getFeed().map(post => (`${post.author.username || post.author.id} - ${new Date(post.time)} +${post.content} +`)) + +commands.name = client.setUsername +commands.about = client.setAbout +commands.say = client.post +commands.add = client.addFollowee + +commands[arg._].apply(client, process.argv.slice(3)).invoke('toString').append('\n').pipe(process.stdout) diff --git a/package.json b/package.json index 64d0307..9963fa3 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "dependencies": { "highland": "^2.10.2", - "superagent": "^3.0.0" + "superagent": "^3.0.0", + "yargs": "^7.0.2" } } From 357c4cff6f9592bad1a07a3385d297bb491d5e4b Mon Sep 17 00:00:00 2001 From: David Konsumer Date: Fri, 7 Apr 2017 23:07:44 -0700 Subject: [PATCH 2/2] simplified, added examples --- cli.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/cli.js b/cli.js index 70f4a79..edd82be 100755 --- a/cli.js +++ b/cli.js @@ -6,12 +6,18 @@ var client = new Blipper() const arg = require('yargs') .usage('Usage: $0 [options]') - .command('name ', 'Set your username') + .command('name ', 'Set your username') .command('about ', 'Set your about info') .command('profile', 'View our username, about, and unique node id') .command('say ', 'Say something which your followers will see in their feeds') .command('add ', 'Add unique id of another node to follow') .command('feed', 'Show your feed of your posts and posts by nodes you follow') + + .example('$0 name konsumer', 'Set your name to "konsumer"') + .example('$0 about "Mostly human"', 'Set your about-text') + .example('$0 say "Hey planet!"', 'Say something') + .example('$0 add QmUNfYBRgXqV1NDDiTws9vvFWtijZwMZCu6KENEVWGcG7k', 'Follow @protometa') + .help('h') .alias('h', 'help') .demandCommand(2) @@ -24,13 +30,13 @@ about: ${profile.about} id: ${profile.id} `) -commands.feed = client.getFeed().map(post => (`${post.author.username || post.author.id} - ${new Date(post.time)} +commands.feed = () => client.getFeed().map(post => (`${post.author.username || post.author.id} - ${new Date(post.time)} ${post.content} `)) -commands.name = client.setUsername -commands.about = client.setAbout -commands.say = client.post -commands.add = client.addFollowee +commands.name = () => client.setUsername(arg.your_username) +commands.about = () => client.setAbout(arg.info) +commands.say = () => client.post(arg.your_message) +commands.add = () => client.addFollowee(arg.friend_id) -commands[arg._].apply(client, process.argv.slice(3)).invoke('toString').append('\n').pipe(process.stdout) +commands[arg._]().invoke('toString').append('\n').pipe(process.stdout)