简体   繁体   中英

Optional command line arguments

My program receives three arguments:

int bufferSize
int priority
int milliTimeOut

Is there a way to make some of these params optional (and to set these params value as default values)?

For example, if the user want to leave priority to be the default priority, but to insert bufferSize and milliTimeOut , what does he type when he runs my program, and how do I check it in my program?

You can implement options , as you often see on Linux.

$ program.exe -b 100 -p 1 -m 100

where -b stands for bufferSize followed by a value for it. Similarly, -p and -m stand for priority and milliTimeOut , respectively, followed by a value for them.

You write your program to handle this, that is, first parse the option , then its value. Then you can know which options are missing on the command line. Once you know the missing option(s), you can take the default value(s) for it (them).

For example,

$ program.exe -b 100 -m 100

where -p is missing, which means, priority needs to have its default value, decided by your program.

Similarly,

$ program.exe -b 100 

where priority and milliTimeOut are defaulted.

It depends entirely on how you parse command line parameters. If you use Boost program_options module you can do this way:

#include <boost/program_options.hpp>
namespace po = boost::program_options;

int main(int argc, char *argv[])
{
    int bufferSize;
    int priority;
    int milliTimeOut;

    po::options_description desc("Options for my program");
    desc.add_options()
        // Option 'buffer-size' and 'b' are equivalent.
        ("buffer-size,b", po::value<int>(& bufferSize)->default_value(8192),
            "The buffer's size")
        // Option 'priority' and 'p' are equivalent.
        ("priority,p", po::value<int>(& priority)->default_value(4),
            "The priority")
        // Option 'timeout' and 't' are equivalent.
        ("timeout,t", po::value<int>(& milliTimeOut)->default_value(30000),
            "Time out in ms")
        ;

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    // Here your variables have the values specified by the user,
    // or the default one.
}

Then you call your program like:

program --buffer-size=100000 -t 600000
program -b 100000 --priority=10
program -b 5000 -t 50000 -p 15
program

The parameters you do not specify get their default value.

You should read the package documentation , it is quite complete and flexible (eg you can have default value only for some parameters).

Yes, you can make arguments optional, and supply default values if none is entered on the command line. Just for example, you could use -b to specify a buffer size, and assume 64K if none is entered:

int main(int argc, char **argv) { 

    int bufferSize = 65536;

Then comes the tedious code to check whether a command line argument starts with -b , and if so, read the desired buffer size from the rest of that argument.

You might want to use Boost program_options to ease that somewhat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM