Search code examples
javaccommand-line-argumentsgetopt

how to parse parameters without leading hyphen with Getopt


I am trying to parse command line arguments using Getopt.
However, some of my arguments are from the form host myhost server myserver where host and server are constants and myhost and myserver are changing arguments.

Can I parse options like that?


Solution

  • You might be able to preprocess the argv array (preferably copying it to a new location rather than trying to modify it in-place) to conform to the expected form, then use getopt. However, that's probably a lot more work that it's worth and might still have corner cases that break. getopt is intended only for use with utilities whose options conform to the POSIX guidelines. I would just write your own argument-processing code; actually that's my preference even when getopt would work because I usually prefer data-driven (e.g. an array of structs describing the possible options/arguments, their types, and how they should be stored) command line processing to code-driven (switch statement with explicit code for each option).