What is so special about running a c++ program through commnd line?

Click For Summary

Discussion Overview

The discussion revolves around the benefits and implications of using the command line interface in C++ programs, particularly focusing on the use of the main function signature main(int argc, char *argv[]) compared to the simpler int main(). Participants explore the flexibility, practicality, and potential applications of command line arguments in programming.

Discussion Character

  • Exploratory
  • Debate/contested
  • Technical explanation

Main Points Raised

  • Some participants argue that using main(int argc, char *argv[]) allows programs to accept command line arguments, which can enhance versatility and usability.
  • Others suggest that not using command line arguments may be seen as a lack of effort or foresight in programming practice.
  • It is noted that command line interfaces can be more flexible than graphical user interfaces (GUIs) for certain tasks, allowing for scripting and automation.
  • Some participants highlight specific use cases, such as invoking debug modes in applications or facilitating inter-task communication in multi-tasking environments.
  • There is a discussion about the appropriateness of using C++ for command line programs, with some questioning the relevance of GUI-oriented classes in this context.
  • One participant mentions the convenience of drag-and-drop functionality in Windows, where file names are passed as command line arguments.
  • Another viewpoint is expressed that C++ is not inherently biased towards GUI programming and can be effectively used for a variety of applications beyond GUIs.

Areas of Agreement / Disagreement

Participants express a range of opinions on the necessity and advantages of command line arguments, with no clear consensus reached. Some agree on the flexibility of command line interfaces, while others debate the relevance of C++ in command line programming.

Contextual Notes

Some points raised involve assumptions about user needs and programming practices, as well as the varying contexts in which command line arguments may or may not be beneficial.

dmatador
Messages
120
Reaction score
1
I.e., what is beneficial about main(int argc,char *argv[])?

I see a lot of seemingly good programmers write the main like this, but what does it do that int main() cannot do?
 
Technology news on Phys.org
One word: Unix.
 
dmatador said:
what does it do that int main() cannot do?

Quite simply, can't it NOT accept command line arguments, ever?

I guess it seems to me like NOT accepting command line arguments sort of the lazy way of approaching the problem. "99% of the time, my users won't want command line options, so I won't include them, because they're just THAT much harder to code." It might make sense when you're writing something that's more-or-less throw away code, or if you REALLY can't be bothered. But any program worth its salt will be versatile and accept command line options.

In the end, I would argue that it's just good practice. Get used to doing it, and you'll be writing better programs. Maybe only 1% better, but hey, you're more likely to be glad that you did it than glad you didn't.

DaveE
 
Do you know what argc and argv are? Once you do, their use becomes apparent. argc is the number of arguments and argv is an array of char* that are those arguments.
 
dmatador said:
I.e., what is beneficial about main(int argc,char *argv[])?

I see a lot of seemingly good programmers write the main like this, but what does it do that int main() cannot do?

Many programs use parameters on the command line to do useful things. If your program needs no other parameters as input, or has other ways of getting input (say using a default config file, through process objects like pipes, network and so on), then that's when you will probably just use int main() instead of the argc/argv vector method.
 
I've noticed some PC games use command line parameters to invoke a setup or debug mode.

Another possible use is for a multi-tasking application, because setting up inter task communication is awkward in windows (no shared memory). One method is for one task to pass information via command line parameters when invoking a second task, where the command line parameters are the first task's process handle value and some of that first task's handle values to be used by the second task for DuplicateHandle() to be able to access stuff from the first task (named handles can also be used).
 
TylerH said:
Do you know what argc and argv are? Once you do, their use becomes apparent. argc is the number of arguments and argv is an array of char* that are those arguments.

That part is obvious. I was trying to look for input from people who have strong and founded opinions about using command line.
 
dmatador said:
That part is obvious. I was trying to look for input from people who have strong and founded opinions about using command line.
Simply put, command line is more flexible than a GUI for a lot of tasks, and for a fraction of the development work. Let's say you're a system administrator. You have a GUI for it, and only a GUI. You often have to do a series of steps (whatever they may be). It would be much easier if you could write a script to do them, but the program doesn't allow it, and so you end up doing all this repetitive work for nothing.

