Hi everyone (again!!)

Sorry for all the questions, but I was wondering if anyone could help me a bit....

I jknow a bit about passing command line parameters when running a C++ app on Ubuntu through the terminal. At the minute I have the app either running with no command line parameters, running it with just passing a numeric value, with a string (for output path) or both parameters. What I was wondering is how do you get it too look more "professional"

e.g. you see programs where you pass parameters like

./myprogram --input=thispath/thisfile.txt --outputfile=thatpath/thatfile.txt --generate=50

is it possible to do this in C++ without the use of any extra libraries (such as Boost)? Not looking anyone to code me an answer, just some pointers or advice to help get me started thats all, any help would be much appreciated :)

Recommended Answers

All 5 Replies

I don't understand, you can't pass the arguments, or you don't know how to make it look all nice? To pass the arguments, you just take the parameters from main:

int main(int argNum,const char* argv[]) {}

argNum is the number of arguments (with spaces), 1st one being the program name itself and the second one is array of char arrays. The rest is just how you code it.

For example, if you want to pass the pass as the argument, you can loop and search for char array -path= and then store the things after = in another char array and use it as a path.

Yeah sorry didn't explain it too well, I know how to pass the arguments, just wanted a way for the user to enter some sort of indicator of what the parameter is before the actual parameter i.e. --path=thepath/folder/file.txt

You simply parse each string (argv[1], argv[2], ...) one at a time.

Decide on a parameter format and parse based on your decision. For example, my style is prog -iInputfile -oOutput -tTimeValue -nNumeric So if any string doesn't start with a -, the parameters are improperly formed
Then test the 2nd character and parse the rest of the string according to the format specified (i & o=file format, t=hh:mm format, etc.)

The format you specified is
1) start with 2 dashes --
2) next is a keyword
3) then a single =
4) finally a value in the proper format for keyword

Since you are on a Posix system, the best practice would be to adhere to getopt(). It's a part of libc; no extra libraries involved.

Hi,

thanks for the replies, gonna have a look at some of the ways to get it done and hopefully not run into anymore problems! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.