473,473 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

command line arguments

25 New Member
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 5589
tyreld
144 New Member
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 New Member
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 New Member
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
jmftrindade
2 New Member
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
jmftrindade
2 New Member
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 New Member
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: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.