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 5607
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 prompt but how can i achieve it in an application
in VS.NET?
Thank you
|
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
|
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:
|
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.
|
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...
| |
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,...
|
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.
|
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
|
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
|
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.
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |