Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of applications use getopt "return in order" behavior?

Quoted from the getopt() Linux Manual page:

If the first character of optstring is a hyphen (-), then each nonoption argv-element is handled as if it were the argument of an option with character code 1. (This is used by programs that were written to expect options and other argv-elements in any order and that care about the ordering of the two.) The special argument double hyphens (--) forces an end of option-scanning regardless of the scanning mode.

Quoted from the manual page of Java GetOpt class:

...The second is to allow options anywhere, but to return all elements in the order they occur on the command line. When a non-option element is ecountered, an integer 1 is returned and the value of the non-option element is stored in optarg is if it were the argument to that option. For example, "-a foo -d", returns first 'a', then 1 (with optarg set to "foo") then 'd' then -1. When this "return in order" functionality is enabled, the only way to stop getopt() from scanning all command line elements is to use the special "--" string by itself as described above. An example is "-a foo -b -- bar", which would return 'a', then integer 1 with optarg set to "foo", then 'b', then -1. optind would then point to "bar" as the first non-option argv element. The "--" is discarded.

Yes, I understand what the above statements talking about, but I still cannot imagine what kind of applications use such a behaviour.

Could someone provides an example of a command-line syntax that likely used by applications that implement the "return in order" behavior?

like image 438
Astaroth Avatar asked Dec 15 '25 06:12

Astaroth


1 Answers

find is an example of a command that mixes options and non-option arguments, and cares about the order.

like image 60
Barmar Avatar answered Dec 16 '25 20:12

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!