[SERVER-5154] Shell script should be able to receive command line arguments Created: 01/Mar/12  Updated: 25/Oct/21  Resolved: 25/Oct/21

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 2.1.0
Fix Version/s: features we're not sure of

Type: Improvement Priority: Major - P3
Reporter: Srimonti Karmakar Assignee: Geert Bosch
Resolution: Won't Do Votes: 5
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All


Participants:

 Description   

It should be possible to run

./mongo dbname myscript.js --myarg value

where myarg would be passed to myscript as a command line argument.



 Comments   
Comment by Srimonti Karmakar [ 03/Mar/12 ]

The feature request suggests use of ordering to distinguish between arguments to be passed to the script versus options set for the shell.

Since the structure is
./mongo [server:port/dbname] [options] [myscript.js]

adding args for the script through
./mongo [server:port/dbname] [options] [myscript.js [my_args]]

should able to detect correct vs incorrect

$ ./mongo --host myDataBaseHost foo.js #correct
$ ./mongo foo.js --hots #correct (as far as shell is concerned, script may report error)
$ ./mongo --hots myDataBaseHost foo.js #incorrect

Comment by Andy Schwerin [ 02/Mar/12 ]

A typical approach to the "duped parameter names" problem on *nix systems is to make use of ordering. Arguments before the script go to the interpreter, and those after go to the script's own "ARGV" array, or equivalent.

Comment by Tad Marshall [ 02/Mar/12 ]

Discussed in http://groups.google.com/group/mongodb-user/browse_thread/thread/fa93bb09e73a4a37

My comment:
Another workaround is to combine --eval with your JavaScript file on the command line, for example:
$ cat addthem.js
printjson( param1 + param2 );
$ ./mongo --nodb --quiet --eval "var param1=7, param2=8" addthem.js
15

If the intent is that --myarg is the parameter name, then this would be hard to do this way without losing the ability to reject invalid command line arguments.

$ ./mongo --host myDataBaseHost foo.js # connect to host myDataBaseHost
$ ./mongo --hots myDataBaseHost foo.js # pass "hots" parameter to script?

We also have the risk of breaking existing parameter-passing command lines if we introduce a new command line option.

Generated at Thu Feb 08 03:08:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.