How to use getopt when option is a string on C? - c

I'm working/developing a C program for my university, that can have 3 options when invoked (-h for help, -o <argument> (with or without it) and last option can be a string like(test-in-1):
./myprogram test-in-1
I have to process these options on my main, and do what's required when they are invoked. None of them are mandatory.
I was thinking in using getopt to parse the options however one of these options is actually a string(char *) and is with this one that I'm kinda lost since getopt is not able to read strings, only char or a char with an argument(for example,-h, -o <argument>, as far as I understood). Any idea of how can I do this?
I can't really post any code besides main line,since I'm at stuck at start of it (int main(int argc, char *argv[])).
Thanks in advance for any advice/point in right direction.

You can pass a colon : which means it requires an argument and that argument will be set in optarg.
See also my answer here

If you're linking to the GNU C library, check out getopt_long.

Related

Executable path as a variable parameter to exec

I've an application which needs to call a specific program 'mips64-unknown-linux-gcc' for linking all objects from a script with all required args for linking.
I am writing an exec function to call the compiler passed by script along with it's args. For this I wrote the code:
//prog.c : gcc prog.c -o prog
int main(int argc, char *argv[]) {
execvp("mips64-unknown-linux-gcc",argv);
}
This works, but the mips64-unknown-linux-gcc and argv are variables from script input.
I need execv first argument to be a variable which is compiler to be invoked. I can somehow (maybe) retrieve it by getenv(”CC”) but due to other dependencies my requirement is that exec shall accept the compiler and args at runtime (something like below). Is there any way I can do this?
./prog mips64-unknown-linux-gcc --sysroot=<<...>> -O3 -Wl -L <<...>> -L <<...>> -I <<...>> -L <<...>> abcd.o a1.o b2.o -o prog
I described my problem at my best. Please ask if anything is not clear.
From your example command line it seems that you want to take the first argument from command line as your command to execute and everything else should be passed to that command.
That is basically the same command line execpt for the first argument.
This makes things rather easy.
Looking at argv you will find these string:
char *argv[] = {"proc","mips64-unkown-linux-gcc", "--sysroot=<<...>>", ..., "-o", "prog", NULL};`
You can use that and call your command:
execvp(argv[1], argv+1);
Of course you should check whether you have at least one argument.
If you want do filter some options and handle in your own program instead of blindly passing it to execvp you must rebuild your own array of arguments where you do not include those options.

Parsing double-character command using getopt() C

Is there a way to make getopt() or getopt_long() recognise double character option?
example: ./a.out -my argument where my is single command.
You can use getopt_long_only, which will try to process options as long ones even if there is only one - sign before them.
This function is GNU extension, as well as getopt_long.

Command line arguments without the hyphen

How can I parse arguments without the hyphen in C?
I.e. virsh install vm
or
git pull origin master
When I tried it out, if there is no - prefix, everything just gets ignored and argc returns 1 (argv[0] is the program call).
I'm using Linux, but it would be nice if there was a cross platform method to achieve this.
UPDATE: the problem was me using a # in front of the first argument, I was trying to pass in #XX eg number_program #12. Needless to say this doesn't work.
Are you using some library to parse the arguments for you? There is no special 'hyphen' arguments when passing in parameters to a C program specifically. Parse argv however you like.
For example:
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
for(i=0; i<argc; i++) {
//dont do this without proper input validation
printf("%s\n", argv[i]);
}
return 0;
}
Example run:
$ ./a.out test test test -hyphen
./a.out
test
test
test
-hyphen
argv contains the program name and the arguments to the program, in the order they were given in the command line.* Hyphens aren't special; they just make it easy for both people and computers to separate options from other args.
If you want to interpret args a certain way, that's your prerogative. That's what git does, basically interpreting argv[1] (if it exists, of course) as the name of a subcommand. And you don't need any libraries in order to do that. You just need to decide how you want the args interpreted.
* Modulo some cross-platform differences in how args are parsed; *nix typically does some pre-parsing for you and expands wildcard patterns, for example. You won't have 100% cross-platform compatibility unless you understand those differences and are ready for them.

command line arguments into make file

I have a C file for which I want to give cmd line arguments.
Say
$ make --argument1
or something like this.
So that in my main program I should be able to do argv[1] and be able to access the variable.
I have tried looking for ways of doing this. Is there actually a way of doing this?
These were the relevant content I found on the GNULinux manual about make.
variables defined on the command line are passed to the sub-make
through MAKEFLAGS. Words in the value of MAKEFLAGS that contain ‘=’,
make treats as variable definitions just as if they appeared on the
command line.
Is this what I need to read up more or is this in a different context?
Do let me know.
I think you misunderstand the use of command line arguments - they are given when the executable is executed not when it is compiled.
Better example
foo.c
#include <stdio.h>
int main()
{
int myval=DEFVAL;
printf("myval=%d\n", myval);
return 0;
}
Makefile
DEFVAL=17
foo: foo.c
gcc -DDEFVAL=${DEFVAL} foo.c -o $#
Your question doesn't make a whole lot of sense, so I'm going to read between the lines and guess that you have a Makefile that someone else wrote, and when you run make, it runs some program, and that's the program that you want to give command line arguments.
In order to do that, you'll probably have to modify the Makefile. In order to that, it helps to understand how make works and how to use it (you might want to find a book on the subject, such as this one), but it may possible to modify the Makefile without too much trouble.
Somewhere in the Makefile, you'll find the action line that is used to invoke you're program. If you can find that line, you can add the argument you want.

How to capture parameters after some string in commandline?

How to capture a parameter after some string in command line?
./executable.out -apps path_to_out
In the above code I want to store path_to_out in a string variable. What is the efficient way of doing that?
It kind of depends what kind of options you want. If you are willing to have only single-letter options, you can use the C library function getopt to parse the command line. If you would like long options (e.g., --apps), you can use getopt_long, but this is a GNU extension that will not port well. If you want really, really fancy option parsing, you can use something like GLib.
Of course, you can just roll your own, iterating over the arguments from 1 to argc and, when you encounter -apps, check that i+1 < argc and grab the next argument.
argv[argc-1] already has the value for you if your path is the last argument in the command line.
if your main is like :
int main(int argc,char **argv)
all the command line arguments are in char **argv.
in your case:
argv[2] is path_to_out.
if you want to copy it in a string,you can anyhow do this below thing:
string path(argv[2]);
It appears that by asking "after some string on the commandline" you are referring to the fact that the commandline argument -apps started with a dash, which makes it an "option".
The preferred way to doing this is to use a predefined library for processing options, such as getopt on GNU systems.
when you run parameter store in this way..
Example:---> ./executable.out -apps path_to_out
argv[0] will be "./executable.out"
argv[1] will be "-apps"
and argv[2] will be "path_to_out".

Resources