command line arguments

2 views
Skip to first unread message

Christoph Dorn

unread,
Sep 20, 2011, 4:16:47 PM9/20/11
to gp...@googlegroups.com
How do I get the command line arguments?

SYSTEM.args is not available.

Christoph

Donny Viszneki

unread,
Sep 20, 2011, 5:37:21 PM9/20/11
to gp...@googlegroups.com
You want the "system" module (not "SYSTEM" module, nor a global
variable SYSTEM) which contains an "args" property that is an array
where element 0 is the path of your command (not the interpreter,
which is gsr) and each following element is each command-line argument
given after the command. ergo:

$ cat test-argv.js
#!/usr/bin/gsr
print(require('system').args)
$ gsr -f test-argv.js
test-argv.js
$ gsr -f test-argv.js foo
test-argv.js,foo
$ gsr -f test-argv.js foo bar baz
test-argv.js,foo,bar,baz

If you have executable permission, you can also invoke this way:

$ ./test-argv.js hello world
./test-argv.js,hello,world

This demonstrates what I mean by "path to your command"

$ `pwd`/test-argv.js hello world
/home/hdon/test-argv.js,hello,world

If you need anything else we're also in #gpsee on irc.freenode.net

--
http://codebad.com/

Christoph Dorn

unread,
Sep 20, 2011, 8:05:53 PM9/20/11
to gp...@googlegroups.com
Donny Viszneki wrote:
> You want the "system" module (not "SYSTEM" module, nor a global
> variable SYSTEM)
Sorry, I am being too biref.

var SYSTEM = require("system");

> which contains an "args" property that is an array
> where element 0 is the path of your command (not the interpreter,
> which is gsr) and each following element is each command-line argument
> given after the command. ergo:

Hmm.

for(var i in SYSTEM)
print("property: "+ i);

Yields:

key: stdin
key: stdout
key: stderr
key: platform
key: global
key: env

No "args", but SYSTEM.args is indeed there.

Thanks!
Christoph

Donny Viszneki

unread,
Sep 20, 2011, 8:16:59 PM9/20/11
to gp...@googlegroups.com
On Tue, Sep 20, 2011 at 8:05 PM, Christoph Dorn
<chri...@christophdorn.com> wrote:
> for(var i in SYSTEM)
>  print("property: "+ i);
>
> No "args", but SYSTEM.args is indeed there.

Thanks for finding this out :)

Not all properties are enumerable in Javascript. This means that
several means of property "discovery" (formally called enumeration in
Javascript) overlook those properties.

Please try print(require("system").args) and see what you get!

--
http://codebad.com/

Christoph Dorn

unread,
Sep 20, 2011, 8:26:04 PM9/20/11
to gp...@googlegroups.com
Donny Viszneki wrote:
> Thanks for finding this out:)
>
> Not all properties are enumerable in Javascript. This means that
> several means of property "discovery" (formally called enumeration in
> Javascript) overlook those properties.
Right. I figured as much once you assured me it was there.

> Please try print(require("system").args) and see what you get!

All good.

Christoph

Reply all
Reply to author
Forward
0 new messages