Skip to content
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

Support piping #137

Closed
mathiasbynens opened this issue Mar 18, 2013 · 3 comments
Closed

Support piping #137

mathiasbynens opened this issue Mar 18, 2013 · 3 comments

Comments

@mathiasbynens
Copy link

Say you want to support piping in your script, e.g.:

echo "foo bar" | ./test.js

That’s currently possible using code that looks like this:

var data = '';
function withPipe(data) {
   console.log('content was piped');
   console.log(data.trim());
}
function withoutPipe() {
   console.log('no content was piped');
}

var self = process.stdin;
self.on('readable', function() {
    var chunk = this.read();
    if (chunk === null) {
        withoutPipe();
    } else {
       data += chunk;
    }
});
self.on('end', function() {
   withPipe(data);
});

It would be neat if commander.js could somehow abstract all this away and make it easier to write scripts that accept piped content.

@jrschumacher
Copy link

👍

This is what I did:

//...
var stdin = '';

program
  .command('some-command [message]')
  .action(function(message) {
    if(stdin) {
      message = stdin;
    }
  });

if(process.stdin.isTTY) {
  program.parse(process.argv);
}
else {
  process.stdin.on('readable', function() {
      var chunk = this.read();
      if (chunk !== null) {
         stdin += chunk;
      }
  });
  process.stdin.on('end', function() {
    program.parse(process.argv); 
  });
}

@Potherca
Copy link

I did the same thing here. Works like a charm.

@shadowspawn
Copy link
Collaborator

Popular approach from @jrschumacher , thanks

This issue has not had any activity in over six months. It isn't likely to get acted on due to this report.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants