1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
Arguments
=========
If you've launched your application via the command-line, this Array-like object can be used to read command-line arguments. This is available at `Application.args`.
The example below prints all the arguments to the screen:
```
// Method 1 (foreach)
foreach(argument in Application.args)
Console.print(argument);
// Method 2 (for loops)
for(i = 0; i < Application.args.length; i++)
Console.print(Application.args[i]);
// Or, alternatively:
Console.print(Application.args);
```
Properties
----------
#### length
`length`: number, read-only.
The number of command-line arguments, including the executable.
Functions
---------
#### get
`get(index)`
Gets the specified command-line argument. Instead of calling `get()`, one may use equivalently the `[ ]` operator.
*Arguments*
* `index`: integer number between 0 and `Application.args.length - 1`, inclusive.
*Returns*
A string with the specified command-line argument, or `null` if there is no such argument.
*Example*
```
// Suppose that you run surgescript via the command-line:
// surgescript test_args.ss
executable = Application.args[0]; // "surgescript"
script_file = Application.args[1]; // "test_args.ss"
```
#### option
`option(optionName)`
Gets the value of a certain command-line option.
*Arguments*
* `optionName`: string. The option you want to read.
*Returns*
A string featuring the value of the desired command-line option, or `null` if such an option hasn't been provided by the user.
*Example*
```
// Suppose that you run surgescript via the command-line:
// surgescript test_args.ss --my-option 12345
my_option = Application.args.option("--my-option");
// Options -p and --port are equivalent
// surgescript test_args.ss -p 80
// surgescript test_args.ss --port 80
port = Application.args.option("--port") || Application.args.option("-p");
// Default values (useful if the option is not present)
// surgescript test_args.ss
// surgescript test_args.ss --name alice
name = Application.args.option("--name") || "anonymous";
```
#### hasOption
`hasOption(optionName)`
Checks if the specified option is present in the command-line.
*Arguments*
* `optionName`: string.
*Returns*
Returns `true` if the specified option is present in the command-line; or `false` otherwise.
#### iterator
`iterator()`
Spawns an iterator.
*Returns*
An iterator to iterate over the command-line arguments.
#### toString
`toString()`
Converts the command-line arguments to a string.
*Returns*
A string featuring the command-line arguments.
|