getopt does not parse optional arguments to parameters

Although not mentioned in glibc documentation or getopt man page, optional arguments to long style command line parameters require ‘equals sign’ (=). Space separating the optional argument from the parameter does not work. An example run with the test code: $ ./respond –praise John Kudos to John $ ./respond –praise=John Kudos to John $ ./respond … Read more

Optional option argument with getopts

This workaround defines ‘R’ with no argument (no ‘:’), tests for any argument after the ‘-R’ (manage last option on the command line) and tests if an existing argument starts with a dash. # No : after R while getopts “hd:R” arg; do case $arg in (…) R) # Check next positional parameter eval nextopt=\${$OPTIND} … Read more

Why use argparse rather than optparse?

As of python 2.7, optparse is deprecated, and will hopefully go away in the future. argparse is better for all the reasons listed on its original page (https://code.google.com/archive/p/argparse/): handling positional arguments supporting sub-commands allowing alternative option prefixes like + and / handling zero-or-more and one-or-more style arguments producing more informative usage messages providing a much … Read more

C getopt multiple value

I know this is quite old but I came across this in my search for a solution. while((command = getopt(argc, argv, “a:”)) != -1){ switch(command){ case ‘a’: (…) optind–; for( ;optind < argc && *argv[optind] != ‘-‘; optind++){ DoSomething( argv[optind] ); } break; } I found that int optind (extern used by getopt() ) points … Read more