Pure Programmer
Blue Matrix


Cluster Map

Command Line Arguments

L1

This page is under construction. Please come back later.

When running programs from the command line we have the option of supplying additional information in the form of arguments on the command line. Command line arguments take the form of strings separated by spaces. For example the following command line program invokation has three additional arguments.

$ ./a.out Fred 123 Flintstone

In our programs we can access each of these arguments using the variables argv[0], argv[1], argv[2], etc. The program name can be found in the variable with the 0 subscript. The user supplied arguments are in the variables with subscripts 1, 2, 3, etc.

CmdLineArgs1.cpp
#include "Utils.hpp"
#include <clocale>
#include <codecvt>
#include <iostream>
#include <string>

std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>);
using namespace std;

int main(int argc, char **argv) {
	setlocale(LC_ALL, "en_US.UTF-8");
	wcout.imbue(utf8loc);
	wcin.imbue(utf8loc);

	wcout << L"Number of arguments: " << (argc - 1) << endl;
	wcout << L"Program Name: " << Utils::UTF8_to_wstring(argv[0]) << endl;
	wcout << L"Arg 1: " << Utils::UTF8_to_wstring(argv[1]) << endl;
	wcout << L"Arg 2: " << Utils::UTF8_to_wstring(argv[2]) << endl;
	wcout << L"Arg 3: " << Utils::UTF8_to_wstring(argv[3]) << endl;
	wcout << L"Arg 4: " << Utils::UTF8_to_wstring(argv[4]) << endl;
	return 0;
}

Output
$ g++ -std=c++17 CmdLineArgs1.cpp -o CmdLineArgs1 -lfmt $ ./CmdLineArgs1 Fred Barney Wilma Betty Number of arguments: 4 Program Name: ./CmdLineArgs1 Arg 1: Fred Arg 2: Barney Arg 3: Wilma Arg 4: Betty $ g++ -std=c++17 CmdLineArgs1.cpp -o CmdLineArgs1 -lfmt $ ./CmdLineArgs1 Φρειδερίκος Барнеи ウィルマ 贝蒂 Number of arguments: 4 Program Name: ./CmdLineArgs1 Arg 1: Φρειδερίκος Arg 2: Барнеи Arg 3: ウィルマ Arg 4: 贝蒂
CmdLineArgs1u.cpp
File not found: CmdLineArgs1u.cpp
Output
File not found!: /kunden/homepages/39/d957328751/htdocs/pureprogrammer/cpp/examples/output/CmdLineArgs1u.out

If we want to treat a command line argument as a number we first need to convert from the string representation passed on the command line to a numeric type. This is where conversion functions come in handy. The example below illustrates how to convert command line arguments to integers.

CmdLineArgs2.cpp
#include "Utils.hpp"
#include <clocale>
#include <codecvt>
#include <fmt/format.h>
#include <fmt/xchar.h>
#include <iostream>
#include <string>

std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>);
using namespace std;

int main(int argc, char **argv) {
	setlocale(LC_ALL, "en_US.UTF-8");
	wcout.imbue(utf8loc);
	wcin.imbue(utf8loc);

	int const a = Utils::stoiWithDefault(Utils::UTF8_to_wstring(argv[1]), 0);
	int const b = Utils::stoiWithDefault(Utils::UTF8_to_wstring(argv[2]), 0);
	int const c = a + b;
	wcout << fmt::format(L"{0:d} + {1:d} = {2:d}", a, b, c) << endl;
	return 0;
}

Output
$ g++ -std=c++17 CmdLineArgs2.cpp -o CmdLineArgs2 -lfmt $ ./CmdLineArgs2 326 805 326 + 805 = 1131

The example below converts the command line arguments to floating point values.

CmdLineArgs3.cpp
#include "Utils.hpp"
#include <clocale>
#include <codecvt>
#include <fmt/format.h>
#include <fmt/xchar.h>
#include <iostream>
#include <string>

std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>);
using namespace std;

int main(int argc, char **argv) {
	setlocale(LC_ALL, "en_US.UTF-8");
	wcout.imbue(utf8loc);
	wcin.imbue(utf8loc);

	double const a = Utils::stodWithDefault(Utils::UTF8_to_wstring(argv[1]), 0);
	double const b = Utils::stodWithDefault(Utils::UTF8_to_wstring(argv[2]), 0);
	double const c = a + b;
	wcout << fmt::format(L"{0:f} + {1:f} = {2:f}", a, b, c) << endl;
	return 0;
}

Output
$ g++ -std=c++17 CmdLineArgs3.cpp -o CmdLineArgs3 -lfmt $ ./CmdLineArgs3 3.21 7.01 3.210000 + 7.010000 = 10.220000

Questions

Projects

More ★'s indicate higher difficulty level.

References