Making an one-line input

Hello

I was wondering, some applications do have a one line input.
Like:
myApp -o file.txt --extraOption 4 --amount 425


However, I always make my inputs like:
Output file:
> file.txt
ExtraOption:
> 4
Amount:
> 425


Now, I also want to have a one line-input. How can that be done? Do you search for those commands and then check the character after that command and that's the value of your variable, and do that all through a for loop?

Or is there a better way to do such an one-line input?

Thanks for reading,
Niely
If I don't misunderstand your question, then the second box above is the contents of an options file read in by myApp and you no longer want to only get those parameters from a separate file but also offer them as command line options.
Have a look at the getopt(3) man page of section 3.
man 3 getopt
It looks for one letter options only. If you do prefer long options as your example, then have a look at getopt_long(3).

As a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <unistd.h>

static std::string USAGE("myApp [-h] [-o <outfilename>]");
static int EXIT_SYNTAX(1);
static int OPTS("ho:");
static unsigned int NR_OF_REQUIRED_ARGS(0);

int scanCmdLine(int argc, char *argv[], string &outfname)
{
    int          opt;

    while ((opt = getopt(argc, argv, OPTS)) != -1)
    {
        switch (opt)
        {
        case 'h':       // Help
            cout << "USAGE " << myname << USAGE << endl;
            exit(EXIT_OK);
            break;

        case 'o':       // Outfile name
            outfname = optarg;
            break;

        default:
            clog << "unknown option [opt=" << ((char) opt) << "]" << endl;
                        clog << "USAGE: " << USAGE << endl;
            exit(EXIT_SYNTAX);
            break;
        }
    }

    if (optind != argc - NR_OF_REQUIRED_ARGS)
    {
        clog << "myApp: bad argument count" << endl;
        clog << "USAGE: "  << USAGE << endl;
        exit(EXIT_SYNTAX);
    }

    return optind;
}
Is there also a way to do it like this?

Every part of a string is a variable? The string get split by spaces and each part is a variable?

Example:

Input: file method amount

Split by spaces:

1. file
2. method
3. amount

Declaring variables:
string file_name = split_input_part_one
string method_name = split_input_part_two
int amount_value = split_input_part_three

So:
file_name is equally to "file"
method_name is equally to "method"
amount_value is equally to 'amount'

Of course it's a pseudo, but is it possible to do it like this?
1
2
3
4
5
#include <iostream>
int main(int argc, char **argv){
   for(int K=0; K<argc; ++K)
      std::cout << "Argument " << K << " is " << argv[K] << '\n';
}
I am not sure, if you mean passing command line arguments (as has been shown), or reading input once your program has started. Since the latter hasn't been shown yet, I will try to provide an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <sstream>

int main()
{
    std::string line;
    std::getline(std::cin, line);
    std::istringstream stream(line);

    std::string file, method, name;
    stream >> file >> method >> name;
    std::cout << "file: " << file << '\n'
              << "method: " << method << '\n'
              << "name: " << name << '\n';
}
That's it bugbyte!
Exactly what I needed. Thanks a lot! ;-)

Also thanks to everyone else!
Topic archived. No new replies allowed.