Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Argument parsing works when node is started with explicit path #9

Merged
merged 1 commit into from

3 participants

@tikonen

First argument is recognized correctly as "node" binary when started with explicit path

Example script myscript.js

cli = require('cli')
console.log(cli.argv)

Before change

$ node example.js 
[]
$ node example.js teemu ikonen
[ 'teemu', 'ikonen' ]
$ /usr/local/bin/node example.js teemu ikonen
[ '/tmp/example.js', 'teemu', 'ikonen' ]

Now

$ node example.js 
[]
$ node example.js teemu ikonen
[ 'teemu', 'ikonen' ]
$ /usr/local/bin/node example.js teemu ikonen
[ 'teemu', 'ikonen' ]
@tikonen tikonen First argument is recognized correctly as "node" binary when started …
…with explicit path e.g. /to/some/path/node myscript.js
da16289
@chriso
Owner

Awesome thanks

@chriso chriso closed this
@chriso chriso merged commit 99e9a5c into chriso:master
@jsen-

But what if the first argument is not 'node' binary at all?
Sometimes during the development I'm running my scripts with node-dev as the node executable, and in this case cli does not cooperate.

my proposal is to alter cli.setArgv method as follows:

  if (process.execPath === cli.app) {
    cli.app = arr.shift();
   }
@chriso chriso referenced this pull request from a commit
@chriso Use process.execPath, re #9 919ee05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 11, 2011
  1. @tikonen

    First argument is recognized correctly as "node" binary when started …

    tikonen authored
    …with explicit path e.g. /to/some/path/node myscript.js
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 3 deletions.
  1. +4 −3 cli.js
View
7 cli.js
@@ -28,7 +28,8 @@ var cli = exports,
argv, curr_opt, curr_val, full_opt, is_long,
short_tags = [], opt_list, parsed = {},
usage, argv_parsed, command_list,
- daemon, daemon_arg, no_color, show_debug;
+ daemon, daemon_arg, no_color, show_debug,
+ path = require('path');
cli.app = null;
cli.version = null;
@@ -149,7 +150,7 @@ cli.setArgv = function (arr, keep_arg0) {
}
cli.app = arr.shift();
//Strip off argv[0] if it's 'node'
- if (!keep_arg0 && 'node' === cli.app) {
+ if (!keep_arg0 && 'node' === path.basename(cli.app)) {
cli.app = arr.shift();
}
cli.app = cli.native.path.basename(cli.app);
@@ -955,7 +956,7 @@ cli.daemon = function (arg, callback) {
break;
case 'log':
try {
- cli.native.fs.createReadStream(log_file, {encoding: 'utf8').pipe(process.stdout);
+ cli.native.fs.createReadStream(log_file, {encoding: 'utf8'}).pipe(process.stdout);
} catch (e) {
return cli.error('No daemon log file');
};
Something went wrong with that request. Please try again.