Instead, you could use command line programs and write a script to do all the steps. Then, you just run this script and there you go. Or you can schedule it to run at certain times. If your GUI doesn't have scheduling and a way of sequencing things, you're reduced to doing it manually every time. And adding those features to a GUI would be a lot of work. With a command line, you get it all for free.

That's without even getting into piping from one program to another to do something the creators of the two programs wouldn't have thought of. The whole becomes more than the sum of the parts.

Unix command line is truly, staggeringly powerful and flexible. I love GUIs and they have their place, but sometimes only the command line will do. It depends on the task. And GUIs usually take a lot more time to develop than a command line driven program. Nor are the two mutually exclusive, of course.
 
Grep said:
Simply put, command line is more flexible than a GUI for a lot of tasks, and for a fraction of the development work.
Command line and GUI interface aren't mutually exclusive. As I mentioned before, there may be a set of icons to launch a GUI based program, each with different command line parameters for invoking the GUI based program, such as a debug or setup mode option for an application.

Maybe the question is what is the point of using C++ for a command line program when most of the predefined classes are GUI oriented, so not much is being leveraged by using C++ for a command line program. An example where C++ is still an advantage is the standard template libary, which includes a more generic set of classes and functions (vectors, sorting, ...).
 
  • #10
rcgldr said:
Command line and GUI interface aren't mutually exclusive.
Indeed. Pretty much what I said in the last sentence in my last post. I completely agree with your first paragraph.
rcgldr said:
Maybe the question is what is the point of using C++ for a command line program when most of the predefined classes are GUI oriented, so not much is being leveraged by using C++ for a command line program. An example where C++ is still an advantage is the standard template libary, which includes a more generic set of classes and functions (vectors, sorting, ...).
Don't know that I'd say that, though. In fact, C++ is completely independent from GUI toolkits. There is nothing about GUIs in any part of the C++ standard. Sure, C++ is well suited for GUIs, but there are many, many other tasks where it's just as well suited. C++ is not biased in any way towards GUI programming, really. It's a fully general purpose programming language.
 
  • #11
I don't think anyone has mentioned yet that under Windows Explorer if you drag-and-drop one or more icons for documents onto the icon of an EXE file, all the filenames are passed to the EXE file as its argv list. I suspect this may also happen under other operating systems' GUIs.

It's quite convenient if your application needs to open a user-specified file, but you don't want the hassle of either typing the file name or selecting it through a dialog.
 
  • #12
DrGreg said:
I don't think anyone has mentioned yet that under Windows Explorer if you drag-and-drop one or more icons for documents onto the icon of an EXE file, all the filenames are passed to the EXE file as its argv list.

That's totally awesome.

It might make sense when you're writing something that's more-or-less throw away code, or if you REALLY can't be bothered.
Or if you really don't need it for whatever reason, like a library-most library code won't need to accept cmd lines args 'cause it's not meant to be accessed that way.

Maybe the question is what is the point of using C++ for a command line program when most of the predefined classes are GUI oriented,...
'cause a GUI is overkill for your needs?

*shrugs* I maintain a cmd line interface to my current python scripts because it's awesome for testing purposes: instead of opening up a script or config file to change some parameter, it's just a switch, which is super useful when I have to fiddle with the parameters a zillion ways to get something just right or if I just want to do something silly like change the file format for the figures I'm generating.
 
  • #13
rcgldr said:
Maybe the question is what is the point of using C++ for a command line program when most of the predefined classes are GUI oriented

There are no predefined GUI classes... in fact

Sure, C++ is well suited for GUIs

IMO C++ is not well suited for GUIs. It's not object oriented enough. Notice how many GUI libraries immediately start by trying to break out of the C++ box and into a more dynamic language (Java, .NET, Cocoa, even WxWindows and Qt do crazy preprocessor stuff and are not proper C++).
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
4
Views
5K
Replies
89
Views
7K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
73
Views
6K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
5K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K