-
-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add stdin and input handling #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,14 @@ var cli = meow({ | |
Object.keys(cli.flags).forEach(function (el) { | ||
console.log(el); | ||
}); | ||
|
||
cli.stdinOrInput() | ||
.then(function (input) { | ||
input.forEach(function (element) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main use-case is one input argument, so would really like to optimize for that, but still support the edge-case of multiple arguments. |
||
console.log(element); | ||
}); | ||
}) | ||
.catch(function (err) { | ||
console.error(err.message); | ||
process.exit(1); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,15 @@ var redent = require('redent'); | |
var readPkgUp = require('read-pkg-up'); | ||
var loudRejection = require('loud-rejection'); | ||
var normalizePackageData = require('normalize-package-data'); | ||
var getStdin = require('get-stdin'); | ||
var Promise = require('pinkie-promise'); | ||
|
||
// get the uncached parent | ||
delete require.cache[__filename]; | ||
var parentDir = path.dirname(module.parent.filename); | ||
|
||
global.Promise = Promise; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't it against the rules or am I missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
module.exports = function (opts, minimistOpts) { | ||
loudRejection(); | ||
|
||
|
@@ -80,6 +84,19 @@ module.exports = function (opts, minimistOpts) { | |
flags: camelcaseKeys(argv), | ||
pkg: pkg, | ||
help: help, | ||
showHelp: showHelp | ||
showHelp: showHelp, | ||
stdinOrInput: function () { | ||
if (!_ && process.stdin.isTTY) { | ||
return Promise.reject(new Error('input required')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
if (_.length > 0) { | ||
return Promise.resolve(_); | ||
} | ||
|
||
return getStdin().then(function (data) { | ||
return [data]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is array returned instead of data? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
}); | ||
} | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,9 +44,9 @@ test('spawn cli and show help screen', async t => { | |
}); | ||
|
||
test('spawn cli and test input', async t => { | ||
const {stdout} = await execa('./fixture.js', ['-u', 'cat']); | ||
const {stdout} = await execa('./fixture.js', ['foo', '-u', 'cat']); | ||
|
||
t.is(stdout, 'u\nunicorn\nmeow'); | ||
t.is(stdout, 'u\nunicorn\nmeow\nfoo'); | ||
}); | ||
|
||
test.serial('pkg.bin as a string should work', t => { | ||
|
@@ -71,3 +71,11 @@ test('type inference', t => { | |
t.is(fn({argv: ['5'], inferType: true}, {string: ['foo']}).input[0], 5); | ||
t.is(fn({argv: ['5'], inferType: true}, {string: ['_', 'foo']}).input[0], 5); | ||
}); | ||
|
||
test('stdinOrInput', async t => { | ||
const stdin = await execa.shell('echo \'foo bar\' | ./fixture.js'); | ||
const input = await execa('./fixture.js', ['foo', 'bar']); | ||
|
||
t.is(stdin.stdout, 'meow\nfoo bar\n'); | ||
t.is(input.stdout, 'meow\nfoo\nbar'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't how stdin is intended. At least, this is completely counterintuitive for any other program out there. Usually how it works is This assumes the command line input is instead input as well. This will confuse a ton of people if Meow's code handling assumes this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a big difference in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not what I mean. Usually programs that accept filenames as arguments are the ones that employ the use of For instance: cat > some-file.txt <<EOF
some text
EOF
grep "text" some-file.txt
echo "some text" | grep "text"
sed 's/some/thing/' some-file.txt
echo "some text" | sed 's/some/thing/' Instead, this PR seems to support the usage of: cat > some-prog.js <<EOF
// ...
console.log(meow.input);
// ...
EOF
some-prog.js This Is Some Text
# doing the same as
echo This Is Some Text | some-prog.js Which is going to confuse a lot of people. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sure, but I also often use it for CLI's that accept text input too, to make it easier for everyone to use it. Then they could use either of these:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I take back my last comment. Better to use stdin for just string input. No point in doing both, and stdin should be preferred as it works nicely in a pipe chain. |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
cli.autoInput()
orcli.getInput()
or something along those lines would look better and shorter?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for
.getInput()
, even though it might be kinda general and it's intent unclear. If it's well documented I think that solves that, and it's easier to remember.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
.getInput()
.