You are not logged in.

#1 2014-04-27 14:12:02

TheDarkByte
Member
From: Sivac, Serbia
Registered: 2014-04-27
Posts: 2
Website

How to pass arguments to my C++ programs ran from Terminal

Hello everyone. I am new here so sorry If I posted this to a wrong board smile
Arch Linux rocks and I have been using it for some time, long enough to try to improve it a bit and add my own software smile)
I wanted to create some cool "commands" to use in Terminal or, without GUI, just while using the command interface.

The idea was simple, create a console application for Linux, and copy it to
/bin

it worked big_smile The application I created in C++ using Code::Blocks was called "passgen"
and I want to use it like any other normal linux command that can be executed from Terminal.
for instance.

passgen -f ~~~example~ --use-numbers --use-signs --upper-case --screen-output

so, yes, I want my application to understand the arguments that were past to it from Terminal.
this command would actually tell my program to create a wordlist in the given format ( thanks to -f ) 
where all the ~ signs will be replaced with different signs. --use-numbers and --use-signs will tell the program
to allow numbers and signs, and --upper-case will tell the program to use upper case letters. And the --screen-output
will determinate will the information be printed to screen or written to a file in the directory user is currently in.
The program will then generate all the possible cases and print all of them seperated by new line.

i thought cin.getline() could do it. You know, to get the string and then extract the data I need, but it only works if I
typed this in Terminal:

passgen
-f ~~~exaple~ --use-numbers --use-signs --upper-case --screen-output

so.. do you know how can I get it to be all in one line only?

Thanks, and sorry  for the novel big_smile

Last edited by TheDarkByte (2014-04-27 14:19:39)


---
TheDarkByte

Offline

#2 2014-04-27 14:21:08

Stebalien
Member
Registered: 2010-04-27
Posts: 1,239
Website

Re: How to pass arguments to my C++ programs ran from Terminal

Google "command line arguments cpp". (searching for documentation is the most useful skill you will learn as a programmer).

Last edited by Stebalien (2014-04-27 14:21:37)


Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C

Offline

#3 2014-04-27 14:25:35

TheDarkByte
Member
From: Sivac, Serbia
Registered: 2014-04-27
Posts: 2
Website

Re: How to pass arguments to my C++ programs ran from Terminal


---
TheDarkByte

Offline

#4 2014-04-27 14:44:10

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: How to pass arguments to my C++ programs ran from Terminal

It's also common to have a for loop:

int main(int argc, const char **argv) {
   int i;
   for (i = 1; i < argc; i++) {
      /* do something with argv[i] */
   }
   return 0;
}

While I prefer handling these bits manually like this (and I definitely think everyone should for a bit while learning) there is also the getopt library which is used in a large portion of FOSS software.

Please remember to edit your first post to prepend "[SOLVED]" to the title.

Last edited by Trilby (2014-04-27 14:44:34)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2014-04-27 14:58:34

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,614

Re: How to pass arguments to my C++ programs ran from Terminal

Moving to Programming and Scripting


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

Board footer

Powered by FluxBB