Command line parameters

So, I've been doing a little bit of research on this. I was just wondering, how exactly do you make these? Could I just get an example of the process of this?

EDIT:
Ok so after some more digging around, I found out how to pass them. Pretty simple actually. To anyone else with this question:
http://www.learncpp.com/cpp-tutorial/713-command-line-arguments/
This link provides the answers.

Now, my next question. How would this work from a user point of view? Let's say I make a program that takes no user input, just command line arguments, how does a user handle this? They can't just double click the cool little icon can they?
Last edited on
instead of using int main() use int main(int argc, char *argv[]).

argc contains the number of arguments and argv is an array of the arguments.

For example, if I wanted to make a MyCopy command I may want to have the user do this:
MyCopy origin destination


To acheive that I'd write something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main(int argc, char*argv[])
{
	if (argc != 2)
	{
		cout << "Error, wrong number of arguments entered" << endl;
		return EXIT_FAILURE;
	}

	string origin = argv[0];
	string destination = argv[1];

	//Do my copy code here.
	return EXIT_SUCCESS;
}
Last edited on
So, the user still has to provide some input? I don't see the benefit of this. Now it just sounds like instead of being prompted during the program for input, they just have to provide it from command prompt?
Isn't that was you were asking about? These are "Command line parameters".
A lot of programs take arguments this way. It is often much more convenient because we can tell a program what to do and when it has done this it can close the program immediately. I assume you use windows because otherwise it is strange if you have never seen this kind of program. Open up the command prompt. cd, dir, ipconfig, etc. all take arguments this way. Even the compiler you are using take arguments like this. If you use an IDE it is often hidden so you don't see it.
Yea I am on windows. Still haven't gotten around to dual booting Linux. So, could you give me an example of some real world programs that would use these? I guess I'm still not seeing when and where these would be practical.
A simple example:

Suppose you have written a program called calendar.exe which displays a simple calendar in a console window. The calendar year is passed as a command line argument. Now setup a command object to run your program.

1. Click Start, then All Programs, then Accessories
2. Right click Command Prompt, then Copy
3. Right click the desktop and then click Paste
4. Now right click this newly created shortcut and click Properties to bring up the properties settings dialog box
5. Click in the Target field under the Shortcut tab. Place the cursor at the end of the line (after cmd.exe)
6. Type a space, then /k ( a forward slash and lower case k)
7. Now type the full path to your program, say "C:\My Documents\calendar.exe" (If the path contains any spaces the double quotes are required.)
8. Type a space and the year 2012
9. Now click on the General tab at the top of the properties box and change the name from Command Prompt to Calendar for 2012 (or whatever other name you want)
10. Close the dialog box

If you now double click the icon, Windows launches cmd.exe in a console window with your calendar program being passed to cmd.exe and the year 2012 passed to your program. Every time you double click the icon you will get the calendar for 2012 with no need to interactively prompt the user for the year. When 2013 rolls around you only need to edit the properties settings to change the parameter to 2013.
Hmm, this still seems like more of a hassle than it should be. Is there no way for the program itself to get the current year?
Yes.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <ctime>

int main()
{
	time_t curr;
	tm local;
	time(&curr); // get current time_t value
	local=*(localtime(&curr)); // dereference and assign
	std::cout << local.tm_year + 1900 << std::endl;
	return 0;

}


See:
http://cplusplus.com/reference/clibrary/ctime/
http://cplusplus.com/reference/clibrary/ctime/tm/

I was only using this as an example. If you wanted to look at last year's calendar and this year's calendar you could set up two different objects, but in that case you would not want the program querying the system to get the current year.

There is no much difference between passing parameters as user input or as command line arguments. However, with command line arguments, you can exploit the features of your shell.
Like tab-completion, wildcard expansion * ? {a..g}, history, control structures (loops), etc.
Also you can easily tweek the default behaviour of the program.

By instance $ rename --just-print 'y/A-Z/a-z/' * will 'rename' the files acording to the regex (in this case convert to lowercase).
As a cautious user, you use the flag so it only shows the actions, but don't commit them.
And the wildcard expands to all the files in the directory.

How would this work from a user point of view?
_ RTFM
_ Make a front end.
_ RTFM
_ RTFM

Also, to parse the options http://linux.die.net/man/3/getopt
Last edited on
Okay here is another example.

Hit [Windows Key]+R and then type cmd and press enter.

Now type ipconfig and press enter. This executes the ipconfig command in windows. In this case we are passing in 0 arguments because we only call the exe's name.

Now the same program can also reset our ipsettings. (don't worry, it's safe if you want to try it).
Try typing ipconfig /renew. Here we are passing the string "/renew" into the program. ipconfig will recognize this as a command and perform the action. Tada!

Now... why is this useful? Let's say that I'm running some sort of test and need to reset my ip settings every 5 minutes for the next 8 hours. I can write a script file (.bat) which will do this automatically for me. In the .bat file I'll call ipconfig /renew every 5 minutes so I don't need to be physically present to deal with the program myself. It's wonderful!

Even your compiler can take command line arguments like this. Why not just start the program and press "build" yourself? Well maybe you want to make an automatic build each night to take a snapshot of a bunch of developers working together. You would set up a script to do this automatically using command line arguments.

Ta da! I hope that explains how useful these can be.
Hmm I think I'm starting to see where some usefulness if at with this.
Topic archived. No new replies allowed.