๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“– C๋ž‘ C++

Boost::program_options ์‚ฌ์šฉ๊ธฐ

์˜ค๋Š˜์€ ์•„์นจ๋ถ€ํ„ฐ ์ผ์ด ์ž˜ ๋˜์ง€ ์•Š๋Š”๋‹ค

๋จธ๋ฆฌ๊ฐ€ ์ž˜ ์•ˆ ๋Œ์•„๊ฐ€์„œ ๋ง‰๋ง‰ํ•œ ๋Š๋‚Œ ใ… .,ใ…œ

๊ทธ๋ž˜์„œ~~ ๊ธฐ๋ถ„ ์ „ํ™˜ ์šฉ์œผ๋กœ ๊ธ€ ํ•˜๋‚˜ ์Œˆ๋ฐ•ํ•˜๊ฒŒ ์˜ฌ๋ฆฌ๊ณ  ๋‹ค์‹œ ์—ด์ผ ํ•ด๋ณด๊ฒ ๋‹ค

 

์ค€ํ˜ธ์”จใ…œใ…œ


์ด์ „์— ์ž‘์„ฑํ•œ boost ๊ด€๋ จ ๊ธ€์—๋„ ์ผ๋‹ค์‹œํ”ผ

๋‚˜๋Š” boost์— ๋งŽ์€ ๊ณ ๋งˆ์›€์„ ๋Š๋‚€๋‹ค +_+ ์—ฅ ์ด๊ฒŒ ์žˆ๋‹ค๊ณ ? ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ

์ด๋ฒˆ์— ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ๋ฅผ ์ž‘์„ฑํ•  ๊ฒŒ ์žˆ์–ด์„œ C๋กœ ํ•˜๋ ค๋‹ค๊ฐ€ C++๋กœ ํ•˜๋ ค๋Š”๋ฐ

์„ธ์ƒ์—.. Boost์—๋Š” program options๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์„œ

argument ๊ธฐ๋Šฅ์„ ์ฐธ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ๋†“์•˜๋‹ค

 


1. ๊ธด ์„œ๋ก 

 

์šฐ๋ฆฌ๊ฐ€ ์–ด๋–ค ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•  ๋•Œ, ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์‹คํ–‰์„ ํ•ด๋ณธ ๊ฒฝํ—˜์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค

argument๋ฅผ ํ”„๋กœ์„ธ์Šค ์‹คํ–‰ ํŒŒ์ผ ๋’ค์— ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณค ํ•˜๋Š”๋ฐ

 

$  gcc --help
Usage: gcc [options] file...
Options:
  -pass-exit-codes         Exit with highest error code from a phase
  --help                   Display this information
  --target-help            Display target specific command line options
  --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]
...

 

ํ”„๋กœ๊ทธ๋žจ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ command์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ๋•Œ๋„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค

Usage๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ๋•Œ --h ๋˜๋Š” --help ์š”๋Ÿฐ ์‹์œผ๋กœ 

์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์˜ ๋ชฉ๋ก์„ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค

 

์ด ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณต์„ ํ•ด์ฃผ๋Š” ์šฐ๋ฆฌ์˜ Boost..

 

https://www.boost.org/doc/libs/1_79_0/doc/html/program_options/tutorial.html

 

Tutorial - 1.79.0

In this section, we'll take a look at the most common usage scenarios of the program_options library, starting with the simplest one. The examples show only the interesting code parts, but the complete programs can be found in the "BOOST_ROOT/libs/program_

www.boost.org

 

์‹œ๊ฐ„์ด ์กฐ๊ธˆ ์žˆ์„ ๋•Œ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค

 

๋งŒ์•ฝ ์ด๊ฑธ ์‚ฌ์šฉํ•˜๊ธฐ ์‹ซ๊ณ  ๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ๋‹ค?? ๋˜๋Š” C๋กœ ๊ฐœ๋ฐœํ•˜๊ณ  ์‹ถ๋‹ค??

๊ทธ๋ ‡๋‹ค๋ฉด ์ •๋ง ๋งŽ์€ ๋‹จ๊ณ„์˜ if-else ๋ฌธ์„ ์“ฐ๋ฉฐ ํ—ˆ์šฐ์ ๊ฑฐ๋ฆด ๊ฒƒ์ด๋‹ค...

๋‚˜๋„ ๊ทธ๋žฌ๋‹ค

 

 


2. ์‚ฌ์šฉ๋ฒ•

 

์šฐ์„  Header file ๋ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค

 

#include <boost/program_options.hpp>
using namespace boost::program_options;
boost_program_options

 

๋ฐ”๋กœ ์˜ˆ์ œ๋ถ€ํ„ฐ ๋ณด๊ณ ~~ ํ•˜๋‚˜ํ•˜๋‚˜ ์ •๋ฆฌํ•ด๋ณด๊ฒ ๋‹ค

 

 

(1) options_description

 

string name;
int age = 0;

options_description desc{"Options"};
        desc.add_options()
            ("help,h", "Help screen")
            ("name", value<string>(&name)->default_value("bell2"), "User Name")
            ("age", value<int>(&age)->notifier(on_age), "User Age");

 

options_description class๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด usage์˜ ์„ค๋ช…์„ ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค

๋˜ํ•œ add_options()๋ผ๋Š” ํ•จ์ˆ˜์— operator()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŽธํ•˜๊ฒŒ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค

 

("name", value<string>()->default_value("bell2"), "User Name")

์š”๋ ‡๊ฒŒ ์“ฐ๋ฉด "name"์€ ์ถ”๊ฐ€ํ•  options์˜ key๊ฐ€ ๋˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž์—๋Š” value๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค

