Boost program options environment variables




















Instead of calling this function for every command-line option, Example It uses the overloaded operator operator , which you can call to pass the required data to define a command-line option. This operator returns a reference to the same proxy object, which allows you to call operator multiple times.

The first command-line option is --help. You set --help on the command line or omit it. You can specify short names for command-line options. A short name must consist of just one letter and is set after a comma. Now the help can be displayed with either --help or -h. Besides --help , two more command-line options are defined: --pi and --age. Both --pi and --age expect to be set to a value.

Thus, the command-line option --age expects an integer and --pi expects a floating point number. That function is then called with the value of the command-line option. In Example After all command-line options have been defined, you use a parser. This function takes argc and argv , which define the command line, and desc , which contains the option descriptions. For example, you can call count to check whether a certain command-line option has been used and is stored in the container.

You can get the object of type boost::any from the member function value. This member function converts the value of a command-line option to the type passed as a template parameter.

Be sure the type you pass to as matches the type of the command-line option. For example, Example You can start Example Here is one example:. In this case Pi: 3.

The output is now On age: 29 and Age: There is no output for --pi because the program uses else if statements that only display the value set with --pi if --age is not set. As you can see, the help can be shown in two different ways because a short name for that command-line option was defined. For --pi the default value is displayed. The command-line options and their descriptions are formatted automatically.

The exception is caught, and an error message is written to standard output. However, there are some notable differences. Can be NULL. The class allows one to specify all the information needed for parsing and to parse the command line.

The 'args' parameter should not include program name. The parameters should be the same as passed to 'main'. Throws on error. Read from given stream. Read from file with the given name. The character type is passed to the file stream. Returns the vector of origianl tokens for all collected options. Viewed 1k times. Add a comment. Active Oldest Votes. Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back



0コメント

  • 1000 / 1000