Command line options

I have a problem with command line options. I finished the program to run it from the command line:

program.exe test.txt copy_test.txt

Basically, my program does the following:

  • enter a text file
  • sorts it and copies to a new text file

BUT (always this, but ?!), I have to run the program from the command line as follows:

program.exe -input=test.txt -output=copy_test.txt

And I don’t know how to do it. I researched, but I did not find any help :(

Please reply.

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

int main ( int argc, char* argv[])
{
 ifstream in(argv[1]);
 ofstream out(argv[2]);
 vector <string> sV;
 string line;
 while (in >> line)
  sV.push_back(line);
 for ( int i = 0; i < sV.size(); i++)
 sort ( sV.begin(), sV.end () );
 for ( int i = 0; i < sV.size(); i++)
 out << sV[i] << endl;
 cin.get();
 return 0;
}
+3
source share
4 answers

Well, with your new arguments in the format, you cannot just pass them as is for the stream constructors.

, , , strncmp, , argv[1]+8 .

--input=, , .

, :

int main (int argc, char* argv[]) {
    ifstream in(argv[1]);
    ofstream out(argv[2]);

- :

int main (int argc, char* argv[]) {
    char *infile = 0;
    char *outfile = 0;
    for (int i = 1; i < argc; i++) {
        if (strncmp (argv[i], "--input=", 8) == 0) {
            infile = argv[i] + 8;
        } else {
            if (strncmp (argv[i], "--output=", 9) == 0) {
                outfile = argv[i] + 9;
            } else {
                std::cerr << "Invalid argument [" << argv[i] << "]" << std::endl;
                return -1;
            }
        }
    }
    if ((infile == 0) || (outfile == 0)) {
        std::cerr << "Need to specify input and output file" << std::endl;
        return -1;
    }

    ifstream in(infile);
    ofstream out(outfile);
0

main argv, , -input, -output .. ..

- , , , , boost.program_options

+7

, -help-, .

, , , .

- , .

0

, . , ++, , "getopt". getopt_long(), (, -c), (, --input). getopt_long_only(), . . .

, , :

#include <iostream>
#include <getopt.h>
#include <map>
#include <string>

int main (int argc, char** argv)
{
    // Create the variables to store your parameters
    std::map<std::string, std::string> input_parameters ;
    input_parameters["input"] = "default_in" ;   // Storage for input
    input_parameters["output"] = "default_out" ; // Storage for output

    // Create variables to hold your parameters
    const struct option longopts[] =
    {
        {"input", required_argument, 0, 'i'},
        {"output", required_argument, 0, 'o'},
        {0,0,0,0} // This tells getopt that this is the end
    };

    // Some parameters for getopt_long
    int c(0);

    // Get the options from the command line
    while (c != -1) {
        int option_index(-1) ;

        // Read the next command line option
        // Note here that the ':' after the 'i' and 'o' denotes that
        // it requires an argument
        c = getopt_long(argc, argv, "i:o:", longopts, &option_index) ;

        // If the option is valid, fill the corresponding value
        if ((c>0)&&(option_index>=0)) {
            std::cout << option_index << std::endl;
            input_parameters[longopts[option_index].name] = optarg ;
        }

        switch (c) {
            case 'i':
                // Fill input option
                input_parameters["input"] = optarg ;
            case 'o':
                // Fill output option
                input_parameters["output"] = optarg ;
            case '?':
                // getopt_long printed an error message
                break ;
        }
    }

    std::cout << "input  = " << input_parameters["input"] << std::endl;
    std::cout << "output = " << input_parameters["output"] << std::endl;

    return 0 ;
}

, , , . :

$ ./myscript --input inputfile.txt --output outputfile.txt
input  = inputfile.txt
output = outputfile.txt

$ ./myscript -i inputfile.txt -o outpufile.txt
input  = inputfile.txt
output = outputfile.txt

--input -i ( --output -o).

( CLOptions , getopt)

, getopt , , , int . ! , "CLOptions", #include "CLOptions.h" ( ), . -h -help ! bool, double, int string , . GitHub how could the above method be implemented . Note that the class is C ++ 11 and will require -std=c++11at compile time (although if someone asks, I can try to create version C code).

Although I have not tried them yet, there are also many other command line programs that were developed by other people to solve this problem (e.g. options or dropt ). You can probably find them by clicking on the link.

0
source

Source: https://habr.com/ru/post/1787417/


All Articles