์ด๋ ‡๊ฒŒ key, value ํ˜•ํƒœ๋กœ ์ €์žฅ์„ ํ•˜๋ฉด ์ € key๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋“ฑ์˜ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค

 

๋‘ ๋ฒˆ์งธ ์ธ์ž์˜

value<string>(&name)->default_value("bell2")

์š”๊ฒƒ์€ --name์˜ ์˜ต์…˜์˜ ๊ฐ’์€ string์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ด๊ณ ,

๊ทธ ๊ฐ€์ ธ์˜จ ๊ฐ’์„ name์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค

default_value๋ฅผ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ , value<string>(&name)๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค

 

๊ทธ๋ฆฌ๊ณ  ์„ธ ๋ฒˆ์งธ ์ธ์ž๋Š” ํ•ด๋‹น ์˜ต์…˜์˜ ์„ค๋ช…์œผ๋กœ, --help ์˜ต์…˜์„ ์ž…๋ ฅ ์‹œ์— ์ปค๋งจ๋“œ ์ฐฝ์— ๋ณด์—ฌ์ค€๋‹ค

 

๋‘ ๋ฒˆ์งธ ์ธ์ž๋“ค์„ ์ž˜ ๋ณด๋ฉด default_value ๋ง๊ณ ๋„ notifier๋ผ๋Š” ๊ฒƒ์ด ์žˆ๋Š”๋ฐ

value<int>(&age)->notifier(on_age)

์–˜๋Š” on_age๋ผ๋Š” callback ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•ด์„œ, age ์˜ต์…˜ ๊ฐ’์ด ์žˆ์œผ๋ฉด on_age()๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ call์ด ๋œ๋‹ค

 

 

(2) variables_map

์–˜๊ฐ€ ์ฐธ ์žฌ๋ฐŒ๋Š” ๊ธฐ๋Šฅ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค

 

variables_map vm;
store(parse_command_line(argc, argv, desc), vm);
notify(vm);

 

vaialbe_map์ด๋ผ๋Š” ๊ฑด ์˜ต์…˜๋“ค์„ key, value ํ˜•ํƒœ๋กœ map์œผ๋กœ ์ €์žฅ์„ ํ•ด์ค€๋‹ค

 

store()

์˜ต์…˜๋“ค์— ๋Œ€ํ•ด ์ตœ์ข… ๊ฐ’์œผ๋กœ ์ €์žฅ์„ ํ•ด์ค€๋‹ค

์šฐ์„ ์ˆœ์œ„๋‚˜ notify ์—ฌ๋ถ€ ๋“ฑ์„ ํฌํ•จํ•ด์„œ~~

 

notify()

์˜ˆ๋Š” ์˜ต์…˜์˜ ์ตœ์ข… ๊ฐ’์ด ๊ฒฐ์ •๋˜๋ฉด ํ•ด๋‹น ์˜ต์…˜์—์„œ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์„œ

์บก์Šํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค

์•„๊นŒ on_age ๊ฐ™์€ callback ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•˜๋ฉด ์ด๋ฅผ ์‹คํ–‰ํ•ด์ค€๋‹ค๋Š” ๋œป

 

 

(3) ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๊ธฐ

 

if (vm.count("help"))
            std::cout << desc << '\n';
else if (vm.count("age"))
            std::cout << "Age: " << vm["age"].as<int>() << '\n';
else if (vm.count("name"))
            std::cout << "Name: " << vm["name"].as<string>() << '\n';
...

 

๊ทธ๋Ÿผ ๊ฒฐ๊ณผ๋ฅผ ์–ด๋–ป๊ฒŒ ์ถœ๋ ฅํ•˜๋Š๋ƒ

๊ฐ’ ์ €์žฅ์€ ์œ„ํ•ด์„œ ์„ค๋ช…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ value<T>(&name) ์ด๋ ‡๊ฒŒ ํ•ด์•ผ ํ•˜๊ณ 

๋งŒ์•ฝ ์ €์žฅ์€ ์•ˆ ํ•˜๊ณ  ๊ฐ€๋ณ๊ฒŒ ์ถœ๋ ฅ๋งŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด

vm["age"].as<int>() ์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค

 


์ „์ฒด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

 

#include <boost/program_options.hpp>
#include <iostream>
#include <string>

using namespace boost::program_options;
using namespace std;

void on_age(int age)
{
	std::cout << "On age: " << age << '\n';
}

int main(int argc, const char *argv[])
{
	try
	{
		options_description desc{"Options"};
		desc.add_options()
			("help,h", "Help screen")
			("name", value<string>()->default_value("bell2"), "User Name")
			("age", value<int>()->notifier(on_age), "User Age");

		variables_map vm;
		store(parse_command_line(argc, argv, desc), vm);
		notify(vm);

		if (vm.count("help"))
			std::cout << desc << '\n';
		else if (vm.count("age"))
			std::cout << "Age: " << vm["age"].as<int>() << '\n';
		else if (vm.count("name"))
			std::cout << "Name: " << vm["name"].as<string>() << '\n';
	}
	catch (const error &ex)
	{
		std::cerr << ex.what() << '\n';
	}
	return 0;
}

 

๊ทธ๋Ÿผ ์ด๋งŒ


์ฐธ๊ณ  ์ž๋ฃŒ

 

https://www.boost.org/doc/libs/1_79_0/doc/html/program_options/overview.html#id-1.3.32.5.7.9

 

Library Overview - 1.79.0

In the tutorial section, we saw several examples of library usage. Here we will describe the overall library design including the primary components and their function. To be a little more concrete, the options_description class is from the options descrip

www.boost.org