Phantom Documentation

[Help Home] [Phantom Home]
options

The options data type stores a set of key/value pairs. Each key is associated with a value, and the value can be retrieved by specifying the key. Each key can be in the set only one time, but the values can take any value. Both the key and value are strings. Each key value pair is separated by a user-defined string token (see SetToken).

Options can be saved to a file and reloaded during a later run. This can provide some persistence between runs if a state needs to be kept. Options files are ASCII text files with the option keys and values in them.

Additionally, options files can be commented, with a special user-defined comment token (see SetComment). Any text after the comment token is ignored when options are loaded. However, comments are not retained and saved when an options set is saved to a file.

The regkey data type supports the following member functions:

AddOption
Clear
GetCount
GetKey
GetOption
HasKey
List
Load
Save
SetComment
SetOption
SetToken

Example Code

# Declare an options variable
options o;

# Set the token separator for the options
o.SetToken(":");

# Set the comment indicator for the options
# Anthing in the file after # will be ignored
# (although, does nothing here)
o.SetComment("#");

# Display the options (0 Keys)
disp(o);

# Add some option key/value sets
o.AddOption("Name", "Phantom");
o.AddOption("Occupation", "Automation");
o.AddOption("Age", "Timeless");

# Display the numnber of keys
disp("Key Count: " + string(o.GetCount()));

# List the values in the options
disp("The Values are: ");
disp(o.List());

# Save the options
o.Save("test_options.txt");

# Clear the options
o.Clear();

# Display the options, verifying that
# the keys have been cleared
disp(o);

# Reload the options from the file
o.Load("test_options.txt");

# Set the 'Age' option to a new value
o.SetOption("Age", "Timeless + 1");

# Display the loaded and changed option
# count and values
disp("Key Count: " + string(o.GetCount()));
disp("The Values are: ");
disp(o.List());

# Get only the 'Name' option
disp("Name only: ");
disp(o.GetOption("Name"));

# Get the 3rd key in the option set
disp("The 3rd key (index 0) is: ");
disp(o.GetKey(2));

# Get the value associated with the
# third key in the option set
disp("The 3rd value (index 0) is: ");
disp(o.GetOption(o.GetKey(2)));

# Look for the key 'Age'
if(o.HasKey("Age")){
  disp("Option 'Age': " + o.GetOption("Age"));
}



Output
0 Keys
Key Count: 3
The Values are:
Age:Timeless
Name:Phantom
Occupation:Automation

0 Keys
Key Count: 3
The Values are:
Age:Timeless + 1
Name:Phantom
Occupation:Automation

Name only:
Phantom
The 3rd key (index 0) is:
Occupation
The 3rd value (index 0) is:
Automation
Option 'Age': Timeless + 1


See Also: Variables


Copyright 2000-2011 Phantom Automated Solutions