C++ | Abseil flags Library














































C++ | Abseil flags Library



The Abseil Flags Library


Why do we use flag ?

We use flags instead of compile-time constants to allow user to specify a non-default value on the command line.

The Abseil flag library allows access to flag values passed on the command-line to binaries. 
Following are the features of flag library -

  • It allows to access Abseil flag in thread-safe manner.
  • It allows to access the flag values, that are valid at any point during program's lifetime.
  • It prevents conflict in flag name by ensuring unique name in a binary.
  • It provides both read and write access of flag.
  • It supports boolean, integer, string and also other Abseil types or custom types.


Header file to be included is - 

#include "absl/flags/flag.h"

Defining Flags

To define a flag, we use the following macro -
      ABSL_FLAG(type, name, default, help-text) , this will create a global variable named FLAGS_name
of the specified type and default value.

Example : 
ABSL_FLAG(std::string, output_dir, "example_string", "output file dir");
ABSL_FLAG(absl::Duration, timeout, absl::Seconds(30), "Default RPC deadline");

Standard Flags
Abseil flag library supports the following types of flags -
  • bool
  • int16_t
  • uint16_t
  • int32_t
  • uint32_t
  • int64_t
  • uint64_t
  • float
  • double
  • std::string
  • std::vector<std::string>
  • absl::LogSeverity
Apart from the standard flags, Abseil library also provide their own custom support for flags.
  • absl::Duration
  • absl::Time
  • absl::CivilSecond
  • absl::CivilMinute
  • absl::CivilHour
  • absl::CivilDay
  • absl::CivilMonth
  • absl::CivilYear
     

Accessing Flags 

 The flags define, can be accessed by absl::GetFlag() and absl::SetFlag() method. 

     // Creates variable "absl::Flag<absl::Duration> FLAGS_timeout;"
ABSL_FLAG(absl::Duration, timeout, absl::Seconds(30), "Default RPC timeout"); // Read the flag absl::Duration d = absl::GetFlag(FLAGS_timeout); // Modify the flag absl::SetFlag(&FLAGS_timeout, d + absl::Seconds(10));


Validating the flag values 

To know whether or not the contents of the flag are within the scope of it's definition, we need to validate the flags. 
Example -
Suppose we have a flag that gives the port number of the program running. Since there are 32768 ports, [0, .., 32767], so the number port number must be within this range only, anything else would be considered as an error.

#include <string> #include "absl/flags/flag.h" #include "absl/flags/marshalling.h" #include "absl/strings/string_view.h" struct PortNumber { explicit PortNumber(int p = 0) : port(p) {} int port; // Valid range is [0..32767] }; // Returns a textual flag value corresponding to the PortNumber `p`. std::string AbslUnparseFlag(PortNumber p) { // Delegate to the usual unparsing for int. return absl::UnparseFlag(p.port); } // returns true on success and error otherwise // on failure. bool AbslParseFlag(absl::string_view text, PortNumber* p, std::string* error) { // Convert from text to int using the int-flag parser. if (!absl::ParseFlag(text, &p->port, error)) { return false; } if (p->port < 0 || p->port > 32767) { *error = "not in range [0,32767]"; return false; } return true; } ABSL_FLAG(PortNumber, port, PortNumber(0), "What port to listen on");


If AbslParseFlag() return false, the process will exit with an error message.  

Implementation of GetFlag() and SetFlag() method :

#include <iostream> #include "absl/flags/flag.h" ABSL_FLAG(int, cnt, 0, "just count flag as example"); int main() { int count = absl::GetFlag(FLAGS_cnt); std::cout << "default value of flag is : " << count << std::endl; absl::SetFlag(&FLAGS_cnt, 10); count = absl::GetFlag(FLAGS_cnt); std::cout << "modified value of flag is : " << count << std::endl; return 0; }

Output :
           


More Articles of Anuj Maurya:

Name Views Likes
C++ | Find smallest interval 222 0
C++ | Lexicographic rank of string 95 0
C++ | Counting Rooms (CSES) 92 0
C++ | Building Roads (CSES) 78 0
C++ | High Score (CSES) 199 0
C++ | Collect maximum coin in grid 75 0
C++ | Is this k-palindrome ? 75 0
C++ | Shuffle the array elements 64 0
C++ | Fizz Buzz 81 0
C++ | Check if given points form square 59 0
C++ | Klee Algorithm to find union of line segment 87 0
C++ | Check if points are collinear 75 0
C++ | Check if given point lies inside triangle 82 0
C++ | Check if two line segment intersect 78 0
C++ | Number of ways to decode message 74 0
C++ || Minimise the subset difference 60 0
C++ | Count of subsets with given sum 67 24
C++ | Currency Arbitrage 97 22
C++ | Estimating the value of π (pi) using Monte-Carlo Simulation 57 18
C++ | Find the first missing number 75 16
C++ | Finding word in grid using DFS 66 11
C++ | Implementing pow(x, y) in logarithmic time 52 9
C++ | Sum of all factors of number 80 10
C++ | Building Teams (CSES) 69 21
C++ | Positions of anagram 47 12
C++ | Integer factorization 55 13
C++ | Primality test 63 14
C++ | Subset Sum Problem 64 12
C++ | Civil Time in Abseil 66 19
C++ | Abseil Time Duration 119 15
C++ | absl::Time 92 12
C++ | Introduction to Abseil Time library 81 9
C++ | absl::StripPrefix() and absl::StripSuffix() 79 8
C++ | absl::ConsumePrefix() and absl::ConsumeSuffix() 92 19
C++ | absl::string_view 71 13
C++ | absl::Substitute() | Formatting string in abseil 84 15
C++ | absl::StrJoin() | Yet another way of joining strings 116 7
C++ | String concatenation in Abseil - 2 | absl::StrCat() 103 10
C++ | absl::StrAppend() | String concatenation in abseil - 1 78 16
C++ | absl::StrSplit() 149 15
C++ | Appending to string and Writing to stream in Abseil 86 11
C++ | absl::PrintF(), absl::FPrintF() and absl::SNPrintF() 96 0
C++ | Abseil StrFormat() 70 27
C++ | Abseil flags Library 123 348
C++ | Abseil Numeric Library 86 65
C++ | Abseil Hash library 81 0
C++ | Abseil Containers 139 46
C++ Introduction to Abseil 88 13
CSES | Shortest Route II 251 47
CSES | Shortest Routes I 187 1

Comments