SO Stinks
SO Stinks

Reputation: 3406

In C, what's the typical way to handle multiple arguments that are "list"-like?

Suppose I have some program called "combine" that takes input of "red", "green" and "blue"-type files to produce an output file (let's say "color.jpg")... BUT the number of each type is arbitrary. Let's also suppose that there's no way to determine what type the file is except through how the user classifies them. What do people usually do in this case?

For instance, on the command line, some of the approaches might be:

command red1,red2,red3 green1,green2 blue1 color.jpg

This comma-approach breaks down if commas can appear in the filenames. It's the approach I like the most though. Another idea would be

command "red1 red2 red3" "green1 green2" "blue1" color.jpg

but this approach also has trouble with spaces in names.

I could also require ASCII files containing lists giving the files of each type:

command redlist greenlist bluelist color.jpg

but this requires lugging around extra files.

Further ideas? Is there a standard LINUX way of doing this?

Upvotes: 2

Views: 90

Answers (2)

perreal
perreal

Reputation: 97948

Another way is accepting:

command -r redfile1 redfile2 -b bluefile1 blue2 blue2 -g green1

so that:

command -r red* -b blue* -g green*

is possible.

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249143

The standard way would be this:

command --red red1.jpg --red red2.jpg --blue blue1.jpg

With short options:

command -r red1.jpg -r red2.jpg -b blue1.jpg

With bash shorthand:

command -r={red1,red2}.jpg -b blue1.jpg

(The above gets expanded by the shell so it looks like the previous invocation.)

Doing things this way avoids arbitrary limitations like "no commas in filenames" and also makes your program more interoperable with standard *nix utilities like xargs and so on.

Upvotes: 3

Related Questions