I am developing a program that prompt the input from the command line argument. For example, this is the lists of command line arguments that I want to include:
-fish
-cat
-dog
-animal
All command line arguments can be in any order, BUT -animal must be in the last command line argument otherwise error message displayed
Let's say this input at the command line (using cc compiler)
a.out -fish -dog -animal
That kind of input is valid, but for:
a.out -fish
a.out -fish -animal -dog
Those kinds of input are not valid because -animal is not at the last command line or it is missing
Can someone help me by giving the code please?
6 5577
You'd want something like this: -
int i;
-
-
// Parse all the command line arguments except the very last one
-
for (i = 1; i < argc - 1; i++) {
-
if (!strcmp(argv[i], "-fish")) {
-
// do whatever
-
} else if (!strcmp(argv[i], "-dog")) {
-
// do whatever
-
} else if (!strcmp(argv[i], "-cat")) {
-
// do whatever
-
} else {
-
// bad option create error
-
}
-
}
-
-
// Parse the final command line argument to make sure it is "-animal"
-
if (strcmp(argv[argc - 1], "-animal") {
-
// generate error because final option isn't "-animal"
-
}
-
Keep in mind that "strcmp" returns 0 if the two strings match. Hence the reason I've used the ! operator in the if statements where you want to check for a match. Also, note that argv[0] is the name of the executable. So, we start checking arguments at argv[1].
You'd want something like this: -
int i;
-
-
// Parse all the command line arguments except the very last one
-
for (i = 1; i < argc - 1; i++) {
-
if (!strcmp(argv[i], "-fish")) {
-
// do whatever
-
} else if (!strcmp(argv[i], "-dog")) {
-
// do whatever
-
} else if (!strcmp(argv[i], "-cat")) {
-
// do whatever
-
} else {
-
// bad option create error
-
}
-
}
-
-
// Parse the final command line argument to make sure it is "-animal"
-
if (strcmp(argv[argc - 1], "-animal") {
-
// generate error because final option isn't "-animal"
-
}
-
Keep in mind that "strcmp" returns 0 if the two strings match. Hence the reason I've used the ! operator in the if statements where you want to check for a match. Also, note that argv[0] is the name of the executable. So, we start checking arguments at argv[1].
so is it possible if I change the comparison in the FOR loop like this:
-
-
for (i = 1;i < argc - 1;i++)
-
{
-
if (!strcmp(argv[argc-1],"-fish") == 0)
-
{
-
// do ....
-
}
-
if (!strcmp(argv[argc-1],"-dog") == 0)
-
{
-
// do ....
-
}
-
}
-
if (strcmp(argv[argc-1],"-animal") == 0)
-
{
-
// do...
-
}
-
else
-
{
-
//do bla bla bla
-
}
-
No. In each iteration of the loop you are always comparing the string at argv[argc - 1]. You need to change the 1 to "i" for this to actually iterate through the arguments list starting with the second to last element.
You do not need the else if statements. You can just use continue: - /* iterate over all arguments */
-
for (i = 1; i < argc - 1; i++) {
-
if (strcmp("-arg0", argv[i]) == 0) {
-
// do something. maybe check what argv[++i] contains
-
continue;
-
}
-
if (strcmp("-arg1", argv[i]) == 0) {
-
// do something. if you want to exit if arg1 is not the last argument, then:
-
if (i != (argc -1)) {
-
/* some function that exits the program and prints the usage */
-
return usageHelp();
-
}
-
continue;
-
}
-
if (strcmp("-arg2", argv[i]) == 0) {
-
// do something
-
continue;
-
}
-
/* some function that exits the program and prints the usage */
-
return usageHelp();
-
}
I forgot to say something before. In your case, when -animal is found, you just need to check if this is the last argument. You can do it like this: -
/* iterate over all arguments */
-
for (i = 1; i < argc - 1; i++) {
-
if (strcmp("-animal", argv[i]) == 0) {
-
// do something. maybe check what argv[++i] contains
-
if (i != (argc - 1)) {
-
// -animal is not the last argument. exit program with usage help
-
return usageHelp();
-
}
-
continue;
-
}
-
if (strcmp("-arg1", argv[i]) == 0) {
-
// do something
-
continue;
-
}
-
if (strcmp("-arg2", argv[i]) == 0) {
-
// do something
-
continue;
-
}
-
/* some function that exits the program and prints the usage */
-
return usageHelp();
-
}
You do not need the else if statements. You can just use continue: - /* iterate over all arguments */
-
for (i = 1; i < argc; i++) {
-
if (strcmp("-arg0", argv[i]) == 0) {
-
// do something. maybe check what argv[++i] contains
-
continue;
-
}
-
if (strcmp("-arg1", argv[i]) == 0) {
-
// do something
-
continue;
-
}
-
if (strcmp("-arg2", argv[i]) == 0) {
-
// do something
-
continue;
-
}
-
/* some function that exits the program and prints the usage */
-
return usageHelp();
-
}
anyone know how to expand on this code so that the last two args are filenames to be opened? bearing in mind that the switch args are optional, and the first filename is also optional, but the last one is compulsory.
so far i can check the optional args, and i can open one file cos it uses the last arg.... now i need it to check the second-last arg to see if its a filename (so no hyphen) and if it is, open it as well.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Hari |
last post by:
can i have command line arguments in VS.NET applicatio?
if yes how?
Can i have some code snippets of the above functionality?
I know we can acjieve this in console application form
command...
|
by: Jon Hewer |
last post by:
hi
i am writing a little script and currently implementing command line
arguments following the guide by mark pilgrim from dive into python;
...
|
by: Steve M |
last post by:
I'm trying to invoke a Java command-line program from my Python program
on Windows XP. I cannot get the paths in one of the arguments to work
right.
The instructions for the program describe the...
|
by: SunRise |
last post by:
Hi
I am creating a C Program , to extract
only-Printable-characters from a file ( any type of file) and display
them.
OS: Windows-XP
Ple help me to fix the Errors & Warnings and explain...
|
by: amirmira |
last post by:
I would like to set command line arguments to a service at install time. I
need to do this because I need to get information from different registry
locations depending on my command line argument....
|
by: Rune Jacobsen |
last post by:
Hi,
I've been trying to figure this one out, but my experience just doesn't
have what it takes... :|
I am writing an application that reads an XML file and displays the
contents in various...
|
by: Roland |
last post by:
Hi,
I am developing a C++ project and want to pass some command line
arguments in VS .NET 2003. I am in debug mode, the configuration is set
to Debug and I entered my argument list in Project ->...
|
by: raphfrk |
last post by:
I have a program which reads in 3 filenames from the command line
prog filename1 filename2 filename3
However, it doesn't work when one of the filenames has spaces in it
(due to a directory...
|
by: Milan |
last post by:
Hi,
Please guide me how to set command line argument and
how to retrive command line argument.
Senario: vb.net application should be able to execute from command
prompt by passing login and...
|
by: Jwe |
last post by:
Hi,
I've written a program which has both a command line interface
and Windows form interface, however it isn't quite working
correctly.
When run from command line with no arguments it should...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |