Properties interface provides several operations for converting properties to and from command-line options.
On this page:
Converting Properties to Command-Line Options
getCommandLineOptions operation converts an initialized set of properties into a sequence of equivalent command-line options:
For example, if you have set the
Filesystem.MaxFileSize property to 1024 and call
getCommandLineOptions, the setting is returned as the string
"--Filesystem.MaxFileSize=1024". This operation is useful for diagnostic purposes, for example, to dump the setting of all properties to a logging facility, or if you want to fork a new process with the same property settings as the current process.
Converting Command-Line Options to Properties
parseCommandLineOptions operation examines the passed argument vector for command-line options that have the specified prefix:
All options having the form
--prefix.Key=Value are converted to property settings (that is, they initialize the corresponding properties). The operation returns an argument vector containing the options that did not match the prefix.
The value for
prefix has an implicit trailing period if one is not present. For example, when calling
parseCommandLineOptions with a prefix value of
"File", the option
--File.Owner=root would match but the option
--Filesystem.MaxFileSize=1024 would not match.
The operation parses command-line options using the same syntax rules as for properties in a configuration file. However, the user's command shell can cause differences in parsing behavior. Suppose we define the following property in a configuration file:
The presence of whitespace in the property definition is not an issue in a configuration file but can be an issue on the command line, where the equivalent option is
--MyApp.Home=C:\Program Files\MyApp. If the user is not careful, the program may receive this as two separate options:
Files\MyApp. In the end, it is the user's responsibility to ensure that the property's complete key and value are contained within a single command-line option.
parseCommandLineOptions expects a sequence of strings, but C++ programs are used to dealing with
argv, Ice provides two utility functions that convert an
argv vector into a sequence of strings and vice-versa:
You need to use
parseCommandLineOptions (and the utility functions) if you want to permit application-specific properties to be set from the command line. For example, to allow the
--Filesystem.MaxFileSize option to be used on the command line, we need to initialize our program as follows:
Using this code, any options beginning with
--Filesystem are converted to properties and are available via the property lookup operations as usual. The call to
initialize then removes any Ice-specific command-line options so, once the communicator is created,
argv only contains options and arguments that are not related to setting either a filesystem or an Ice property.
An easier way to achieve the same thing is to use the overload of
Ice::initialize that accepts a string sequence, instead of an
This version of the code avoids having to convert the string sequence back into an
argv pair before calling
Converting Reserved Command-Line Options to Properties
parseIceCommandLineOptions operation behaves like
parseCommandLineOptions, but removes the reserved Ice-specific options from the argument vector:
This operation is also used internally by the Ice run time to parse Ice-specific options in