clang++ infile.cpp
ls -l -a -h
apt install programname
In C++, arguments are passed to main(.)
argc
argv
int main(int argc, char** argv) {
/* work with arguments */
return 0;
}
#include <iostream>
int main(int argc, char **argv) {
for (int i{0}; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
return 0;
}
Download echo.cpp
#include <iostream>
#include <cstdlib>
#include <string>
using std::cout, std::endl, std::string;
int main(int argc, char **argv) {
for (int i{1}; i < argc; ++i) {
string arg{argv[i]};
if (arg == "-h" or arg == "--help") {
cout << "Usage: a.out [OPTION]... " << endl;
cout << "-h, --help display this help and exit" << endl;
std::exit(0);
}
}
cout << "To solve the world's problems, use the correct arguments" << endl;
return 0;
}
Download help.cpp
getopt
#include <iostream>
#include <string>
#include <unistd.h> /* for getopt */
using std::cout, std::endl, std::oct, std::string;
int main (int argc, char **argv) {
int c{0};
bool aopt = false, bopt = false; // flags
string copt, dopt; // option arguments
while ((c = getopt(argc, argv, "abc:d:")) != -1) {
switch (c) {
case 'a':
aopt = true;
break;
case 'b':
bopt = true;
break;
case 'c':
copt = optarg;
break;
case 'd':
dopt = optarg;
break;
default:
cout << "?? getopt returned character code 0" << oct << c << " ??\n";
}
}
if (optind < argc) {
cout << "non-option ARGV-elements: ";
while (optind < argc) {
cout << argv[optind++] << " ";
}
cout << endl;
}
cout << "aopt: " << aopt << ", bopt: " << bopt << ", copt: " << copt
<< ", dopt: " << dopt << endl;
return 0;
}
Download posix_getopt.cpp
getopt_long
no_argument | required_argument | optional_argument
optarg
;
otherwise optarg
is NULL
flag
is NULL
, getopt_long
returns val
flag
is a pointer to an int
variable,
val
is assigned to the variablegetopt_long
returns 0getopt_long
or stored
in the flag variable depending on the scenario#include <getopt.h> // getopt_long
#include <iostream>
#include <string>
using std::cout, std::endl;
int main(int argc, char** argv) {
int c;
bool has_a = false, has_b = false; // flags
int is_superuser = 0;
int verbosity_level = 0;
std::string c_arg, d_arg; // option_arguments
static struct option long_options[] = {
/* NAME HAS_ARG FLAG VAL (SHORTNAME) */
{"add", required_argument, NULL, 0},
{"append", no_argument, NULL, 1000},
{"delete", no_argument, NULL, 0},
{"verbose", no_argument, NULL, 'v'},
{"create", required_argument, NULL, 'c'},
{"file", required_argument, NULL, 0},
{"config", optional_argument, NULL, 1001},
{"su", optional_argument, &is_superuser, 1},
{NULL, 0, NULL, 0}
};
int option_index = 0;
while ((c = getopt_long(argc, argv, "abc:d:vz",
long_options, &option_index)) != -1) {
switch (c) {
case 0:
cout << "option " << long_options[option_index].name;
if (optarg) {
cout << " with arg " << optarg;
}
cout << endl;
break;
case 1000: // append
cout << "`append` option was given" << endl;
break;
case 1001: // config
cout << "`config` option was given";
if (optarg) { // --config=configfilename
cout << " with arg " << optarg;
}
cout << endl;
break;
case 'a':
has_a = true;
break;
case 'b':
has_b = true;
break;
case 'c':
c_arg = optarg;
break;
case 'd':
d_arg = optarg;
break;
case 'v':
++verbosity_level;
break;
case '?': // getopt_long returns '?' on error (unknown option)
cout << "dealing with invalid option (see getopt_long output)" << endl;
break;
default: // "catches" option 'z' which isn't dealt with above
cout << "getopt returned character code 0" << std::oct << c << " ??\n";
}
}
if (optind < argc) {
cout << "non-option ARGV-elements: ";
while (optind < argc) {
cout << argv[optind++] << " ";
}
cout << endl;
}
if (is_superuser) {
cout << "Superuser mode is on" << endl;
}
cout << "a: " << has_a << ", b: " << has_b << ", c_arg: " << c_arg
<< ", d_arg: " << d_arg << ", verbosity-level: " << verbosity_level
<< endl;
return 0;
}
Download gnu_getopt.cpp
There are many libraries for argument parsing...
... select the one that suits your needs, eg.
cxxopts
Look at the source code of your favorite commanline programs...
This can be done via apt source $PACKAGE
eg. apt source htop
cli.c
main_cli.cpp
Write a C++ program that takes the following arguments
-h, --help
-a, --author
--color {red,green,blue}