473,588 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ 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 5607
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
2598
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 prompt but how can i achieve it in an application in VS.NET? Thank you
6
628
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; http://diveintopython.org/scripts_and_streams/command_line_arguments.html thats all fine, however i am not sure of the BEST way to handle multiple command line arguments
7
4727
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 following for the command-line arguments: java -jar sforcedataloader.jar -Dsalesforce.config.dir=CONFIG_DIRECTORY They also give an example:
2
4181
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 how to use Command-Line Arguments inside C program.
1
7889
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. I have to do it this way as the consumer of the service should not be able to change the argument - except by uninstalling and reinstalling the service. I created the service and the service itself works great. However, when I try to install...
1
2444
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 ways to the end user. This works fine. My challenge lies in the fact that these XML files are generated by various (third party) applications. Which application generates them depends on the user, the country they are in, their personal preferences,...
4
1975
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 -> Project Properties -> Configuration Properties -> Debugging -> Command Arguments. The blurb provided in the properties window for this field reads "The command line arguments to pass to the application." which sounded promising.
40
2717
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 name with a space in it) because that filename gets split into 2. I tried
2
2746
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 password and should be able to execute process form (with his parameter) User should not be able to see GUI
7
9352
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 display the Windows form. The form is being displayed but the command only returns when the form is closed. I want the command line to return immediately, leaving the form displayed.
0
7929
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7862
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8357
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8223
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5729
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2372
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1196
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.