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?
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?
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?
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.
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.
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.
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.