Dealing with non-option arguments in getopt before options are checked - c

I am writing a program and I want to get the non-option arguments before checking the flags.
For example, if the arguments are ./a.out -a -b 50 filename
I want to use filename and then do task_a() and/or task_b()depending on the flags. filename can be any argument in argv[]
int opt;
float value;
while ((opt = getopt(argc, argv, "ab:")) != -1)
case 'a':
task_a(filename);
break;
case 'b':
value = atof(optarg);
task_b(filename, value);
break;
So basically I need to extract filename before using it in my tasks. How can I implement this?

getopt will pass you the command line arguments in the order that it finds them on the command line. This is generally NOT the order that you want to process them.
Instead, have your program set its options first before performing the operations - so something like ( given #include <stdbool.h> ) :
// Set the defaults
bool doTaskA = false;
book doTaskB = false;
float brightness = -1;
int opt;
float value;
while ((opt = getopt(argc, argv, "ab:")) != -1) {
case 'a':
doTaskA = true;
break;
case 'b':
doTaskB = true;
brightness = atof(optarg);
break;
}
filename = ....,,
if (doTaskA) {
task_a(filename);
}
if (doTaskB) {
task_b(filename, brightness);
}
I chose the name “brightness” as a meaningful attribute name - naturally you should choose your own. As good practice, do NOT name your variables “optionA” or some such - variable names should describe what they ARE, not the specific implementation details.
Always imagine that your options can change letters over time (what is now option “a” might become option “R” in six months time) - such changes should not affect anything except the “getopt” arguments and “case” statements.
EDITED TO CHANGE :
I originally wrote this with “if (brightness >= 0.0)”, but decided that in general a separate doTaskB variable is cleaner, more explicit approach that does not limit the possible values of the argument.

Related

Can't read arguments with getopt_long

I have the code below.
But when I run it with --debug=2, the debug variable gets value 100. I 'd expect 2...
Where is my mistake?
Here the code:
int debug=0;
int opt;
struct option longopts[] = {
{ "debug", required_argument, &debug, 'd' }
};
while ((opt = getopt_long(argc, argv, "d", longopts, NULL))!= -1)
{
switch (opt)
{
case 'd':
switch (debug)
{
case 1:
logPrio = LOG_INFO;
printf("1");
break;
case 2:
printf("2");
logPrio = LOG_CRIT;
break;
}
}
}
printf ("--%d--", debug);
Specifying &debug in longopts doesn't store the integer value of the option to the specified address, getopt_long expects you to extract integer values yourself.
According to the manual, the int *flag member of struct option does something completely different:
flag "specifies how results are returned for a long option. [If
non-NULL], getopt_long() returns 0, and flag points to a variable
which is set to val if the option is found, but left unchanged if
the option is not found.
You specify &debug for flag and 'd' for val, so debug gets set to 'd' (the number 100) when --debug is specified. Since you're already storing the result of getopt_long into the opt variable, you don't need to store &debug in longopts at all. Instead, use the optarg variable to get the argument to --debug:
case 'd':
debug = atoi(optarg);
switch (debug) {
...

Processing C command line arguments

How do I process multiple command line arguments like say in a package manager? I am giving a shot at writing a package manager and right now accepting the packages to install is giving me quite the headache. For example the user wants to install package x,y and z. Right now my code will send 3 different requests to a function. I would like to get all the package names in one go. So for example, the user wants to install package x,y and z, it would be processed and my code will send a request to a function stating that it needs x,y and z and the function will begin work immediately.
Here's my current implementation...
case 'S':
switch (argv[1][2]) {
case 'u':
id = 1;
alfred("system", "update", "", id);
break;
case 'r':
id = 1;
alfred("system", "reload", "", id);
break;
case 'i':
if (argc - 2 != 0) {
// Loop until packages are complete.
packages = 2; // Starting point of packages = argv[2][0]
srand(time(NULL)); // Seed for random number
id = rand(); // Generate random number for id
argc = argc - 2 + 1; // argc minus the number of packages and plus 1
/* This is a very inefficent loop! */
/* Must get all targets and feed it to alfred */
while (packages <= argc) {
alfred("system", "install", &argv[packages][0], id);
packages++;
}
} else {
printf("Unrecognized format. Execute alfred -h for more information.\n");
}
break;
default:
printf("Unrecognized format. Execute alfred -h for more information.\n");
break;
}
break;
You can also try libargtable.
I used it before to parse various arguments, it's very handy.
Have you try the getopt library?
http://www.gnu.org/software/libc/manual/html_node/Getopt.html
I think what you are looking for is getopt
the documentation says
The getopt() function parses the command-line arguments. Its arguments argc and argv are the argument count and
array as passed to the main() function on program invocation. An element of argv that starts with '-' (and is
not exactly "-" or "--") is an option element. The characters of this element (aside from the initial '-') are
option characters. If getopt() is called repeatedly, it returns successively each of the option characters from
each of the option elements.

Constant integral expression required error

I am writing code for AVR ATmega32-A microcontroller. I am using switch case as shown below.
unsigned char Command;
unsigned int Param;
void runCom(void){
switch(Command){
case(NO_COM):
Command = 0;
break;
case(INF):
printf("\r\n\r\n");
printf("university\r\n");
printf("sweden\r\n");
printf("Ver. 1.0A\r\n");
Command = 0;
break;
case (DB):
Command = 0;
break;
case(CLEARM):
Command = 0;
break;
default:
Command = 0;
break;
}
}
the above code is working but now i want to add one more case in that switch like as shown below.
unsigned char Command, Command1;
unsigned int Param;
void runCom(void){
switch(Command){
case(NO_COM):
Command = 0;
break;
case(INF):
printf("\r\n\r\n");
printf("university\r\n");
printf("sweden\r\n");
printf("Ver. 1.0A\r\n");
Command = 0;
break;
case (DB):
Command = 0;
break;
case(ADC):
printf("ADC Value",ReadAd());
printf("Enter Amplification stage");
switch(Command1){
case(stage1):
PORTC=0x00;
DDRC=0xC0;
printf("ADC Value",ReadAd());
Command1 = 0;
break;
case(stage2):
PORTC=0x00;
DDRC=0x03;
printf("ADC Value",ReadAd());
Command1 = 0;
break;
}
Command = 0;
break;
case(MEM):
Command = 0;
break;
case(CLEARM):
Command = 0;
break;
default:
Command = 0;
break;
}
}
I am getting error like
undefind symbol 'ADC'
undefind symbol 'stage1'
undefind symbol 'stage2'
then i have declared like this
unsigned char Command, ADC, Command1, stage1, stage2;
unsigned int Param;
now i am getting error like
constant integral expression required at line case(ADC)
constant integral expression required at line case(stage1)
constant integral expression required at line case(stage2)
so please can any one suggest me how to overcome this error. I haven't declared WGP, WGF, INF any where but I am not getting "undefined symbol of WGF, WGP, INF. why this error occurring for ADC only. i have fallowed same switch case rules as i used before. any suggestions appreciated.
Thanks in advance.
The values in a case needs to be constants or literals.
If you look up e.g. WGP you will see that it's most likely a #define'd literal. Do the same for your new values.
The values you use in the cases of a switch statement must be compile-time constants. That is, they must have fixed values that are known to to the compiler at compile time.
There are four main ways to do this.
You can use a constant literal (a number).
case 42:
Note that using unnamed values ("magic numbers") isn't recommended because it doesn't document the code in any way, and makes the code fragile (what if you need to change the value to 43 in five different places and forget one?)
You can use a preprocessor constant.
#define COMMAND_CODE 42
...
case COMMAND_CODE:
That's better because if you need to changet the command code value to 43 you only have to change the #define (which should only occur once, in a header file).
You can use an enum.
typedef enum { COMMAND_ON=42, COMMAND_OFF=2, COMMAND RESET=77 } command_codes:
...
case COMMAND_ON:
In C++, if your compiler is reasonably modern, you can use a constant:
const int magic_number = 42;
case magic_number:
(Note that the switch value shown in a case statement does NOT need to have parentheses around it.)
From the code you have shown, it seems that you have defined Command, ADC, etc., as variables (rather than as constants). In this case the compiler can't know what value those variables will have at execution time when it compiles the code. The language specifies that case values must be known at compile time so that the compiler can generate fast code (faster then a whole series of if ... else if ... else if statements).

What does "passing argument 2 of strcmp makes pointer from integer without a cast" error in C mean?

I am compiling my C code and am getting two errors:
warning:passing argument 2 of strcmp makes pointer from integer without a cast
and
warning: note: expected const char * but argument is of type int
This my main:
int main(int argc, char *argv[])
{
//check to make sure that the command line arguments are valid
if(argc!=3)
{
printf("invalid function call try again\n");
}
//else to choose the proper command
else
{
//reverse routine A
if(strcmp(argv[2],'a'||'A')==0) //line 138
{
reva(argv[1]);
}
//reverse routine B
else if(strcmp(argv[2],'b'||'B')==0) //line 143
{
revb(argv[1]);
}
//reverse routine C
else if(strcmp(argv[2],'c'||'C')==0) //line 148
{
revc(argv[1]);
}
//unacceptable command line argumant
else
{
printf("unacceptable command line argument for reverse routine try again:\n");
}
}
}
It means what it says. 'a'||'A' is an integer – specifically, it is the integer 1. The second argument of strcmp must be a string, not an integer.
You appear to intend to compare argv[2] to a and A. You need two different strcmp calls for that. Also, you need to use double, not single, quotes.
In "C", the '||' operator is a boolean 'or' operation, not a concatenation operation. Also, the use of apostrophes denotes a single character which basically 'char' type.
I think you want something like this (for line 148):
if (strcmp(argv[2], "C")==0 || (strcmp(argv[2], "c")==0) ...
or, if your C library supports it:
if (strcasecmp(argv[2], "C") == 0)
which is a case insensitive comparison.
I believe that your object here is to compare the command line argument (argv[2]) with the character (string) "C" or "c", ie you just if the user gave c or C at the command line.
The SO users have already offered an explanation. You need to use
(strcmp(argv[2], "C")==0 || (strcmp(argv[2], "c")==0)
in order to eliminate your warning.
However, this is not the optimal way to parse command line arguments in C. If your program is too complicated when parsing user input I would suggest to use the library "getopt". It is designed to help the user parse and analyze the input in a structured manner.
Here is a small code snipper
opt = getopt_long( argc, argv, optString, longOpts, &longIndex );
while( opt != -1 ) {
switch( opt ) {
case 'I':
globalArgs.noIndex = 1; /* true */
break;
case 'l':
globalArgs.langCode = optarg;
break;
case 'o':
globalArgs.outFileName = optarg;
break;
case 'v':
globalArgs.verbosity++;
break;
case 'h': /* fall-through is intentional */
case '?':
display_usage();
break;
case 0: /* long option without a short arg */
if( strcmp( "randomize", longOpts[longIndex].name ) == 0 ) {
globalArgs.randomized = 1;
}
break;
default:
/* You won't actually get here. */
break;
}
opt = getopt_long( argc, argv, optString, longOpts, amp;longIndex );
}
Please search some documents (or the linux man pages) for getopt and getopt_long. Here is an example from GNU.

getopt does not parse optional arguments to parameters

In C, getopt_long does not parse the optional arguments to command line parameters parameters.
When I run the program, the optional argument is not recognized like the example run below.
$ ./respond --praise John
Kudos to John
$ ./respond --blame John
You suck !
$ ./respond --blame
You suck !
Here is the test code.
#include <stdio.h>
#include <getopt.h>
int main(int argc, char ** argv )
{
int getopt_ret, option_index;
static struct option long_options[] = {
{"praise", required_argument, 0, 'p'},
{"blame", optional_argument, 0, 'b'},
{0, 0, 0, 0} };
while (1) {
getopt_ret = getopt_long( argc, argv, "p:b::",
long_options, &option_index);
if (getopt_ret == -1) break;
switch(getopt_ret)
{
case 0: break;
case 'p':
printf("Kudos to %s\n", optarg); break;
case 'b':
printf("You suck ");
if (optarg)
printf (", %s!\n", optarg);
else
printf ("!\n", optarg);
break;
case '?':
printf("Unknown option\n"); break;
}
}
return 0;
}
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 --blame John
You suck !
$ ./respond --blame=John
You suck , John!
The man page certainly doesn't document it very well, but the source code helps a little.
Briefly: you're supposed to do something like the following (though this may be a little over-pedantic):
if( !optarg
&& optind < argc // make sure optind is valid
&& NULL != argv[optind] // make sure it's not a null string
&& '\0' != argv[optind][0] // ... or an empty string
&& '-' != argv[optind][0] // ... or another option
) {
// update optind so the next getopt_long invocation skips argv[optind]
my_optarg = argv[optind++];
}
/* ... */
From among the comments preceding _getopt_internal:
...
If getopt finds another option character, it returns that character,
updating optind and nextchar so that the next call to getopt can
resume the scan with the following option character or ARGV-element.
If there are no more option characters, getopt returns -1.
Then optind is the index in ARGV of the first ARGV-element
that is not an option. (The ARGV-elements have been permuted
so that those that are not options now come last.) <-- a note from me:
if the 3rd argument to getopt_long starts with a dash, argv will not
be permuted
...
If a char in OPTSTRING is followed by a colon, that means it wants an arg,
so the following text in the same ARGV-element, or the text of the following
ARGV-element, is returned in optarg. Two colons mean an option that
wants an optional arg; if there is text in the current ARGV-element,
it is returned in optarg, otherwise optarg is set to zero.
...
... though you have to do some reading between the lines. The following does what you want:
#include <stdio.h>
#include <getopt.h>
int main(int argc, char* argv[] ) {
int getopt_ret;
int option_index;
static struct option long_options[] = {
{"praise", required_argument, 0, 'p'}
, {"blame", optional_argument, 0, 'b'}
, {0, 0, 0, 0}
};
while( -1 != ( getopt_ret = getopt_long( argc
, argv
, "p:b::"
, long_options
, &option_index) ) ) {
const char *tmp_optarg = optarg;
switch( getopt_ret ) {
case 0: break;
case 1:
// handle non-option arguments here if you put a `-`
// at the beginning of getopt_long's 3rd argument
break;
case 'p':
printf("Kudos to %s\n", optarg); break;
case 'b':
if( !optarg
&& NULL != argv[optind]
&& '-' != argv[optind][0] ) {
// This is what makes it work; if `optarg` isn't set
// and argv[optind] doesn't look like another option,
// then assume it's our parameter and overtly modify optind
// to compensate.
//
// I'm not terribly fond of how this is done in the getopt
// API, but if you look at the man page it documents the
// existence of `optarg`, `optind`, etc, and they're
// not marked const -- implying they expect and intend you
// to modify them if needed.
tmp_optarg = argv[optind++];
}
printf( "You suck" );
if (tmp_optarg) {
printf (", %s!\n", tmp_optarg);
} else {
printf ("!\n");
}
break;
case '?':
printf("Unknown option\n");
break;
default:
printf( "Unknown: getopt_ret == %d\n", getopt_ret );
break;
}
}
return 0;
}
I recently came across this issue myself. I arrived at a similar solution to the one Brian Vandenberg and Haystack suggested. But to improve readability and avoid code duplication, you can wrap it all up in a macro like below:
#define OPTIONAL_ARGUMENT_IS_PRESENT \
((optarg == NULL && optind < argc && argv[optind][0] != '-') \
? (bool) (optarg = argv[optind++]) \
: (optarg != NULL))
The macro can be used like this:
case 'o': // option with optional argument
if (OPTIONAL_ARGUMENT_IS_PRESENT)
{
// Handle is present
}
else
{
// Handle is not present
}
break;
If you are interested, you can read more about how this solution works in a blog post I wrote:
https://cfengine.com/blog/2021/optional-arguments-with-getopt-long/
This solution is tested and is – at the time of this writing – currently used in CFEngine.
I also ran into the same problem and came here. Then I realised this .
You don't have much of a use case of "optional_argument" . If an option is required you check from program logic, if an option is optional then you need not do anything because at getopt level all options are optional , they are not mandatory, so there is no use case of "optional_argument". Hope this helps.
ps: for the above example i think the correct options are
--praise --praise-name "name" --blame --blame-name "name"

Resources