473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

command line arguments

25
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?
Oct 7 '06 #1
6 5577
tyreld
144 100+
You'd want something like this:

Expand|Select|Wrap|Line Numbers
  1. int i;
  2.  
  3. // Parse all the command line arguments except the very last one
  4. for (i = 1; i < argc - 1; i++) {
  5.    if (!strcmp(argv[i], "-fish")) {
  6.       // do whatever
  7.    } else if (!strcmp(argv[i], "-dog")) {
  8.       // do whatever
  9.    } else if (!strcmp(argv[i], "-cat")) {
  10.       // do whatever
  11.    } else {
  12.       // bad option create error
  13.    }
  14. }
  15.  
  16. // Parse the final command line argument to make sure it is "-animal"
  17. if (strcmp(argv[argc - 1], "-animal") {
  18.    // generate error because final option isn't "-animal"
  19. }
  20.  
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].
Oct 7 '06 #2
evantri
25
You'd want something like this:

Expand|Select|Wrap|Line Numbers
  1. int i;
  2.  
  3. // Parse all the command line arguments except the very last one
  4. for (i = 1; i < argc - 1; i++) {
  5.    if (!strcmp(argv[i], "-fish")) {
  6.       // do whatever
  7.    } else if (!strcmp(argv[i], "-dog")) {
  8.       // do whatever
  9.    } else if (!strcmp(argv[i], "-cat")) {
  10.       // do whatever
  11.    } else {
  12.       // bad option create error
  13.    }
  14. }
  15.  
  16. // Parse the final command line argument to make sure it is "-animal"
  17. if (strcmp(argv[argc - 1], "-animal") {
  18.    // generate error because final option isn't "-animal"
  19. }
  20.  
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. for (i = 1;i < argc - 1;i++)
  3.         {
  4.          if (!strcmp(argv[argc-1],"-fish") == 0)
  5.                  {
  6.                   // do ....
  7.                  }
  8.         if (!strcmp(argv[argc-1],"-dog") == 0)
  9.                  {
  10.                   // do ....
  11.                  }
  12.          }
  13. if (strcmp(argv[argc-1],"-animal") == 0)
  14.            {
  15.             // do...
  16.             }
  17. else
  18.         {
  19.          //do bla bla bla
  20.          }
  21.  
Oct 8 '06 #3
tyreld
144 100+
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.
Oct 9 '06 #4
You do not need the else if statements. You can just use continue:

Expand|Select|Wrap|Line Numbers
  1.    /* iterate over all arguments */
  2.    for (i = 1; i < argc - 1; i++) {
  3.        if (strcmp("-arg0", argv[i]) == 0) {
  4.           // do something. maybe check what argv[++i] contains
  5.           continue;
  6.        }
  7.        if (strcmp("-arg1", argv[i]) == 0) {
  8.           // do something. if you want to exit if arg1 is not the last argument, then:
  9.           if (i != (argc -1)) {
  10.               /* some function that exits the program and prints the usage */
  11.               return usageHelp();
  12.           }
  13.           continue;
  14.        }
  15.        if (strcmp("-arg2", argv[i]) == 0) {
  16.           // do something
  17.           continue;
  18.        }
  19.        /* some function that exits the program and prints the usage */
  20.        return usageHelp();
  21.    }
Feb 23 '08 #5
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:

Expand|Select|Wrap|Line Numbers
  1. /* iterate over all arguments */
  2. for (i = 1; i < argc - 1; i++) {
  3. if (strcmp("-animal", argv[i]) == 0) {
  4. // do something. maybe check what argv[++i] contains
  5. if (i != (argc - 1)) {
  6. // -animal is not the last argument. exit program with usage help
  7. return usageHelp();
  8. }
  9. continue;
  10. }
  11. if (strcmp("-arg1", argv[i]) == 0) {
  12. // do something
  13. continue;
  14. }
  15. if (strcmp("-arg2", argv[i]) == 0) {
  16. // do something
  17. continue;
  18. }
  19. /* some function that exits the program and prints the usage */
  20. return usageHelp();
  21. }
You do not need the else if statements. You can just use continue:

Expand|Select|Wrap|Line Numbers
  1.    /* iterate over all arguments */
  2.    for (i = 1; i < argc; i++) {
  3.        if (strcmp("-arg0", argv[i]) == 0) {
  4.           // do something. maybe check what argv[++i] contains
  5.           continue;
  6.        }
  7.        if (strcmp("-arg1", argv[i]) == 0) {
  8.           // do something
  9.           continue;
  10.        }
  11.        if (strcmp("-arg2", argv[i]) == 0) {
  12.           // do something
  13.           continue;
  14.        }
  15.        /* some function that exits the program and prints the usage */
  16.        return usageHelp();
  17.    }
Feb 23 '08 #6
MeeM
1
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.
Mar 18 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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...
6
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; ...
7
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...
2
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...
1
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....
1
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...
4
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 ->...
40
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...
2
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...
7
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.