I have a program where you enter an option
-d
and then whether or not you supply a non-optional argument after the option, do something.
Heres my code:
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#define OPT_LIST "d::"
int main (int argc, char *argv[])
{
int c;
char string[] = "blah";
while ((c = getopt (argc, argv, OPT_LIST)) != -1)
{
switch (c)
{
case 'd':
printf("%s\n", optarg);
break;
case '?':
fprintf(stderr, "invalid option\n");
exit(EXIT_FAILURE);
}
}
}
So if you enter a non-optional argument after the option, it prints the argument. But I want it to print out the char "string" if the user doesn't supply a non-optional argument (this is why I put the double colon in the
OPT_LIST). But I'm not sure how to do this so any help would be greatly appreciated.
Heres what happens when I run the program:
user:desktop shaun$ ./arg -d hello
hello
user:desktop shaun$ ./arg -d
./arg: option requires an argument -- d
invalid option
I'm running a Mac with OS X using C language.
The "optional value of an option" feature is only a GNU libc extension, not required by POSIX, and is probably simply unimplemented by the libc shipped with Mac OS X.
The options argument is a string that specifies the option characters that are valid for this program. An option character in this string can be followed by a colon (‘:’) to indicate that it takes a required argument. If an option character is followed by two colons (‘::’), its argument is optional; this is a GNU extension.
https://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html
In fact, POSIX.1-2008, section 12.2, "Utility Syntax Guidelines", explicitly forbids this feature:
Guideline 7: Option-arguments should not be optional.
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02
According to the getopt documentation, it will return : if an option with an argument does not have one. It also sets optopt with the matching argument.
Therefore, use:
int main (int argc, char *argv[])
{
int c;
while ((c = getopt (argc, argv, ":d:f:")) != -1)
{
switch (c)
{
case 'd':
case 'f':
printf("option -%c with argument '%s'\n", c, optarg);
break;
case ':':
switch (optopt)
{
case 'd':
printf("option -%c with default argument value\n", optopt);
break;
default:
fprintf(stderr, "option -%c is missing a required argument\n", optopt);
return EXIT_FAILURE;
}
break;
case '?':
fprintf(stderr, "invalid option: -%c\n", optopt);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
Maybe I'm misunderstanding the question. I'm reading it as though the user wants to over ride getopt's default error handling. If that is the case, shouldn't there be a : in the beginning of their OPT_LIST? I think the code above is good, however, I think it will still print
./arg: option requires an argument -- d
To surpress that, don't we need a : at the beginning of the OPT_LIST? For example, changing this:
while ((c = getopt (argc, argv, "d:f:")) != -1)
to this:
while ((c = getopt (argc, argv, ":d:f:")) != -1)
Correct me if I'm wrong.
Try this solution. It works for me. Consider option 'z' as option with optional argument.
int c;
opterr = 0; //if (opterr != 0) (which it is by default), getopt() prints its own error messages for invalid options and for missing option arguments.
while ((c = getopt (argc, argv, "x:y:z:")) != -1)
switch (c)
{
case 'x':
case 'y':
case 'z':
printf("OK ... option -%c with argument '%s'\n", c, optarg);
break;
case '?':
if (optopt == 'x' || optopt == 'y')
fprintf (stderr, "ERR ... Option -%c requires an argument.\n", optopt);
else if(optopt == 'z' && isprint(optopt))
{
printf("OK ... option '-z' without argument \n");
break;
}
else if (isprint (optopt))
fprintf (stderr, "ERR ... Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "ERR ... Unknown option character `\\x%x'.\n", optopt);
return -1;
default: ;
}
Here are some examples:
./prog -x
ERR ... Option -x requires an argument.
./prog -y
ERR ... Option -x requires an argument.
./prog -z
OK ... option '-z' without argument
./prog -x aaa
OK ... option -x with argument 'aaa'
./prog -y bbb -x aaa
OK ... option -y with argument 'bbb'
OK ... option -x with argument 'aaa'
./prog -x aaa -y bbb -z
OK ... option -x with argument 'aaa'
OK ... option -y with argument 'bbb'
OK ... option '-z' without argument
./prog -x aaa -y bbb -z ccc
OK ... option -x with argument 'aaa'
OK ... option -y with argument 'bbb'
OK ... option -z with argument 'ccc'
Related
I noticed during debugging that the optarg variable of GNU's getopt remained zero (i.e. address 0x0) during debugging. But when optarg is used as right-hand operand of assignment or passed as argument to a parameter, the expected value is applied. The example from GNU:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "abc:")) != -1)
switch (c)
{
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c': # b 26
cvalue = optarg; # gdb stops right before executing this line
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
printf ("aflag = %d, bflag = %d, cvalue = %s\n",
aflag, bflag, cvalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
Usage
$ gcc -o getopt getopt.c -g # debugging ON, optimizations OFF
$ gdb getopt
(gdb) b 26 # set breakpoint
(gdb) r -cfoo
(gdb) p optarg
$1 = 0x0 # no "foo"?
(gdb) n # executes cvalue = optarg
(gdb) p cvalue
$2 = 0x7fffffffea99 "foo" # here it is, please don't expect to get the same memory-address
Why optarg doesn't contain the character string "foo" at line 26? What do I miss here?
More bewildering, I figured out it doesn't contain "foo" even after the line has been executed. I've put a glance at the original source and didn't notice something like a compiler optimization. I'm using GCC 11.1.0 on Archlinux, x86_64.
Thank you
PaulMcKenzie seems right. It is probably this old bug 8588 within GDB. GDB struggles handling the GOT (Global-Offset-Table) right which is a part of PIC (Position-Independent-Code].
Workaround
p (char*)'optarg#GLIBC_2.2.5'
We reference here the variable optarg and the version symbol of the GLIBC. The version 2.2.5 is the baseline which already includes provides GNU getopt and therefore the newer ones don't apply.
Thank you
I am having trouble with getopt. In my program, I wish to run the program with the following options: -l -w -n -b. n and b are optional, however the program must have either l or w, not both. Also, if w is invoked, it must be followed by an argument. Also, the order should be l or w first, followed by n or b, whose order does not matter.
Here are examples of valid arguments:
./prog -l -n -b
./prog -w argument -b -n
./prog -l -n
./prog -l -b
These examples are not valid:
./prog -l -w argument
./prog -n -l
./prog -b -l
Based on these requirements, I having having trouble using getopt. Of course I can use a myriad of if else statements, but that would be ugly and unnecessarily complicated.
Specifically, I am having trouble with the order of the arguments and making -l and -w options an either or relationship.
Here is what I have so far, although it is not much because of these uncertainties.
while ((option = getopt(argc, argv,"lw:nb:")) != -1) {
switch(option) {
case'l': ... // I haven't wrote anything here yet
case'w': ...
case'n': ...
case'b': ...
case '?': ...
default: ...
}
}
First, getopt makes sure that w has an argument. That's the point of the
: in your getopt string. If -b does not need an argument, then you should remove the : after it.
Second, I don't think verifying argument conditions is as bad as you're thinking
it's going to be. You should already have variables to keep track of your
options, like this:
int opt_l = 0;
int opt_w = 0;
char *arg_w = 0;
int opt_n = 0;
int opt_b = 0;
You should also have a function that prints out the usage and a helpful error
message to the user, like this:
int exit_usage(const char *err)
{
if( err ) { printf("Bad arguments: %s\n", err); }
printf("Usage: ...\n");
exit(1);
}
Then it's a simple one-line check to verify your preconditions. If a
precondition fails, then print an error and show the usage info to your user.
You don't have to cram the precondition check into one line, but it tends to
be easier to read a list of preconditions when they're all one line each.
Like this:
while ((option = getopt(argc, argv, "lw:nb")) != -1)
{
switch(option)
{
case 'l':
if( opt_w != 0 ) { exit_usage("Cannot combine -l with -w"); }
++opt_l;
break;
case 'w':
if( opt_l != 0 ) { exit_usage("Cannot combine -l with -w"); }
arg_w = optarg;
++opt_w;
break;
case 'n':
if( opt_l == 0 && opt_w == 0 ) { exit_usage("-n requires -l or -w first"); }
++opt_n;
break;
case 'b':
if( opt_l == 0 && opt_w == 0 ) { exit_usage("-n requires -l or -w first"); }
++opt_b;
break;
case '?':
exit_usage(NULL);
break;
default:
break;
}
}
I am working on trying to take in command line arguments. If I want to have multiple optional command line arguments how would I go about doing that? For example you can run the program in the following ways:
(a is required every instance but -b -c -d can be given optionally and in any order)
./myprogram -a
./myprogram -a -c -d
./myprogram -a -d -b
I know that getopt()'s third argument is options. I can set these options to be "abc" but will the way I have my switch case set up causes the loop to break at each option.
The order doesn't matter so far as getopt() is concerned. All that matters is your third argument to getopt() (ie: it's format string) is correct:
The follow format strings are all equivalent:
"c:ba"
"c:ab"
"ac:b"
"abc:"
In your particular case, the format string just needs to be something like "abcd", and the switch() statement is properly populated.
The following minimal example¹ will help.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "abc:")) != -1)
{
switch (c)
{
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
}
printf ("aflag = %d, bflag = %d, cvalue = %s\n",
aflag, bflag, cvalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
¹Example taken from the GNU manual
I wrote (copied and pasted from Google and simplified) a C program to use getopt to print out the values of the arguments passed in from the Unix command line.
From Unix command line:
./myprog -a 0 -b 1 -c 2
My C code is:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int i;
while ((i = getopt(argc, argv, "abc")) != -1) {
switch (i) {
case 'a':
printf("A = %s\n", optarg);
break;
case 'b':
printf("B = %s\n", optarg);
break;
case 'c':
printf("C = %s\n", optarg);
break;
default:
break;
}
}
return 0;
}
I want to program to print out each of the values passed e.g.
A = 0
B = 1
C = 2
However it is not printing out anything at all.
You forget about ":" after any option with argument. If you will change one line
while ((i = getopt(argc, argv, "a:b:c:")) != -1) {
you will get working variant.
Read properly man 3 getopt, it said about third argument of getopt that
… optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, so
getopt() places a pointer to the following text in the same argv-element, or the text of the following argv-element, in optarg. Two colons mean an option
takes an optional arg; if there is text in the current argv-element (i.e., in the same word as the option name itself, for example, "-oarg"), then it is
returned in optarg, otherwise optarg is set to zero. …
Hello I'm new with C and I'm trying to start this lab where the command syntax is to first list any command line flags then list one or more file names.
I'm having trouble organizing how I want to scan the input arguments and differentiating them between flags and file names.
I thought of doing a loop to see if the argument is a flag or file name. But I'm unsure how to begin implementing it. Since the first 4 arguments can be potential flags in any order then anything after is the file name. But it is also possible for no flags to be given and just start with filenames. I don't know at all how to initalize this loop to go through each argument 1 by 1. Can anyone help please?
Example of possible command line arguments:
wc fcopy.c head.c (no flags given just file names)
wc -l -c -w -L fcopy.c head.c a.exe (flags given and multiple files)
wc -l -w -c -L fcopy.c (flags given and 1 file name)
Reading command line args one by one is pretty simple:
int main( int argc, // Number of strings in array argv char *argv[])
The two basic args to a C function are argc (int number of args), and argv (an array of strings for the args)
The first string is always the name of the program that is running, after that is the args that were passed over the command line.
if(argc > 1)
for( count = 1 count < argc; count++ )
printf("%s", argv[count]);
Will display to the screen each of the arguments passed in past the name of the calling program.
I assume your flags are passed with a "-" as the first character? If so, you can check the contents of the first character in each string for a '-' to tell if its a flag or not.
You can use getopt()
Check these questions:
Parsing command-line arguments?
Handling command line options before and after an argument in C
Argument-parsing helpers for C/UNIX
Getopt shift optarg
Check out getopt, it allows you to parse command line arguments and their flags:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;
opterr = 0;
while ( (c = getopt (argc, argv, "abc:")) != -1 )
switch (c) {
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
printf ("aflag = %d, bflag = %d, cvalue = %s\n",
aflag, bflag, cvalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
Example of Parsing Arguments with getopt
Also see http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html