qed
qed

Reputation: 23114

Multiple values for an option in std.getopt

Just as an example, I have made a program for filtering out empty lines of a file and write the result into a new file:

// dlang filter out empty lines

import std.stdio;
import std.string;
import std.getopt;

string inputFile;
string outputFile;

void main(string[] args)
{
    getopt(args, "if", &inputFile, "of", &outputFile);

    File ifh = File(inputFile, "r");
    File ofh = File(outputFile, "w");

    foreach(line; ifh.byLine) {
        line = line.chomp;
        if(line != "") {
            ofh.writeln(line);
        }
    }
}

It is quite nice and the setup is super easy, but what if I want to take multiple values for the --if option?

Upvotes: 1

Views: 122

Answers (1)

user4040057
user4040057

Reputation: 21

You can use a string[] receiver:

string[] inputFiles
getopt(args, "if", &inputFiles);
foreach(f; inputFiles) {...}

Then give multiple --if options when running the program, and they all end up in inputFiles.

See also: http://dlang.org/phobos/std_getopt.html - Array options

Upvotes: 2

Related Questions