Question

I want to make a program that takes an optional parameter -a, which can have an optional value 'filename'. To do this, I want to use getopt but I can't make -a taking an optional parameter.

If I pass "a" I can do ./my_prog -a

If I pass "a:" I can do ./my_prog -a filename // filenamen is not optional here

And if I pass "a::" I can do ./my_prog -a[filename]

But is there a way to obtain the result -a [filename] // with filename being optional ?

Était-ce utile?

La solution

I don't believe what you're asking for is possible.

This is probably a good thing: what should your program do if invoked as ./my_prog -a -b where -bis another option it's supposed to take? It's not obvious whether it should use -b as the optional filename following -a, or assume the filename wasn't given and treat it as an option.

I would therefore suggest reconsidering whether the behaviour you ask for is actually a good idea.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top