getopt icon indicating copy to clipboard operation
getopt copied to clipboard

Shift all non-option arguments to the end

Open eriknyquist opened this issue 6 years ago • 2 comments

Hey, I was looking for exactly this for some project that I wanted to work on Linux & Windows (see https://github.com/eriknyquist/BrainfuckIntern), except that I didn't want the non-option arguments to be required at the end, so I added support for having non-option arguments wherever you like. Here it is in case you want to pull it in. Thanks!

Description:

On first invocation, run over the argument list and shift all non-option arguments to the end of the argument list. This allows non-option arguments to appear anywhere in the argument list, rather than always requiring them at the end. Tested on Debian 9 and Windows 10.

eriknyquist avatar Nov 09 '18 07:11 eriknyquist

I'm happy to see it my library was useful for you. I understand you're sharing your changes more to be nice than because of a real need to get it into my repository. For this project I'm not really interested in these changes, and I'll document those reasons for anyone following along.

I'm going for essentially the bare minimum that meets the POSIX specification. The only extraneous feature is the ability to reset the parser with optind=0, which is primarily there for testing. While it's
a nice feature, the specification technically doesn't permit permutation due to the const in the prototype.

When I want something fancier, I pull in my other getopt()-like argument parsing library instead: optparse. It supports permutation (the default) long options, and has no global state.

https://github.com/skeeto/optparse

My getopt() implementation, a single function, it doesn't introduce any symbols that are not strictly part of the interface. (The remaining namespace issue is that, due to being a header library, it could interact improperly with user-defined macros.) This isn't essential, but it's pretty cool. This extra permutation feature, as written, adds new helper functions.

My implementation is strict ANSI C89 while this PR is C99. Trivial to fix, but it also trivially doesn't currently pass the tests (make check).

Still, thanks for opening this PR and contributing back! Someone may come across this in the future and prefer your version, and this PR helps them find it.

skeeto avatar Nov 26 '18 22:11 skeeto

no worries. I've got what I need, I was indeed just being nice 😊

eriknyquist avatar Nov 26 '18 23:11 eriknyquist