Reputation: 1373
I want to make a program to be called from the command line like this:
myprogram.exe -F/-T FILE/TEXT -W FILE.WAV -P FILE.PHO -A
They are 3 parts:
So it can be:
myprogram.exe -T "Text Text, test text" -A
or:
myprogram.exe -F FILENAME -A
or:
myprogram.exe -F FILENAME -P FILENAME -W FILENAME
etc.
-A
is one function (needs the text or the file)
-W
writes a WAV file with the info from the text/file
-P
does something similar to -W
What is the best way to handle it? Analyzing argv[x]
one by one, and deciding with if
s? Something easier?
I am new to programming and using VS2008.
Upvotes: 1
Views: 257
Reputation: 2166
I'd use getopt
if you can: it's simple enough. It'll handle combined options too (like in ls -lt
instead of ls -l -t
) and it handles options with arguments as well, and the order in which they appear also doesn't matter. All this makes it "standard" to people used to command line arguments (normally order of options is irrelevant, except when giving contradictory options...).
Upvotes: 2
Reputation: 35901
this is already done by others, so I wouldn't spend all your time on it if I were you. Look at Boost's ProgramOptions for example.
Upvotes: 0
Reputation: 46607
Analyizing argv[x] one by one
That's what I would do. Maybe maintain a counter of the current element ...
unsigned int cur = 0;
if (argc <= 1) {
.. error
}
if (!strncmp(argv[cur],"-T",2)) {
.. process
++cur;
}
...
for (;cur < argc;++cur) {
if (!strncmp(argv[cur],"-F",2)) {
.. process
}
else if ...
}
There are some command line parers out there. Unix has getopt
, for example.
Upvotes: 2