Processing command line parameters before and after an argument in C

So far, I have used getopt_long to parse parameters for the C command-line program.

Is there a way to stop getopt_long parsing when it gets into an argument without an option? If not, what is the best way to handle this in C?

To give an example, I would like to process commands in the same way as git and have common arguments before the command, and command line arguments after it:

 git [general options] <command> [command options] 

eg:.

 git --bare commit -a git -p --bare status -s 

-p and --bare are common parameters and can be used with all commands, while -a is specific to the commit command and -s is specific to the status command.

Using getopt_long , they will first try to analyze all the parameters, and then leave the arguments without parameters processed. Ideally, I would like to stop parsing as soon as I delete the unnecessary (i.e., Command), and then pass the remaining arguments to the command-specific parameter parser.

+4
source share
1 answer

The GNU Getopt manual says:

POSIX requires the following behavior: the first option without options stops processing options. This mode is selected either by setting the environment variable POSIXLY_CORRECT, or by the beginning of a line of parameter arguments with a plus sign ("+").

+6
source

All Articles