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

Next revision
Previous revision
gams:getting_command_line_arguments_into_a_model [2007/08/09 16:14]
127.0.0.1 external edit
gams:getting_command_line_arguments_into_a_model [2021/05/06 21:54]
Atharv Bhosekar removed
Line 3: Line 3:
 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;''?//​ 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;''?//​
  
-The GAMS command line arguments ​ ''​user1''​ to ''​user5'' ​ will do the job. 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. ​+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>​ The example from above could be done like this:<​code>​
Line 15: Line 31:
 SCALE = %myscale%; SCALE = %myscale%;
 </​code>​ </​code>​
 +
 +