command line processing library - getopt

后端 未结 4 790
余生分开走
余生分开走 2021-01-05 15:42

Can someone help me with the getopt function?

When I do the following in main:

char *argv1[] = {\"testexec\",\"-?\"};
char *argv2[] = {\"testexec\",\         


        
相关标签:
4条回答
  • 2021-01-05 16:11

    argv1 and 2 must end in 0:

    char* argv1[] = {"par1", "par2", 0};
    

    Edit: OK, I read the getopt man page and I found this:

    The variable optind is the index of the next element to be processed in argv. The system initializes this value to 1. The caller can reset it to 1 to restart scanning of the same argv, or when scanning a new argument vector.

    So, making optind=1 between the two calls at getopt makes it work as expected.

    0 讨论(0)
  • 2021-01-05 16:11

    Is there any reason why you are not using getopt_long() instead? On most platforms, getopt() just calls _getopt_long() with a switch to disable long arguments. That's the case with almost every platform that I know of (still in use), including Linux, BSD and even emerging OS's like HelenOS -, I know, I was the one who ported getopt to its libc :)

    It is much easier on ANYONE using your program to have long options at least until they get used to using it.

    getopt_long() will allow you to use two (or more) option indexes that can stay 'live' after they are done processing arguments, only the internal (global, non-reentrant) one would have to be re-set which is no big deal.

    This lets you easily compare the argument count to the number of options actually passed in both invocations with many other benefits .. please consider not using the antiquated interface.

    Look at getopt.h, you'll see what I mean.

    0 讨论(0)
  • 2021-01-05 16:18

    As stated in the man page:

    "A program that scans multiple argument vectors, or rescans the same vector more than once, and wants to make use of GNU extensions such as '+' and '-' at the start of optstring, or changes the value of POSIXLY_CORRECT between scans, must reinitialize getopt() by resetting optind to 0, rather than the traditional value of 1. (Resetting to 0 forces the invocation of an internal initialization routine that rechecks POSIXLY_CORRECT and checks for GNU extensions in optstring.)"

    0 讨论(0)
  • 2021-01-05 16:21

    The getopt() function uses some global variables, like optind and optarg, to store state information between calls. After you finish processing one set of options, there is data left in those variables that is causing problems with the next set of options. You could potentially try to reset getopt's state between calls by clearing the variables, but I'm not sure that would work since the function might use other variables which aren't documented and you'd never know if you'd gotten them all; besides, it would be absolutely nonportable (i.e. if the implementation of getopt() changes, your code breaks). See the man page for details. Best not to use getopt() for more than one set of arguments in a given program if you can help it.

    I'm not sure if there is an actual function to reset getopt's state (or perhaps a reentrant version of the function, which lets you store the state in your own variables)... I seem to remember seeing something like that once, but I can't find it now that I look :-/

    0 讨论(0)
提交回复
热议问题