User Tools

Site Tools


gams:getting_command_line_arguments_into_a_model

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
gams:getting_command_line_arguments_into_a_model [2021/05/06 21:54]
Atharv Bhosekar removed
— (current)
Line 1: Line 1:
-====== Getting Command Line Arguments into a Model ====== 
- 
-Q: //Is it possible to specify command line arguments in the GAMS call, e.g: ''​gams dosomething.gms 10''​ and in ''​dosomething.gms''​ then set some scalar to 10, e.g: ''​SCALE = $1;''?//​ 
- 
-One can pass the argument as a command line option using ''​- -''​. ​ In your GAMS program you have 
-<​code>​ 
-$if not set myscale $set myscale 10 
-</​code>​ 
-Now on the GAMS command line you can do: 
-<​code>​ 
-gams myprogram --myscale=5 
-</​code>​ 
-This has the advantage, that even if you don't define the string macro with ''​- -myscale''​ on the command line, you still have it available with some default value. Note, that ''​myscale''​ is a scoped string macro. ​ 
- 
-If you really need it global you could do the following: 
-<​code>​ 
-$if not set myscale $set myscale 10 
-$setglobal myscale %myscale% ​ 
-</​code>​ 
- 
-Another approach is to use the GAMS command line arguments ​ ''​user1''​ to ''​user5''​. On the command line (or in the IDE parameter box) you can specify ''​user1=10''​ (or ''​u1=10''​) and inside the GAMS program you can refer to the parameter by using ''​%gams.user1%''​. Note, that this is a compile time string replacement,​ so u1 to u5 are not restricted to numbers. If the string has delimiter character (e.g. space) you have to put quotes around your string. ​ 
- 
-The example from above could be done like this:<​code>​ 
-  gams dosomething.gms u1=10 
-  SCALE = %gams.user1%;​ 
-</​code>​ 
-Note that if ''​user1''​ is not defined the result would look like:''​ SCALE =;'',​ which would trigger a compilation error. In order to prevent this, you might specify a default and overwrite this if user1 is set: 
-<​code>​ 
-$set myscale 1 
-$if not %gams.user1% == ""​ $set myscale %gams.user1% 
-SCALE = %myscale%; 
-</​code>​ 
- 
  
IMPRESSUM / LEGAL NOTICEPRIVACY POLICY gams/getting_command_line_arguments_into_a_model.1620330879.txt.gz ยท Last modified: 2021/05/06 21:54 by Atharv Bhosekar