[C] Command Line args and main

f0ur

Newbie
Joined
Mar 6, 2019
Messages
25
Reaction score
11
Hello, world!

I kind of forgot this existed (not gonna lie), but I am slightly back. Ish. I have several questions regarding command line args and main, but first of all, story time (ish).

I am currently working on a program that uses a lot of command line arguments because the user needs to specify what flags and such they want depending on the architecture of the system that the program runs in. My issue that I am finding is that currently I have too many if-else statements checking these args, but I am confident that this is not at all efficient and well, a bit sloppy. I am thinking of creating another function that utilizes the switch-case cuz from what I have seen when case is used, it makes prettier code and also, easier to maintain and update. Any thoughts and suggestions are welcome.

~ Regards
 
Hello, world!

I kind of forgot this existed (not gonna lie), but I am slightly back. Ish. I have several questions regarding command line args and main, but first of all, story time (ish).

I am currently working on a program that uses a lot of command line arguments because the user needs to specify what flags and such they want depending on the architecture of the system that the program runs in. My issue that I am finding is that currently I have too many if-else statements checking these args, but I am confident that this is not at all efficient and well, a bit sloppy. I am thinking of creating another function that utilizes the switch-case cuz from what I have seen when case is used, it makes prettier code and also, easier to maintain and update. Any thoughts and suggestions are welcome.

~ Regards
Don't write your own. Use a command line parser library. There are a lot out there already.
 
It depends on your skill level. Using a library will not necessarily make things better for you, but it could be a good solution. There's a boost lib for C++ for instance, Program_Options https://www.boost.org/doc/libs/1_70_0/doc/html/program_options.html
 
It depends on your skill level. Using a library will not necessarily make things better for you, but it could be a good solution. There's a boost lib for C++ for instance, Program_Options https://www.boost.org/doc/libs/1_70_0/doc/html/program_options.html
I could probs make my own header file and whatever if I wanted to, but that is another issue entirely.
Also, I should note that my goal is not to use a third party library. I see where you are trying to go, but that isn't 100% what I am looking for. The issue isn't parsing the command line args, but efficiency (program still faster than I need it to be, but...).
 
so your program is slow because of the parsing of the command line arguments? also, this part sounds fishy: "program still faster than I need it to be" - do you want to make your program slower?

how do you do the parsing now and how would you like it to actually be?
 
so your program is slow because of the parsing of the command line arguments? also, this part sounds fishy: "program still faster than I need it to be" - do you want to make your program slower?

how do you do the parsing now and how would you like it to actually be?

Oh! My bad. Lol. Pretty sure I wrote that while half asleep. Anyways... what I meant was that, I am looking for a 'more efficient' way of doing things because I am aware that if-else statements can well, make very ugly code and make it harder to maintain. I was thinking long term if this project of mine becomes 'bigger' than it is right now.
 
If you want clean looking code it's def. a command line parsing library you are looking for. You can code your own if you feel like wasting a few days but it's def. what you are looking for in some form or other.
 
Back
Top