-1

I need to parse command options as given below in C:

./myBinary --option1 name --option2 age --option3 address

Getopt only support -l , -a kind of flags. Any suggestions?

  • C language has no notion of command line parsing, only additional **libraries** have. Standard BSD and POSIX versions only accept single character options, but GNU also defines `getopt_long` which is able to process multi-character options. – Serge Ballesta Dec 16 '21 at 10:26

1 Answers1

0

The only solid way is parsing those arguments without depending on external libraries. There is no widely accepted standard for handling command line arguments.

The best you have is the standardised call to main providing access to command line arguments:

int main(int argc, char *argv[]) {
}

There are major differences between operating systems, but it's worse than that.

  • Windows tends to use /option or -option, but many tools borrow from other platforms.
  • Linux tends to go the GNU way, offering both --option and -o via getopt_long.
  • BSD and Unix tend to use only the short -o, but some applications may be ported from Linux.
  • Many macOS tools use -option but borrows from BSD and sometimes Linux.

So only GNU provides a "standard" library for parsing such options. Consider it a non-portable extension.

Long story short: you need to provide more details about your target platform(s) to get a better answer, but don't expect some perfect solution.

Cheatah
  • 1,825
  • 2
  • 13
  • 21